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.

173 lines
5.3 KiB

6 years ago
6 years ago
6 years ago
  1. package s3api
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "github.com/aws/aws-sdk-go/aws"
  9. "github.com/aws/aws-sdk-go/service/s3"
  10. "github.com/chrislusf/seaweedfs/weed/filer2"
  11. "github.com/chrislusf/seaweedfs/weed/glog"
  12. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  13. "github.com/satori/go.uuid"
  14. )
  15. func (s3a *S3ApiServer) createMultipartUpload(input *s3.CreateMultipartUploadInput) (output *s3.CreateMultipartUploadOutput, code ErrorCode) {
  16. uploadId, _ := uuid.NewV4()
  17. uploadIdString := uploadId.String()
  18. if err := s3a.mkdir(s3a.genUploadsFolder(*input.Bucket), uploadIdString, func(entry *filer_pb.Entry) {
  19. if entry.Extended == nil {
  20. entry.Extended = make(map[string][]byte)
  21. }
  22. entry.Extended["key"] = []byte(*input.Key)
  23. }); err != nil {
  24. glog.Errorf("NewMultipartUpload error: %v", err)
  25. return nil, ErrInternalError
  26. }
  27. output = &s3.CreateMultipartUploadOutput{
  28. Bucket: input.Bucket,
  29. Key: input.Key,
  30. UploadId: aws.String(uploadIdString),
  31. }
  32. return
  33. }
  34. func (s3a *S3ApiServer) completeMultipartUpload(input *s3.CompleteMultipartUploadInput) (output *s3.CompleteMultipartUploadOutput, code ErrorCode) {
  35. uploadDirectory := s3a.genUploadsFolder(*input.Bucket) + "/" + *input.UploadId
  36. entries, err := s3a.list(uploadDirectory, "", "", false, 0)
  37. if err != nil {
  38. glog.Errorf("completeMultipartUpload %s %s error: %v", *input.Bucket, *input.UploadId, err)
  39. return nil, ErrNoSuchUpload
  40. }
  41. var finalParts []*filer_pb.FileChunk
  42. var offset int64
  43. for _, entry := range entries {
  44. if strings.HasSuffix(entry.Name, ".part") && !entry.IsDirectory {
  45. for _, chunk := range entry.Chunks {
  46. finalParts = append(finalParts, &filer_pb.FileChunk{
  47. FileId: chunk.FileId,
  48. Offset: offset,
  49. Size: chunk.Size,
  50. Mtime: chunk.Mtime,
  51. ETag: chunk.ETag,
  52. })
  53. offset += int64(chunk.Size)
  54. }
  55. }
  56. }
  57. entryName := filepath.Base(*input.Key)
  58. dirName := filepath.Dir(*input.Key)
  59. if dirName == "." {
  60. dirName = ""
  61. }
  62. dirName = fmt.Sprintf("%s/%s/%s", s3a.option.BucketsPath, *input.Bucket, dirName)
  63. err = s3a.mkFile(dirName, entryName, finalParts)
  64. if err != nil {
  65. glog.Errorf("completeMultipartUpload %s/%s error: %v", dirName, entryName, err)
  66. return nil, ErrInternalError
  67. }
  68. output = &s3.CompleteMultipartUploadOutput{
  69. Bucket: input.Bucket,
  70. ETag: aws.String("\"" + filer2.ETag(finalParts) + "\""),
  71. Key: input.Key,
  72. }
  73. return
  74. }
  75. func (s3a *S3ApiServer) abortMultipartUpload(input *s3.AbortMultipartUploadInput) (output *s3.AbortMultipartUploadOutput, code ErrorCode) {
  76. exists, err := s3a.exists(s3a.genUploadsFolder(*input.Bucket), *input.UploadId, true)
  77. if err != nil {
  78. glog.V(1).Infof("bucket %s abort upload %s: %v", *input.Bucket, *input.UploadId, err)
  79. return nil, ErrNoSuchUpload
  80. }
  81. if exists {
  82. err = s3a.rm(s3a.genUploadsFolder(*input.Bucket), *input.UploadId, true, true, true)
  83. }
  84. if err != nil {
  85. glog.V(1).Infof("bucket %s remove upload %s: %v", *input.Bucket, *input.UploadId, err)
  86. return nil, ErrInternalError
  87. }
  88. return &s3.AbortMultipartUploadOutput{}, ErrNone
  89. }
  90. func (s3a *S3ApiServer) listMultipartUploads(input *s3.ListMultipartUploadsInput) (output *s3.ListMultipartUploadsOutput, code ErrorCode) {
  91. output = &s3.ListMultipartUploadsOutput{
  92. Bucket: input.Bucket,
  93. Delimiter: input.Delimiter,
  94. EncodingType: input.EncodingType,
  95. KeyMarker: input.KeyMarker,
  96. MaxUploads: input.MaxUploads,
  97. Prefix: input.Prefix,
  98. }
  99. entries, err := s3a.list(s3a.genUploadsFolder(*input.Bucket), *input.Prefix, *input.KeyMarker, true, int(*input.MaxUploads))
  100. if err != nil {
  101. glog.Errorf("listMultipartUploads %s error: %v", *input.Bucket, err)
  102. return
  103. }
  104. for _, entry := range entries {
  105. if entry.Extended != nil {
  106. key := entry.Extended["key"]
  107. output.Uploads = append(output.Uploads, &s3.MultipartUpload{
  108. Key: aws.String(string(key)),
  109. UploadId: aws.String(entry.Name),
  110. })
  111. }
  112. }
  113. return
  114. }
  115. func (s3a *S3ApiServer) listObjectParts(input *s3.ListPartsInput) (output *s3.ListPartsOutput, code ErrorCode) {
  116. output = &s3.ListPartsOutput{
  117. Bucket: input.Bucket,
  118. Key: input.Key,
  119. UploadId: input.UploadId,
  120. MaxParts: input.MaxParts, // the maximum number of parts to return.
  121. PartNumberMarker: input.PartNumberMarker, // the part number starts after this, exclusive
  122. }
  123. entries, err := s3a.list(s3a.genUploadsFolder(*input.Bucket)+"/"+*input.UploadId,
  124. "", fmt.Sprintf("%04d.part", *input.PartNumberMarker), false, int(*input.MaxParts))
  125. if err != nil {
  126. glog.Errorf("listObjectParts %s %s error: %v", *input.Bucket, *input.UploadId, err)
  127. return nil, ErrNoSuchUpload
  128. }
  129. for _, entry := range entries {
  130. if strings.HasSuffix(entry.Name, ".part") && !entry.IsDirectory {
  131. partNumberString := entry.Name[:len(entry.Name)-len(".part")]
  132. partNumber, err := strconv.Atoi(partNumberString)
  133. if err != nil {
  134. glog.Errorf("listObjectParts %s %s parse %s: %v", *input.Bucket, *input.UploadId, entry.Name, err)
  135. continue
  136. }
  137. output.Parts = append(output.Parts, &s3.Part{
  138. PartNumber: aws.Int64(int64(partNumber)),
  139. LastModified: aws.Time(time.Unix(entry.Attributes.Mtime, 0)),
  140. Size: aws.Int64(int64(filer2.TotalSize(entry.Chunks))),
  141. ETag: aws.String("\"" + filer2.ETag(entry.Chunks) + "\""),
  142. })
  143. }
  144. }
  145. return
  146. }