filer_multipart.go 10.0 KB

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