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.

250 lines
8.5 KiB

4 years ago
6 years ago
6 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
6 years ago
6 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package s3api
  2. import (
  3. "encoding/xml"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/s3api/s3err"
  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/google/uuid"
  13. "github.com/chrislusf/seaweedfs/weed/filer"
  14. "github.com/chrislusf/seaweedfs/weed/glog"
  15. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  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(input *s3.CreateMultipartUploadInput) (output *InitiateMultipartUploadResult, code s3err.ErrorCode) {
  22. uploadId, _ := uuid.NewRandom()
  23. uploadIdString := uploadId.String()
  24. if err := s3a.mkdir(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, s3err.ErrInternalError
  32. }
  33. output = &InitiateMultipartUploadResult{
  34. CreateMultipartUploadOutput: s3.CreateMultipartUploadOutput{
  35. Bucket: input.Bucket,
  36. Key: objectKey(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(input *s3.CompleteMultipartUploadInput) (output *CompleteMultipartUploadResult, code s3err.ErrorCode) {
  47. uploadDirectory := s3a.genUploadsFolder(*input.Bucket) + "/" + *input.UploadId
  48. entries, _, err := s3a.list(uploadDirectory, "", "", false, 0)
  49. if err != nil || len(entries) == 0 {
  50. glog.Errorf("completeMultipartUpload %s %s error: %v, entries:%d", *input.Bucket, *input.UploadId, err, len(entries))
  51. return nil, s3err.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.GetFileIdString(),
  60. Offset: offset,
  61. Size: chunk.Size,
  62. Mtime: chunk.Mtime,
  63. CipherKey: chunk.CipherKey,
  64. ETag: chunk.ETag,
  65. }
  66. finalParts = append(finalParts, p)
  67. offset += int64(chunk.Size)
  68. }
  69. }
  70. }
  71. entryName := filepath.Base(*input.Key)
  72. dirName := filepath.Dir(*input.Key)
  73. if dirName == "." {
  74. dirName = ""
  75. }
  76. if strings.HasPrefix(dirName, "/") {
  77. dirName = dirName[1:]
  78. }
  79. dirName = fmt.Sprintf("%s/%s/%s", s3a.option.BucketsPath, *input.Bucket, dirName)
  80. // remove suffix '/'
  81. if strings.HasSuffix(dirName, "/") {
  82. dirName = dirName[:len(dirName)-1]
  83. }
  84. err = s3a.mkFile(dirName, entryName, finalParts)
  85. if err != nil {
  86. glog.Errorf("completeMultipartUpload %s/%s error: %v", dirName, entryName, err)
  87. return nil, s3err.ErrInternalError
  88. }
  89. output = &CompleteMultipartUploadResult{
  90. CompleteMultipartUploadOutput: s3.CompleteMultipartUploadOutput{
  91. Location: aws.String(fmt.Sprintf("http://%s%s/%s", s3a.option.Filer, dirName, entryName)),
  92. Bucket: input.Bucket,
  93. ETag: aws.String("\"" + filer.ETagChunks(finalParts) + "\""),
  94. Key: objectKey(input.Key),
  95. },
  96. }
  97. if err = s3a.rm(s3a.genUploadsFolder(*input.Bucket), *input.UploadId, false, true); err != nil {
  98. glog.V(1).Infof("completeMultipartUpload cleanup %s upload %s: %v", *input.Bucket, *input.UploadId, err)
  99. }
  100. return
  101. }
  102. func (s3a *S3ApiServer) abortMultipartUpload(input *s3.AbortMultipartUploadInput) (output *s3.AbortMultipartUploadOutput, code s3err.ErrorCode) {
  103. exists, err := s3a.exists(s3a.genUploadsFolder(*input.Bucket), *input.UploadId, true)
  104. if err != nil {
  105. glog.V(1).Infof("bucket %s abort upload %s: %v", *input.Bucket, *input.UploadId, err)
  106. return nil, s3err.ErrNoSuchUpload
  107. }
  108. if exists {
  109. err = s3a.rm(s3a.genUploadsFolder(*input.Bucket), *input.UploadId, true, true)
  110. }
  111. if err != nil {
  112. glog.V(1).Infof("bucket %s remove upload %s: %v", *input.Bucket, *input.UploadId, err)
  113. return nil, s3err.ErrInternalError
  114. }
  115. return &s3.AbortMultipartUploadOutput{}, s3err.ErrNone
  116. }
  117. type ListMultipartUploadsResult struct {
  118. XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ ListMultipartUploadsResult"`
  119. // copied from s3.ListMultipartUploadsOutput, the Uploads is not converting to <Upload></Upload>
  120. Bucket *string `type:"string"`
  121. Delimiter *string `type:"string"`
  122. EncodingType *string `type:"string" enum:"EncodingType"`
  123. IsTruncated *bool `type:"boolean"`
  124. KeyMarker *string `type:"string"`
  125. MaxUploads *int64 `type:"integer"`
  126. NextKeyMarker *string `type:"string"`
  127. NextUploadIdMarker *string `type:"string"`
  128. Prefix *string `type:"string"`
  129. UploadIdMarker *string `type:"string"`
  130. Upload []*s3.MultipartUpload `locationName:"Upload" type:"list" flattened:"true"`
  131. }
  132. func (s3a *S3ApiServer) listMultipartUploads(input *s3.ListMultipartUploadsInput) (output *ListMultipartUploadsResult, code s3err.ErrorCode) {
  133. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListMultipartUploads.html
  134. output = &ListMultipartUploadsResult{
  135. Bucket: input.Bucket,
  136. Delimiter: input.Delimiter,
  137. EncodingType: input.EncodingType,
  138. KeyMarker: input.KeyMarker,
  139. MaxUploads: input.MaxUploads,
  140. Prefix: input.Prefix,
  141. }
  142. entries, isLast, err := s3a.list(s3a.genUploadsFolder(*input.Bucket), *input.Prefix, *input.KeyMarker, true, uint32(*input.MaxUploads))
  143. if err != nil {
  144. glog.Errorf("listMultipartUploads %s error: %v", *input.Bucket, err)
  145. return
  146. }
  147. output.IsTruncated = aws.Bool(!isLast)
  148. for _, entry := range entries {
  149. if entry.Extended != nil {
  150. key := entry.Extended["key"]
  151. output.Upload = append(output.Upload, &s3.MultipartUpload{
  152. Key: objectKey(aws.String(string(key))),
  153. UploadId: aws.String(entry.Name),
  154. })
  155. if !isLast {
  156. output.NextUploadIdMarker = aws.String(entry.Name)
  157. }
  158. }
  159. }
  160. return
  161. }
  162. type ListPartsResult struct {
  163. XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ ListPartsResult"`
  164. // copied from s3.ListPartsOutput, the Parts is not converting to <Part></Part>
  165. Bucket *string `type:"string"`
  166. IsTruncated *bool `type:"boolean"`
  167. Key *string `min:"1" type:"string"`
  168. MaxParts *int64 `type:"integer"`
  169. NextPartNumberMarker *int64 `type:"integer"`
  170. PartNumberMarker *int64 `type:"integer"`
  171. Part []*s3.Part `locationName:"Part" type:"list" flattened:"true"`
  172. StorageClass *string `type:"string" enum:"StorageClass"`
  173. UploadId *string `type:"string"`
  174. }
  175. func (s3a *S3ApiServer) listObjectParts(input *s3.ListPartsInput) (output *ListPartsResult, code s3err.ErrorCode) {
  176. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListParts.html
  177. output = &ListPartsResult{
  178. Bucket: input.Bucket,
  179. Key: objectKey(input.Key),
  180. UploadId: input.UploadId,
  181. MaxParts: input.MaxParts, // the maximum number of parts to return.
  182. PartNumberMarker: input.PartNumberMarker, // the part number starts after this, exclusive
  183. StorageClass: aws.String("STANDARD"),
  184. }
  185. entries, isLast, err := s3a.list(s3a.genUploadsFolder(*input.Bucket)+"/"+*input.UploadId, "", fmt.Sprintf("%04d.part", *input.PartNumberMarker), false, uint32(*input.MaxParts))
  186. if err != nil {
  187. glog.Errorf("listObjectParts %s %s error: %v", *input.Bucket, *input.UploadId, err)
  188. return nil, s3err.ErrNoSuchUpload
  189. }
  190. output.IsTruncated = aws.Bool(!isLast)
  191. for _, entry := range entries {
  192. if strings.HasSuffix(entry.Name, ".part") && !entry.IsDirectory {
  193. partNumberString := entry.Name[:len(entry.Name)-len(".part")]
  194. partNumber, err := strconv.Atoi(partNumberString)
  195. if err != nil {
  196. glog.Errorf("listObjectParts %s %s parse %s: %v", *input.Bucket, *input.UploadId, entry.Name, err)
  197. continue
  198. }
  199. output.Part = append(output.Part, &s3.Part{
  200. PartNumber: aws.Int64(int64(partNumber)),
  201. LastModified: aws.Time(time.Unix(entry.Attributes.Mtime, 0).UTC()),
  202. Size: aws.Int64(int64(filer.FileSize(entry))),
  203. ETag: aws.String("\"" + filer.ETag(entry) + "\""),
  204. })
  205. if !isLast {
  206. output.NextPartNumberMarker = aws.Int64(int64(partNumber))
  207. }
  208. }
  209. }
  210. return
  211. }