filer_multipart.go 9.5 KB

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