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.

217 lines
6.7 KiB

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