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.

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