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.

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