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.

211 lines
6.4 KiB

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