diff --git a/cmd/uplink/cmd/rb.go b/cmd/uplink/cmd/rb.go index 20514cb68..d2efddbbe 100644 --- a/cmd/uplink/cmd/rb.go +++ b/cmd/uplink/cmd/rb.go @@ -9,18 +9,25 @@ import ( "github.com/spf13/cobra" "storj.io/common/fpath" + "storj.io/uplink" +) + +var ( + rbForceFlag *bool ) func init() { - addCmd(&cobra.Command{ + rbCmd := addCmd(&cobra.Command{ Use: "rb sj://BUCKET", Short: "Remove an empty bucket", RunE: deleteBucket, Args: cobra.ExactArgs(1), }, RootCmd) + rbForceFlag = rbCmd.Flags().Bool("force", false, "if true, empties the bucket of objects first") + setBasicFlags(rbCmd.Flags(), "force") } -func deleteBucket(cmd *cobra.Command, args []string) error { +func deleteBucket(cmd *cobra.Command, args []string) (err error) { ctx, _ := withTelemetry(cmd) if len(args) == 0 { @@ -40,17 +47,48 @@ func deleteBucket(cmd *cobra.Command, args []string) error { return fmt.Errorf("nested buckets not supported, use format sj://bucket/") } - project, err := cfg.getProject(ctx, false) + project, err := cfg.getProject(ctx, true) if err != nil { return convertError(err, dst) } defer closeProject(project) + counter := 0 + defer func() { + // print number of deleted files no mater if we end with error or without + if counter > 0 { + fmt.Printf("Files (%d) from bucket %s deleted\n", counter, dst.Bucket()) + } + if err == nil { + fmt.Printf("Bucket %s deleted\n", dst.Bucket()) + } + }() + + if *rbForceFlag { + objects := project.ListObjects(ctx, dst.Bucket(), &uplink.ListObjectsOptions{ + Recursive: true, + }) + + for objects.Next() { + object := objects.Item() + path := object.Key + _, err := project.DeleteObject(ctx, dst.Bucket(), path) + if err != nil { + return fmt.Errorf("failed to delete encrypted object, cannot empty bucket: %q", path) + } + counter++ + if counter%10 == 0 { + fmt.Printf("Files (%d) from bucket %s deleted\n", counter, dst.Bucket()) + } + } + if err := objects.Err(); err != nil { + return err + } + } + if _, err := project.DeleteBucket(ctx, dst.Bucket()); err != nil { return convertError(err, dst) } - fmt.Printf("Bucket %s deleted\n", dst.Bucket()) - return nil } diff --git a/scripts/test-uplink.sh b/scripts/test-uplink.sh index 102775e8a..c486bb848 100755 --- a/scripts/test-uplink.sh +++ b/scripts/test-uplink.sh @@ -100,3 +100,10 @@ compare_files "$SRC_DIR/multisegment-upload-testfile" "$DST_DIR/multisegment-upl compare_files "$SRC_DIR/diff-size-segments" "$DST_DIR/diff-size-segments" compare_files "$SRC_DIR/put-file" "$DST_DIR/put-file" compare_files "$SRC_DIR/put-file" "$DST_DIR/put-file-from-cat" + +# test deleting non empty bucket with --force flag +uplink mb "sj://$BUCKET/" + +uplink cp "$SRC_DIR/small-upload-testfile" "sj://$BUCKET/" --progress=false + +uplink rb "sj://$BUCKET" --force \ No newline at end of file