filer_multipart.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. package s3api
  2. import (
  3. "cmp"
  4. "encoding/hex"
  5. "encoding/xml"
  6. "fmt"
  7. "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
  8. "github.com/seaweedfs/seaweedfs/weed/stats"
  9. "golang.org/x/exp/slices"
  10. "math"
  11. "path/filepath"
  12. "sort"
  13. "strconv"
  14. "strings"
  15. "time"
  16. "github.com/aws/aws-sdk-go/aws"
  17. "github.com/aws/aws-sdk-go/service/s3"
  18. "github.com/google/uuid"
  19. "github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
  20. "github.com/seaweedfs/seaweedfs/weed/filer"
  21. "github.com/seaweedfs/seaweedfs/weed/glog"
  22. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  23. )
  24. const (
  25. multipartExt = ".part"
  26. multiPartMinSize = 5 * 1024 * 1024
  27. )
  28. type InitiateMultipartUploadResult struct {
  29. XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ InitiateMultipartUploadResult"`
  30. s3.CreateMultipartUploadOutput
  31. }
  32. func (s3a *S3ApiServer) createMultipartUpload(input *s3.CreateMultipartUploadInput) (output *InitiateMultipartUploadResult, code s3err.ErrorCode) {
  33. glog.V(2).Infof("createMultipartUpload input %v", input)
  34. uploadIdString := s3a.generateUploadID(*input.Key)
  35. uploadIdString = uploadIdString + "_" + strings.ReplaceAll(uuid.New().String(), "-", "")
  36. if err := s3a.mkdir(s3a.genUploadsFolder(*input.Bucket), uploadIdString, func(entry *filer_pb.Entry) {
  37. if entry.Extended == nil {
  38. entry.Extended = make(map[string][]byte)
  39. }
  40. entry.Extended["key"] = []byte(*input.Key)
  41. for k, v := range input.Metadata {
  42. entry.Extended[k] = []byte(*v)
  43. }
  44. if input.ContentType != nil {
  45. entry.Attributes.Mime = *input.ContentType
  46. }
  47. }); err != nil {
  48. glog.Errorf("NewMultipartUpload error: %v", err)
  49. return nil, s3err.ErrInternalError
  50. }
  51. output = &InitiateMultipartUploadResult{
  52. CreateMultipartUploadOutput: s3.CreateMultipartUploadOutput{
  53. Bucket: input.Bucket,
  54. Key: objectKey(input.Key),
  55. UploadId: aws.String(uploadIdString),
  56. },
  57. }
  58. return
  59. }
  60. type CompleteMultipartUploadResult struct {
  61. XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ CompleteMultipartUploadResult"`
  62. s3.CompleteMultipartUploadOutput
  63. }
  64. func (s3a *S3ApiServer) completeMultipartUpload(input *s3.CompleteMultipartUploadInput, parts *CompleteMultipartUpload) (output *CompleteMultipartUploadResult, code s3err.ErrorCode) {
  65. glog.V(2).Infof("completeMultipartUpload input %v", input)
  66. if len(parts.Parts) == 0 {
  67. stats.S3HandlerCounter.WithLabelValues(stats.ErrorCompletedNoSuchUpload).Inc()
  68. return nil, s3err.ErrNoSuchUpload
  69. }
  70. completedPartNumbers := []int{}
  71. completedPartMap := make(map[int][]string)
  72. for _, part := range parts.Parts {
  73. if _, ok := completedPartMap[part.PartNumber]; !ok {
  74. completedPartNumbers = append(completedPartNumbers, part.PartNumber)
  75. }
  76. completedPartMap[part.PartNumber] = append(completedPartMap[part.PartNumber], part.ETag)
  77. }
  78. sort.Ints(completedPartNumbers)
  79. uploadDirectory := s3a.genUploadsFolder(*input.Bucket) + "/" + *input.UploadId
  80. entries, _, err := s3a.list(uploadDirectory, "", "", false, maxPartsList)
  81. if err != nil {
  82. glog.Errorf("completeMultipartUpload %s %s error: %v, entries:%d", *input.Bucket, *input.UploadId, err, len(entries))
  83. stats.S3HandlerCounter.WithLabelValues(stats.ErrorCompletedNoSuchUpload).Inc()
  84. return nil, s3err.ErrNoSuchUpload
  85. }
  86. if len(entries) == 0 {
  87. entryName, dirName := s3a.getEntryNameAndDir(input)
  88. if entry, _ := s3a.getEntry(dirName, entryName); entry != nil && entry.Extended != nil {
  89. if uploadId, ok := entry.Extended[s3_constants.SeaweedFSUploadId]; ok && *input.UploadId == string(uploadId) {
  90. return &CompleteMultipartUploadResult{
  91. CompleteMultipartUploadOutput: s3.CompleteMultipartUploadOutput{
  92. Location: aws.String(fmt.Sprintf("http://%s%s/%s", s3a.option.Filer.ToHttpAddress(), urlEscapeObject(dirName), urlPathEscape(entryName))),
  93. Bucket: input.Bucket,
  94. ETag: aws.String("\"" + filer.ETagChunks(entry.GetChunks()) + "\""),
  95. Key: objectKey(input.Key),
  96. },
  97. }, s3err.ErrNone
  98. }
  99. }
  100. stats.S3HandlerCounter.WithLabelValues(stats.ErrorCompletedNoSuchUpload).Inc()
  101. return nil, s3err.ErrNoSuchUpload
  102. }
  103. pentry, err := s3a.getEntry(s3a.genUploadsFolder(*input.Bucket), *input.UploadId)
  104. if err != nil {
  105. glog.Errorf("completeMultipartUpload %s %s error: %v", *input.Bucket, *input.UploadId, err)
  106. stats.S3HandlerCounter.WithLabelValues(stats.ErrorCompletedNoSuchUpload).Inc()
  107. return nil, s3err.ErrNoSuchUpload
  108. }
  109. deleteEntries := []*filer_pb.Entry{}
  110. partEntries := make(map[int][]*filer_pb.Entry, len(entries))
  111. entityTooSmall := false
  112. for _, entry := range entries {
  113. foundEntry := false
  114. glog.V(4).Infof("completeMultipartUpload part entries %s", entry.Name)
  115. if entry.IsDirectory || !strings.HasSuffix(entry.Name, multipartExt) {
  116. continue
  117. }
  118. partNumber, err := parsePartNumber(entry.Name)
  119. if err != nil {
  120. stats.S3HandlerCounter.WithLabelValues(stats.ErrorCompletedPartNumber).Inc()
  121. glog.Errorf("completeMultipartUpload failed to pasre partNumber %s:%s", entry.Name, err)
  122. continue
  123. }
  124. completedPartsByNumber, ok := completedPartMap[partNumber]
  125. if !ok {
  126. continue
  127. }
  128. for _, partETag := range completedPartsByNumber {
  129. partETag = strings.Trim(partETag, `"`)
  130. entryETag := hex.EncodeToString(entry.Attributes.GetMd5())
  131. if partETag != "" && len(partETag) == 32 && entryETag != "" {
  132. if entryETag != partETag {
  133. glog.Errorf("completeMultipartUpload %s ETag mismatch chunk: %s part: %s", entry.Name, entryETag, partETag)
  134. stats.S3HandlerCounter.WithLabelValues(stats.ErrorCompletedEtagMismatch).Inc()
  135. continue
  136. }
  137. } else {
  138. glog.Warningf("invalid complete etag %s, partEtag %s", partETag, entryETag)
  139. stats.S3HandlerCounter.WithLabelValues(stats.ErrorCompletedEtagInvalid).Inc()
  140. }
  141. if len(entry.Chunks) == 0 {
  142. glog.Warningf("completeMultipartUpload %s empty chunks", entry.Name)
  143. stats.S3HandlerCounter.WithLabelValues(stats.ErrorCompletedPartEmpty).Inc()
  144. continue
  145. }
  146. //there maybe multi same part, because of client retry
  147. partEntries[partNumber] = append(partEntries[partNumber], entry)
  148. foundEntry = true
  149. }
  150. if foundEntry {
  151. if len(completedPartNumbers) > 1 && partNumber != completedPartNumbers[len(completedPartNumbers)-1] &&
  152. entry.Attributes.FileSize < multiPartMinSize {
  153. glog.Warningf("completeMultipartUpload %s part file size less 5mb", entry.Name)
  154. entityTooSmall = true
  155. }
  156. } else {
  157. deleteEntries = append(deleteEntries, entry)
  158. }
  159. }
  160. if entityTooSmall {
  161. stats.S3HandlerCounter.WithLabelValues(stats.ErrorCompleteEntityTooSmall).Inc()
  162. return nil, s3err.ErrEntityTooSmall
  163. }
  164. mime := pentry.Attributes.Mime
  165. var finalParts []*filer_pb.FileChunk
  166. var offset int64
  167. for _, partNumber := range completedPartNumbers {
  168. partEntriesByNumber, ok := partEntries[partNumber]
  169. if !ok {
  170. glog.Errorf("part %d has no entry", partNumber)
  171. stats.S3HandlerCounter.WithLabelValues(stats.ErrorCompletedPartNotFound).Inc()
  172. return nil, s3err.ErrInvalidPart
  173. }
  174. found := false
  175. if len(partEntriesByNumber) > 1 {
  176. slices.SortFunc(partEntriesByNumber, func(a, b *filer_pb.Entry) int {
  177. return cmp.Compare(b.Chunks[0].ModifiedTsNs, a.Chunks[0].ModifiedTsNs)
  178. })
  179. }
  180. for _, entry := range partEntriesByNumber {
  181. if found {
  182. deleteEntries = append(deleteEntries, entry)
  183. stats.S3HandlerCounter.WithLabelValues(stats.ErrorCompletedPartEntryMismatch).Inc()
  184. continue
  185. }
  186. for _, chunk := range entry.GetChunks() {
  187. p := &filer_pb.FileChunk{
  188. FileId: chunk.GetFileIdString(),
  189. Offset: offset,
  190. Size: chunk.Size,
  191. ModifiedTsNs: chunk.ModifiedTsNs,
  192. CipherKey: chunk.CipherKey,
  193. ETag: chunk.ETag,
  194. }
  195. finalParts = append(finalParts, p)
  196. offset += int64(chunk.Size)
  197. }
  198. found = true
  199. }
  200. }
  201. entryName, dirName := s3a.getEntryNameAndDir(input)
  202. err = s3a.mkFile(dirName, entryName, finalParts, func(entry *filer_pb.Entry) {
  203. if entry.Extended == nil {
  204. entry.Extended = make(map[string][]byte)
  205. }
  206. entry.Extended[s3_constants.SeaweedFSUploadId] = []byte(*input.UploadId)
  207. for k, v := range pentry.Extended {
  208. if k != "key" {
  209. entry.Extended[k] = v
  210. }
  211. }
  212. if pentry.Attributes.Mime != "" {
  213. entry.Attributes.Mime = pentry.Attributes.Mime
  214. } else if mime != "" {
  215. entry.Attributes.Mime = mime
  216. }
  217. entry.Attributes.FileSize = uint64(offset)
  218. })
  219. if err != nil {
  220. glog.Errorf("completeMultipartUpload %s/%s error: %v", dirName, entryName, err)
  221. return nil, s3err.ErrInternalError
  222. }
  223. output = &CompleteMultipartUploadResult{
  224. CompleteMultipartUploadOutput: s3.CompleteMultipartUploadOutput{
  225. Location: aws.String(fmt.Sprintf("http://%s%s/%s", s3a.option.Filer.ToHttpAddress(), urlEscapeObject(dirName), urlPathEscape(entryName))),
  226. Bucket: input.Bucket,
  227. ETag: aws.String("\"" + filer.ETagChunks(finalParts) + "\""),
  228. Key: objectKey(input.Key),
  229. },
  230. }
  231. for _, deleteEntry := range deleteEntries {
  232. //delete unused part data
  233. glog.Infof("completeMultipartUpload cleanup %s upload %s unused %s", *input.Bucket, *input.UploadId, deleteEntry.Name)
  234. if err = s3a.rm(uploadDirectory, deleteEntry.Name, true, true); err != nil {
  235. glog.Warningf("completeMultipartUpload cleanup %s upload %s unused %s : %v", *input.Bucket, *input.UploadId, deleteEntry.Name, err)
  236. }
  237. }
  238. if err = s3a.rm(s3a.genUploadsFolder(*input.Bucket), *input.UploadId, false, true); err != nil {
  239. glog.V(1).Infof("completeMultipartUpload cleanup %s upload %s: %v", *input.Bucket, *input.UploadId, err)
  240. }
  241. return
  242. }
  243. func (s3a *S3ApiServer) getEntryNameAndDir(input *s3.CompleteMultipartUploadInput) (string, string) {
  244. entryName := filepath.Base(*input.Key)
  245. dirName := filepath.ToSlash(filepath.Dir(*input.Key))
  246. if dirName == "." {
  247. dirName = ""
  248. }
  249. if strings.HasPrefix(dirName, "/") {
  250. dirName = dirName[1:]
  251. }
  252. dirName = fmt.Sprintf("%s/%s/%s", s3a.option.BucketsPath, *input.Bucket, dirName)
  253. // remove suffix '/'
  254. if strings.HasSuffix(dirName, "/") {
  255. dirName = dirName[:len(dirName)-1]
  256. }
  257. return entryName, dirName
  258. }
  259. func parsePartNumber(fileName string) (int, error) {
  260. var partNumberString string
  261. index := strings.Index(fileName, "_")
  262. if index != -1 {
  263. partNumberString = fileName[:index]
  264. } else {
  265. partNumberString = fileName[:len(fileName)-len(multipartExt)]
  266. }
  267. return strconv.Atoi(partNumberString)
  268. }
  269. func (s3a *S3ApiServer) abortMultipartUpload(input *s3.AbortMultipartUploadInput) (output *s3.AbortMultipartUploadOutput, code s3err.ErrorCode) {
  270. glog.V(2).Infof("abortMultipartUpload input %v", input)
  271. exists, err := s3a.exists(s3a.genUploadsFolder(*input.Bucket), *input.UploadId, true)
  272. if err != nil {
  273. glog.V(1).Infof("bucket %s abort upload %s: %v", *input.Bucket, *input.UploadId, err)
  274. return nil, s3err.ErrNoSuchUpload
  275. }
  276. if exists {
  277. err = s3a.rm(s3a.genUploadsFolder(*input.Bucket), *input.UploadId, true, true)
  278. }
  279. if err != nil {
  280. glog.V(1).Infof("bucket %s remove upload %s: %v", *input.Bucket, *input.UploadId, err)
  281. return nil, s3err.ErrInternalError
  282. }
  283. return &s3.AbortMultipartUploadOutput{}, s3err.ErrNone
  284. }
  285. type ListMultipartUploadsResult struct {
  286. XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ ListMultipartUploadsResult"`
  287. // copied from s3.ListMultipartUploadsOutput, the Uploads is not converting to <Upload></Upload>
  288. Bucket *string `type:"string"`
  289. Delimiter *string `type:"string"`
  290. EncodingType *string `type:"string" enum:"EncodingType"`
  291. IsTruncated *bool `type:"boolean"`
  292. KeyMarker *string `type:"string"`
  293. MaxUploads *int64 `type:"integer"`
  294. NextKeyMarker *string `type:"string"`
  295. NextUploadIdMarker *string `type:"string"`
  296. Prefix *string `type:"string"`
  297. UploadIdMarker *string `type:"string"`
  298. Upload []*s3.MultipartUpload `locationName:"Upload" type:"list" flattened:"true"`
  299. }
  300. func (s3a *S3ApiServer) listMultipartUploads(input *s3.ListMultipartUploadsInput) (output *ListMultipartUploadsResult, code s3err.ErrorCode) {
  301. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListMultipartUploads.html
  302. glog.V(2).Infof("listMultipartUploads input %v", input)
  303. output = &ListMultipartUploadsResult{
  304. Bucket: input.Bucket,
  305. Delimiter: input.Delimiter,
  306. EncodingType: input.EncodingType,
  307. KeyMarker: input.KeyMarker,
  308. MaxUploads: input.MaxUploads,
  309. Prefix: input.Prefix,
  310. IsTruncated: aws.Bool(false),
  311. }
  312. entries, _, err := s3a.list(s3a.genUploadsFolder(*input.Bucket), "", *input.UploadIdMarker, false, math.MaxInt32)
  313. if err != nil {
  314. glog.Errorf("listMultipartUploads %s error: %v", *input.Bucket, err)
  315. return
  316. }
  317. uploadsCount := int64(0)
  318. for _, entry := range entries {
  319. if entry.Extended != nil {
  320. key := string(entry.Extended["key"])
  321. if *input.KeyMarker != "" && *input.KeyMarker != key {
  322. continue
  323. }
  324. if *input.Prefix != "" && !strings.HasPrefix(key, *input.Prefix) {
  325. continue
  326. }
  327. output.Upload = append(output.Upload, &s3.MultipartUpload{
  328. Key: objectKey(aws.String(key)),
  329. UploadId: aws.String(entry.Name),
  330. })
  331. uploadsCount += 1
  332. }
  333. if uploadsCount >= *input.MaxUploads {
  334. output.IsTruncated = aws.Bool(true)
  335. output.NextUploadIdMarker = aws.String(entry.Name)
  336. break
  337. }
  338. }
  339. return
  340. }
  341. type ListPartsResult struct {
  342. XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ ListPartsResult"`
  343. // copied from s3.ListPartsOutput, the Parts is not converting to <Part></Part>
  344. Bucket *string `type:"string"`
  345. IsTruncated *bool `type:"boolean"`
  346. Key *string `min:"1" type:"string"`
  347. MaxParts *int64 `type:"integer"`
  348. NextPartNumberMarker *int64 `type:"integer"`
  349. PartNumberMarker *int64 `type:"integer"`
  350. Part []*s3.Part `locationName:"Part" type:"list" flattened:"true"`
  351. StorageClass *string `type:"string" enum:"StorageClass"`
  352. UploadId *string `type:"string"`
  353. }
  354. func (s3a *S3ApiServer) listObjectParts(input *s3.ListPartsInput) (output *ListPartsResult, code s3err.ErrorCode) {
  355. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListParts.html
  356. glog.V(2).Infof("listObjectParts input %v", input)
  357. output = &ListPartsResult{
  358. Bucket: input.Bucket,
  359. Key: objectKey(input.Key),
  360. UploadId: input.UploadId,
  361. MaxParts: input.MaxParts, // the maximum number of parts to return.
  362. PartNumberMarker: input.PartNumberMarker, // the part number starts after this, exclusive
  363. StorageClass: aws.String("STANDARD"),
  364. }
  365. entries, isLast, err := s3a.list(s3a.genUploadsFolder(*input.Bucket)+"/"+*input.UploadId, "", fmt.Sprintf("%04d%s", *input.PartNumberMarker, multipartExt), false, uint32(*input.MaxParts))
  366. if err != nil {
  367. glog.Errorf("listObjectParts %s %s error: %v", *input.Bucket, *input.UploadId, err)
  368. return nil, s3err.ErrNoSuchUpload
  369. }
  370. // Note: The upload directory is sort of a marker of the existence of an multipart upload request.
  371. // So can not just delete empty upload folders.
  372. output.IsTruncated = aws.Bool(!isLast)
  373. for _, entry := range entries {
  374. if strings.HasSuffix(entry.Name, multipartExt) && !entry.IsDirectory {
  375. partNumber, err := parsePartNumber(entry.Name)
  376. if err != nil {
  377. glog.Errorf("listObjectParts %s %s parse %s: %v", *input.Bucket, *input.UploadId, entry.Name, err)
  378. continue
  379. }
  380. output.Part = append(output.Part, &s3.Part{
  381. PartNumber: aws.Int64(int64(partNumber)),
  382. LastModified: aws.Time(time.Unix(entry.Attributes.Mtime, 0).UTC()),
  383. Size: aws.Int64(int64(filer.FileSize(entry))),
  384. ETag: aws.String("\"" + filer.ETag(entry) + "\""),
  385. })
  386. if !isLast {
  387. output.NextPartNumberMarker = aws.Int64(int64(partNumber))
  388. }
  389. }
  390. }
  391. return
  392. }