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.

301 lines
10 KiB

4 years ago
6 years ago
6 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 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. 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. if len(entries) == 0 {
  231. glog.Errorf("listObjectParts %s %s not found", *input.Bucket, *input.UploadId)
  232. return nil, s3err.ErrNoSuchUpload
  233. }
  234. output.IsTruncated = aws.Bool(!isLast)
  235. for _, entry := range entries {
  236. if strings.HasSuffix(entry.Name, ".part") && !entry.IsDirectory {
  237. partNumberString := entry.Name[:len(entry.Name)-len(".part")]
  238. partNumber, err := strconv.Atoi(partNumberString)
  239. if err != nil {
  240. glog.Errorf("listObjectParts %s %s parse %s: %v", *input.Bucket, *input.UploadId, entry.Name, err)
  241. continue
  242. }
  243. output.Part = append(output.Part, &s3.Part{
  244. PartNumber: aws.Int64(int64(partNumber)),
  245. LastModified: aws.Time(time.Unix(entry.Attributes.Mtime, 0).UTC()),
  246. Size: aws.Int64(int64(filer.FileSize(entry))),
  247. ETag: aws.String("\"" + filer.ETag(entry) + "\""),
  248. })
  249. if !isLast {
  250. output.NextPartNumberMarker = aws.Int64(int64(partNumber))
  251. }
  252. }
  253. }
  254. return
  255. }