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.

285 lines
9.5 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
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. 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.ToHttpAddress(), 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. }