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.

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