command_s3_clean_uploads.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package shell
  2. import (
  3. "flag"
  4. "fmt"
  5. "io"
  6. "math"
  7. "time"
  8. "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
  9. "github.com/seaweedfs/seaweedfs/weed/security"
  10. "github.com/seaweedfs/seaweedfs/weed/util"
  11. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  12. )
  13. func init() {
  14. Commands = append(Commands, &commandS3CleanUploads{})
  15. }
  16. type commandS3CleanUploads struct{}
  17. func (c *commandS3CleanUploads) Name() string {
  18. return "s3.clean.uploads"
  19. }
  20. func (c *commandS3CleanUploads) Help() string {
  21. return `clean up stale multipart uploads
  22. Example:
  23. s3.clean.uploads -timeAgo 1.5h
  24. `
  25. }
  26. func (c *commandS3CleanUploads) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  27. bucketCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  28. uploadedTimeAgo := bucketCommand.Duration("timeAgo", 24*time.Hour, "created time before now. \"1.5h\" or \"2h45m\". Valid time units are \"m\", \"h\"")
  29. if err = bucketCommand.Parse(args); err != nil {
  30. return nil
  31. }
  32. signingKey := util.GetViper().GetString("jwt.filer_signing.key")
  33. var filerBucketsPath string
  34. filerBucketsPath, err = readFilerBucketsPath(commandEnv)
  35. if err != nil {
  36. return fmt.Errorf("read buckets: %v", err)
  37. }
  38. var buckets []string
  39. err = filer_pb.List(commandEnv, filerBucketsPath, "", func(entry *filer_pb.Entry, isLast bool) error {
  40. buckets = append(buckets, entry.Name)
  41. return nil
  42. }, "", false, math.MaxUint32)
  43. if err != nil {
  44. return fmt.Errorf("list buckets under %v: %v", filerBucketsPath, err)
  45. }
  46. for _, bucket := range buckets {
  47. if err := c.cleanupUploads(commandEnv, writer, filerBucketsPath, bucket, *uploadedTimeAgo, signingKey); err != nil {
  48. fmt.Fprintf(writer, fmt.Sprintf("failed cleanup uploads for bucket %s: %v", bucket, err))
  49. }
  50. }
  51. return err
  52. }
  53. func (c *commandS3CleanUploads) cleanupUploads(commandEnv *CommandEnv, writer io.Writer, filerBucketsPath string, bucket string, timeAgo time.Duration, signingKey string) error {
  54. uploadsDir := filerBucketsPath + "/" + bucket + "/" + s3_constants.MultipartUploadsFolder
  55. var staleUploads []string
  56. now := time.Now()
  57. err := filer_pb.List(commandEnv, uploadsDir, "", func(entry *filer_pb.Entry, isLast bool) error {
  58. ctime := time.Unix(entry.Attributes.Crtime, 0)
  59. if ctime.Add(timeAgo).Before(now) {
  60. staleUploads = append(staleUploads, entry.Name)
  61. }
  62. return nil
  63. }, "", false, math.MaxUint32)
  64. if err != nil {
  65. return fmt.Errorf("list uploads under %v: %v", uploadsDir, err)
  66. }
  67. var encodedJwt security.EncodedJwt
  68. if signingKey != "" {
  69. encodedJwt = security.GenJwtForFilerServer(security.SigningKey(signingKey), 15*60)
  70. }
  71. for _, staleUpload := range staleUploads {
  72. deleteUrl := fmt.Sprintf("http://%s%s/%s?recursive=true&ignoreRecursiveError=true", commandEnv.option.FilerAddress.ToHttpAddress(), uploadsDir, staleUpload)
  73. fmt.Fprintf(writer, "purge %s\n", deleteUrl)
  74. err = util.Delete(deleteUrl, string(encodedJwt))
  75. if err != nil && err.Error() != "" {
  76. return fmt.Errorf("purge %s/%s: %v", uploadsDir, staleUpload, err)
  77. }
  78. }
  79. return nil
  80. }