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.

459 lines
16 KiB

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