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.

212 lines
6.6 KiB

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/satori/go.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.NewV4()
  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: 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.FileId,
  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. err = s3a.mkFile(ctx, dirName, entryName, finalParts)
  80. if err != nil {
  81. glog.Errorf("completeMultipartUpload %s/%s error: %v", dirName, entryName, err)
  82. return nil, ErrInternalError
  83. }
  84. output = &CompleteMultipartUploadResult{
  85. CompleteMultipartUploadOutput: s3.CompleteMultipartUploadOutput{
  86. Bucket: input.Bucket,
  87. ETag: aws.String("\"" + filer2.ETag(finalParts) + "\""),
  88. Key: input.Key,
  89. },
  90. }
  91. if err = s3a.rm(ctx, s3a.genUploadsFolder(*input.Bucket), *input.UploadId, true, false, true); err != nil {
  92. glog.V(1).Infof("completeMultipartUpload cleanup %s upload %s: %v", *input.Bucket, *input.UploadId, err)
  93. }
  94. return
  95. }
  96. func (s3a *S3ApiServer) abortMultipartUpload(ctx context.Context, input *s3.AbortMultipartUploadInput) (output *s3.AbortMultipartUploadOutput, code ErrorCode) {
  97. exists, err := s3a.exists(ctx, s3a.genUploadsFolder(*input.Bucket), *input.UploadId, true)
  98. if err != nil {
  99. glog.V(1).Infof("bucket %s abort upload %s: %v", *input.Bucket, *input.UploadId, err)
  100. return nil, ErrNoSuchUpload
  101. }
  102. if exists {
  103. err = s3a.rm(ctx, s3a.genUploadsFolder(*input.Bucket), *input.UploadId, true, true, true)
  104. }
  105. if err != nil {
  106. glog.V(1).Infof("bucket %s remove upload %s: %v", *input.Bucket, *input.UploadId, err)
  107. return nil, ErrInternalError
  108. }
  109. return &s3.AbortMultipartUploadOutput{}, ErrNone
  110. }
  111. type ListMultipartUploadsResult struct {
  112. XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ ListMultipartUploadsResult"`
  113. s3.ListMultipartUploadsOutput
  114. }
  115. func (s3a *S3ApiServer) listMultipartUploads(ctx context.Context, input *s3.ListMultipartUploadsInput) (output *ListMultipartUploadsResult, code ErrorCode) {
  116. output = &ListMultipartUploadsResult{
  117. ListMultipartUploadsOutput: s3.ListMultipartUploadsOutput{
  118. Bucket: input.Bucket,
  119. Delimiter: input.Delimiter,
  120. EncodingType: input.EncodingType,
  121. KeyMarker: input.KeyMarker,
  122. MaxUploads: input.MaxUploads,
  123. Prefix: input.Prefix,
  124. },
  125. }
  126. entries, err := s3a.list(ctx, s3a.genUploadsFolder(*input.Bucket), *input.Prefix, *input.KeyMarker, true, int(*input.MaxUploads))
  127. if err != nil {
  128. glog.Errorf("listMultipartUploads %s error: %v", *input.Bucket, err)
  129. return
  130. }
  131. for _, entry := range entries {
  132. if entry.Extended != nil {
  133. key := entry.Extended["key"]
  134. output.Uploads = append(output.Uploads, &s3.MultipartUpload{
  135. Key: aws.String(string(key)),
  136. UploadId: aws.String(entry.Name),
  137. })
  138. }
  139. }
  140. return
  141. }
  142. type ListPartsResult struct {
  143. XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ ListPartsResult"`
  144. s3.ListPartsOutput
  145. }
  146. func (s3a *S3ApiServer) listObjectParts(ctx context.Context, input *s3.ListPartsInput) (output *ListPartsResult, code ErrorCode) {
  147. output = &ListPartsResult{
  148. ListPartsOutput: s3.ListPartsOutput{
  149. Bucket: input.Bucket,
  150. Key: input.Key,
  151. UploadId: input.UploadId,
  152. MaxParts: input.MaxParts, // the maximum number of parts to return.
  153. PartNumberMarker: input.PartNumberMarker, // the part number starts after this, exclusive
  154. },
  155. }
  156. entries, err := s3a.list(ctx, s3a.genUploadsFolder(*input.Bucket)+"/"+*input.UploadId,
  157. "", fmt.Sprintf("%04d.part", *input.PartNumberMarker), false, int(*input.MaxParts))
  158. if err != nil {
  159. glog.Errorf("listObjectParts %s %s error: %v", *input.Bucket, *input.UploadId, err)
  160. return nil, ErrNoSuchUpload
  161. }
  162. for _, entry := range entries {
  163. if strings.HasSuffix(entry.Name, ".part") && !entry.IsDirectory {
  164. partNumberString := entry.Name[:len(entry.Name)-len(".part")]
  165. partNumber, err := strconv.Atoi(partNumberString)
  166. if err != nil {
  167. glog.Errorf("listObjectParts %s %s parse %s: %v", *input.Bucket, *input.UploadId, entry.Name, err)
  168. continue
  169. }
  170. output.Parts = append(output.Parts, &s3.Part{
  171. PartNumber: aws.Int64(int64(partNumber)),
  172. LastModified: aws.Time(time.Unix(entry.Attributes.Mtime, 0)),
  173. Size: aws.Int64(int64(filer2.TotalSize(entry.Chunks))),
  174. ETag: aws.String("\"" + filer2.ETag(entry.Chunks) + "\""),
  175. })
  176. }
  177. }
  178. return
  179. }