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.

218 lines
6.8 KiB

6 years ago
6 years ago
6 years ago
6 years ago
  1. package s3api
  2. import (
  3. "context"
  4. "encoding/xml"
  5. "fmt"
  6. "path/filepath"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/aws/aws-sdk-go/aws"
  11. "github.com/aws/aws-sdk-go/service/s3"
  12. "github.com/chrislusf/seaweedfs/weed/filer2"
  13. "github.com/chrislusf/seaweedfs/weed/glog"
  14. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  15. "github.com/google/uuid"
  16. )
  17. type InitiateMultipartUploadResult struct {
  18. XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ InitiateMultipartUploadResult"`
  19. s3.CreateMultipartUploadOutput
  20. }
  21. func (s3a *S3ApiServer) createMultipartUpload(ctx context.Context, input *s3.CreateMultipartUploadInput) (output *InitiateMultipartUploadResult, code ErrorCode) {
  22. uploadId, _ := uuid.NewRandom()
  23. uploadIdString := uploadId.String()
  24. if err := s3a.mkdir(ctx, s3a.genUploadsFolder(*input.Bucket), uploadIdString, func(entry *filer_pb.Entry) {
  25. if entry.Extended == nil {
  26. entry.Extended = make(map[string][]byte)
  27. }
  28. entry.Extended["key"] = []byte(*input.Key)
  29. }); err != nil {
  30. glog.Errorf("NewMultipartUpload error: %v", err)
  31. return nil, ErrInternalError
  32. }
  33. output = &InitiateMultipartUploadResult{
  34. CreateMultipartUploadOutput: s3.CreateMultipartUploadOutput{
  35. Bucket: input.Bucket,
  36. Key: objectKey(input.Key),
  37. UploadId: aws.String(uploadIdString),
  38. },
  39. }
  40. return
  41. }
  42. type CompleteMultipartUploadResult struct {
  43. XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ CompleteMultipartUploadResult"`
  44. s3.CompleteMultipartUploadOutput
  45. }
  46. func (s3a *S3ApiServer) completeMultipartUpload(ctx context.Context, input *s3.CompleteMultipartUploadInput) (output *CompleteMultipartUploadResult, code ErrorCode) {
  47. uploadDirectory := s3a.genUploadsFolder(*input.Bucket) + "/" + *input.UploadId
  48. entries, err := s3a.list(ctx, uploadDirectory, "", "", false, 0)
  49. if err != nil {
  50. glog.Errorf("completeMultipartUpload %s %s error: %v", *input.Bucket, *input.UploadId, err)
  51. return nil, ErrNoSuchUpload
  52. }
  53. var finalParts []*filer_pb.FileChunk
  54. var offset int64
  55. for _, entry := range entries {
  56. if strings.HasSuffix(entry.Name, ".part") && !entry.IsDirectory {
  57. for _, chunk := range entry.Chunks {
  58. p := &filer_pb.FileChunk{
  59. FileId: chunk.GetFileIdString(),
  60. Offset: offset,
  61. Size: chunk.Size,
  62. Mtime: chunk.Mtime,
  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(ctx, 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("\"" + filer2.ETag(finalParts) + "\""),
  93. Key: objectKey(input.Key),
  94. },
  95. }
  96. if err = s3a.rm(ctx, s3a.genUploadsFolder(*input.Bucket), *input.UploadId, true, 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(ctx context.Context, input *s3.AbortMultipartUploadInput) (output *s3.AbortMultipartUploadOutput, code ErrorCode) {
  102. exists, err := s3a.exists(ctx, 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(ctx, s3a.genUploadsFolder(*input.Bucket), *input.UploadId, true, 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. s3.ListMultipartUploadsOutput
  119. }
  120. func (s3a *S3ApiServer) listMultipartUploads(ctx context.Context, input *s3.ListMultipartUploadsInput) (output *ListMultipartUploadsResult, code ErrorCode) {
  121. output = &ListMultipartUploadsResult{
  122. ListMultipartUploadsOutput: s3.ListMultipartUploadsOutput{
  123. Bucket: input.Bucket,
  124. Delimiter: input.Delimiter,
  125. EncodingType: input.EncodingType,
  126. KeyMarker: input.KeyMarker,
  127. MaxUploads: input.MaxUploads,
  128. Prefix: input.Prefix,
  129. },
  130. }
  131. entries, err := s3a.list(ctx, s3a.genUploadsFolder(*input.Bucket), *input.Prefix, *input.KeyMarker, true, int(*input.MaxUploads))
  132. if err != nil {
  133. glog.Errorf("listMultipartUploads %s error: %v", *input.Bucket, err)
  134. return
  135. }
  136. for _, entry := range entries {
  137. if entry.Extended != nil {
  138. key := entry.Extended["key"]
  139. output.Uploads = append(output.Uploads, &s3.MultipartUpload{
  140. Key: objectKey(aws.String(string(key))),
  141. UploadId: aws.String(entry.Name),
  142. })
  143. }
  144. }
  145. return
  146. }
  147. type ListPartsResult struct {
  148. XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ ListPartsResult"`
  149. s3.ListPartsOutput
  150. }
  151. func (s3a *S3ApiServer) listObjectParts(ctx context.Context, input *s3.ListPartsInput) (output *ListPartsResult, code ErrorCode) {
  152. output = &ListPartsResult{
  153. ListPartsOutput: s3.ListPartsOutput{
  154. Bucket: input.Bucket,
  155. Key: objectKey(input.Key),
  156. UploadId: input.UploadId,
  157. MaxParts: input.MaxParts, // the maximum number of parts to return.
  158. PartNumberMarker: input.PartNumberMarker, // the part number starts after this, exclusive
  159. },
  160. }
  161. entries, err := s3a.list(ctx, s3a.genUploadsFolder(*input.Bucket)+"/"+*input.UploadId,
  162. "", fmt.Sprintf("%04d.part", *input.PartNumberMarker), false, int(*input.MaxParts))
  163. if err != nil {
  164. glog.Errorf("listObjectParts %s %s error: %v", *input.Bucket, *input.UploadId, err)
  165. return nil, ErrNoSuchUpload
  166. }
  167. for _, entry := range entries {
  168. if strings.HasSuffix(entry.Name, ".part") && !entry.IsDirectory {
  169. partNumberString := entry.Name[:len(entry.Name)-len(".part")]
  170. partNumber, err := strconv.Atoi(partNumberString)
  171. if err != nil {
  172. glog.Errorf("listObjectParts %s %s parse %s: %v", *input.Bucket, *input.UploadId, entry.Name, err)
  173. continue
  174. }
  175. output.Parts = append(output.Parts, &s3.Part{
  176. PartNumber: aws.Int64(int64(partNumber)),
  177. LastModified: aws.Time(time.Unix(entry.Attributes.Mtime, 0)),
  178. Size: aws.Int64(int64(filer2.TotalSize(entry.Chunks))),
  179. ETag: aws.String("\"" + filer2.ETag(entry.Chunks) + "\""),
  180. })
  181. }
  182. }
  183. return
  184. }