You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
8.4 KiB

6 years ago
6 years ago
6 years ago
6 years ago
4 years ago
  1. package s3api
  2. import (
  3. "encoding/xml"
  4. "fmt"
  5. "path/filepath"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/aws/aws-sdk-go/aws"
  10. "github.com/aws/aws-sdk-go/service/s3"
  11. "github.com/google/uuid"
  12. "github.com/chrislusf/seaweedfs/weed/filer"
  13. "github.com/chrislusf/seaweedfs/weed/glog"
  14. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  15. )
  16. type InitiateMultipartUploadResult struct {
  17. XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ InitiateMultipartUploadResult"`
  18. s3.CreateMultipartUploadOutput
  19. }
  20. func (s3a *S3ApiServer) createMultipartUpload(input *s3.CreateMultipartUploadInput) (output *InitiateMultipartUploadResult, code ErrorCode) {
  21. uploadId, _ := uuid.NewRandom()
  22. uploadIdString := uploadId.String()
  23. if err := s3a.mkdir(s3a.genUploadsFolder(*input.Bucket), uploadIdString, func(entry *filer_pb.Entry) {
  24. if entry.Extended == nil {
  25. entry.Extended = make(map[string][]byte)
  26. }
  27. entry.Extended["key"] = []byte(*input.Key)
  28. }); err != nil {
  29. glog.Errorf("NewMultipartUpload error: %v", err)
  30. return nil, ErrInternalError
  31. }
  32. output = &InitiateMultipartUploadResult{
  33. CreateMultipartUploadOutput: s3.CreateMultipartUploadOutput{
  34. Bucket: input.Bucket,
  35. Key: objectKey(input.Key),
  36. UploadId: aws.String(uploadIdString),
  37. },
  38. }
  39. return
  40. }
  41. type CompleteMultipartUploadResult struct {
  42. XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ CompleteMultipartUploadResult"`
  43. s3.CompleteMultipartUploadOutput
  44. }
  45. func (s3a *S3ApiServer) completeMultipartUpload(input *s3.CompleteMultipartUploadInput) (output *CompleteMultipartUploadResult, code ErrorCode) {
  46. uploadDirectory := s3a.genUploadsFolder(*input.Bucket) + "/" + *input.UploadId
  47. entries, _, err := s3a.list(uploadDirectory, "", "", false, 0)
  48. if err != nil || len(entries) == 0 {
  49. glog.Errorf("completeMultipartUpload %s %s error: %v, entries:%d", *input.Bucket, *input.UploadId, err, len(entries))
  50. return nil, ErrNoSuchUpload
  51. }
  52. var finalParts []*filer_pb.FileChunk
  53. var offset int64
  54. for _, entry := range entries {
  55. if strings.HasSuffix(entry.Name, ".part") && !entry.IsDirectory {
  56. for _, chunk := range entry.Chunks {
  57. p := &filer_pb.FileChunk{
  58. FileId: chunk.GetFileIdString(),
  59. Offset: offset,
  60. Size: chunk.Size,
  61. Mtime: chunk.Mtime,
  62. CipherKey: chunk.CipherKey,
  63. ETag: chunk.ETag,
  64. }
  65. finalParts = append(finalParts, p)
  66. offset += int64(chunk.Size)
  67. }
  68. }
  69. }
  70. entryName := filepath.Base(*input.Key)
  71. dirName := filepath.Dir(*input.Key)
  72. if dirName == "." {
  73. dirName = ""
  74. }
  75. if strings.HasPrefix(dirName, "/") {
  76. dirName = dirName[1:]
  77. }
  78. dirName = fmt.Sprintf("%s/%s/%s", s3a.option.BucketsPath, *input.Bucket, dirName)
  79. // remove suffix '/'
  80. if strings.HasSuffix(dirName, "/") {
  81. dirName = dirName[:len(dirName)-1]
  82. }
  83. err = s3a.mkFile(dirName, entryName, finalParts)
  84. if err != nil {
  85. glog.Errorf("completeMultipartUpload %s/%s error: %v", dirName, entryName, err)
  86. return nil, ErrInternalError
  87. }
  88. output = &CompleteMultipartUploadResult{
  89. CompleteMultipartUploadOutput: s3.CompleteMultipartUploadOutput{
  90. Location: aws.String(fmt.Sprintf("http://%s%s/%s", s3a.option.Filer, dirName, entryName)),
  91. Bucket: input.Bucket,
  92. ETag: aws.String("\"" + filer.ETagChunks(finalParts) + "\""),
  93. Key: objectKey(input.Key),
  94. },
  95. }
  96. if err = s3a.rm(s3a.genUploadsFolder(*input.Bucket), *input.UploadId, false, true); err != nil {
  97. glog.V(1).Infof("completeMultipartUpload cleanup %s upload %s: %v", *input.Bucket, *input.UploadId, err)
  98. }
  99. return
  100. }
  101. func (s3a *S3ApiServer) abortMultipartUpload(input *s3.AbortMultipartUploadInput) (output *s3.AbortMultipartUploadOutput, code ErrorCode) {
  102. exists, err := s3a.exists(s3a.genUploadsFolder(*input.Bucket), *input.UploadId, true)
  103. if err != nil {
  104. glog.V(1).Infof("bucket %s abort upload %s: %v", *input.Bucket, *input.UploadId, err)
  105. return nil, ErrNoSuchUpload
  106. }
  107. if exists {
  108. err = s3a.rm(s3a.genUploadsFolder(*input.Bucket), *input.UploadId, true, true)
  109. }
  110. if err != nil {
  111. glog.V(1).Infof("bucket %s remove upload %s: %v", *input.Bucket, *input.UploadId, err)
  112. return nil, ErrInternalError
  113. }
  114. return &s3.AbortMultipartUploadOutput{}, ErrNone
  115. }
  116. type ListMultipartUploadsResult struct {
  117. XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ ListMultipartUploadsResult"`
  118. // copied from s3.ListMultipartUploadsOutput, the Uploads is not converting to <Upload></Upload>
  119. Bucket *string `type:"string"`
  120. Delimiter *string `type:"string"`
  121. EncodingType *string `type:"string" enum:"EncodingType"`
  122. IsTruncated *bool `type:"boolean"`
  123. KeyMarker *string `type:"string"`
  124. MaxUploads *int64 `type:"integer"`
  125. NextKeyMarker *string `type:"string"`
  126. NextUploadIdMarker *string `type:"string"`
  127. Prefix *string `type:"string"`
  128. UploadIdMarker *string `type:"string"`
  129. Upload []*s3.MultipartUpload `locationName:"Upload" type:"list" flattened:"true"`
  130. }
  131. func (s3a *S3ApiServer) listMultipartUploads(input *s3.ListMultipartUploadsInput) (output *ListMultipartUploadsResult, code ErrorCode) {
  132. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListMultipartUploads.html
  133. output = &ListMultipartUploadsResult{
  134. Bucket: input.Bucket,
  135. Delimiter: input.Delimiter,
  136. EncodingType: input.EncodingType,
  137. KeyMarker: input.KeyMarker,
  138. MaxUploads: input.MaxUploads,
  139. Prefix: input.Prefix,
  140. }
  141. entries, isLast, err := s3a.list(s3a.genUploadsFolder(*input.Bucket), *input.Prefix, *input.KeyMarker, true, uint32(*input.MaxUploads))
  142. if err != nil {
  143. glog.Errorf("listMultipartUploads %s error: %v", *input.Bucket, err)
  144. return
  145. }
  146. output.IsTruncated = aws.Bool(!isLast)
  147. for _, entry := range entries {
  148. if entry.Extended != nil {
  149. key := entry.Extended["key"]
  150. output.Upload = append(output.Upload, &s3.MultipartUpload{
  151. Key: objectKey(aws.String(string(key))),
  152. UploadId: aws.String(entry.Name),
  153. })
  154. if !isLast {
  155. output.NextUploadIdMarker = aws.String(entry.Name)
  156. }
  157. }
  158. }
  159. return
  160. }
  161. type ListPartsResult struct {
  162. XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ ListPartsResult"`
  163. // copied from s3.ListPartsOutput, the Parts is not converting to <Part></Part>
  164. Bucket *string `type:"string"`
  165. IsTruncated *bool `type:"boolean"`
  166. Key *string `min:"1" type:"string"`
  167. MaxParts *int64 `type:"integer"`
  168. NextPartNumberMarker *int64 `type:"integer"`
  169. PartNumberMarker *int64 `type:"integer"`
  170. Part []*s3.Part `locationName:"Part" type:"list" flattened:"true"`
  171. StorageClass *string `type:"string" enum:"StorageClass"`
  172. UploadId *string `type:"string"`
  173. }
  174. func (s3a *S3ApiServer) listObjectParts(input *s3.ListPartsInput) (output *ListPartsResult, code ErrorCode) {
  175. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListParts.html
  176. output = &ListPartsResult{
  177. Bucket: input.Bucket,
  178. Key: objectKey(input.Key),
  179. UploadId: input.UploadId,
  180. MaxParts: input.MaxParts, // the maximum number of parts to return.
  181. PartNumberMarker: input.PartNumberMarker, // the part number starts after this, exclusive
  182. StorageClass: aws.String("STANDARD"),
  183. }
  184. entries, isLast, err := s3a.list(s3a.genUploadsFolder(*input.Bucket)+"/"+*input.UploadId, "", fmt.Sprintf("%04d.part", *input.PartNumberMarker), false, uint32(*input.MaxParts))
  185. if err != nil {
  186. glog.Errorf("listObjectParts %s %s error: %v", *input.Bucket, *input.UploadId, err)
  187. return nil, ErrNoSuchUpload
  188. }
  189. output.IsTruncated = aws.Bool(!isLast)
  190. for _, entry := range entries {
  191. if strings.HasSuffix(entry.Name, ".part") && !entry.IsDirectory {
  192. partNumberString := entry.Name[:len(entry.Name)-len(".part")]
  193. partNumber, err := strconv.Atoi(partNumberString)
  194. if err != nil {
  195. glog.Errorf("listObjectParts %s %s parse %s: %v", *input.Bucket, *input.UploadId, entry.Name, err)
  196. continue
  197. }
  198. output.Part = append(output.Part, &s3.Part{
  199. PartNumber: aws.Int64(int64(partNumber)),
  200. LastModified: aws.Time(time.Unix(entry.Attributes.Mtime, 0).UTC()),
  201. Size: aws.Int64(int64(filer.FileSize(entry))),
  202. ETag: aws.String("\"" + filer.ETag(entry) + "\""),
  203. })
  204. if !isLast {
  205. output.NextPartNumberMarker = aws.Int64(int64(partNumber))
  206. }
  207. }
  208. }
  209. return
  210. }