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.

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