diff --git a/pkg/client/client.go b/pkg/client/client.go index bda5025e..a472cb31 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -41,6 +41,7 @@ type Client interface { GetObject(offset, length int64) (body io.ReadCloser, size int64, err *probe.Error) PutObject(size int64, data io.Reader) *probe.Error Remove() *probe.Error + RemoveIncompleteUpload() *probe.Error // URL returns back internal url URL() *URL diff --git a/pkg/client/fs/fs.go b/pkg/client/fs/fs.go index 15621086..8f544267 100644 --- a/pkg/client/fs/fs.go +++ b/pkg/client/fs/fs.go @@ -17,6 +17,7 @@ package fs import ( + "errors" "io" "os" "path/filepath" @@ -191,6 +192,11 @@ func (f *fsClient) GetObject(offset, length int64) (io.ReadCloser, int64, *probe return body, length, nil } +// RemoveIncompleteUpload - remove multiparts of an incomplete multi[art upload] +func (f *fsClient) RemoveIncompleteUpload() *probe.Error { + return probe.NewError(errors.New("Call not implemented")) +} + func (f *fsClient) Remove() *probe.Error { err := os.Remove(f.Path) return probe.NewError(err) diff --git a/pkg/client/s3v2/s3v2.go b/pkg/client/s3v2/s3v2.go index 5ec24ce2..2daa9e12 100644 --- a/pkg/client/s3v2/s3v2.go +++ b/pkg/client/s3v2/s3v2.go @@ -88,6 +88,14 @@ func (c *s3Client) Remove() *probe.Error { return probe.NewError(err) } +// RemoveIncompleteUpload - remove multiparts of an incomplete multiart upload +func (c *s3Client) RemoveIncompleteUpload() *probe.Error { + bucket, object := c.url2BucketAndObject() + ch := c.api.DropIncompleteUpload(bucket, object) + err := <-ch + return probe.NewError(err) +} + // Share - get a usable get object url to share func (c *s3Client) ShareDownload(expires time.Duration) (string, *probe.Error) { bucket, object := c.url2BucketAndObject() diff --git a/pkg/client/s3v4/s3v4.go b/pkg/client/s3v4/s3v4.go index 0c15c825..144931b6 100644 --- a/pkg/client/s3v4/s3v4.go +++ b/pkg/client/s3v4/s3v4.go @@ -88,6 +88,14 @@ func (c *s3Client) Remove() *probe.Error { return probe.NewError(err) } +// RemoveIncompleteUpload - remove multiparts of an incomplete multipart upload +func (c *s3Client) RemoveIncompleteUpload() *probe.Error { + bucket, object := c.url2BucketAndObject() + ch := c.api.DropIncompleteUpload(bucket, object) + err := <-ch + return probe.NewError(err) +} + // Share - get a usable get object url to share func (c *s3Client) ShareDownload(expires time.Duration) (string, *probe.Error) { bucket, object := c.url2BucketAndObject() diff --git a/remove-main.go b/remove-main.go index c990b511..37f1cc62 100644 --- a/remove-main.go +++ b/remove-main.go @@ -34,26 +34,36 @@ var rmCmd = cli.Command{ mc {{.Name}} - {{.Usage}} USAGE: - mc {{.Name}} TARGET + mc {{.Name}} TARGET [incomplete] [force] + + incomplete - remove incomplete uploads + force - force recursive remove EXAMPLES: 1. Remove a file on Cloud storage $ mc {{.Name}} https://s3.amazonaws.com/jazz-songs/louis/file01.mp3 2. Remove a folder recursively on Cloud storage - $ mc {{.Name}} force https://s3.amazonaws.com/jazz-songs/louis/... + $ mc {{.Name}} https://s3.amazonaws.com/jazz-songs/louis/... force 3. Remove a bucket on Minio cloud storage $ mc {{.Name}} https://play.minio.io:9000/mongodb-backup 4. Remove a bucket on Cloud storage recursively - $ mc {{.Name}} force https://s3.amazonaws.com/jazz-songs/... + $ mc {{.Name}} https://s3.amazonaws.com/jazz-songs/... force 5. Remove a file on local filesystem: $ mc {{.Name}} march/expenses.doc 6. Remove a file named "force" on local filesystem: $ mc {{.Name}} force force + + 7. Remove incomplete upload of a file on Cloud storage: + $ mc {{.Name}} https://s3.amazonaws.com/jazz-songs/louis/file01.mp3 incomplete + + 2. Remove incomplete uploads of folder recursively on Cloud storage + $ mc {{.Name}} force https://s3.amazonaws.com/jazz-songs/louis/... incomplete force + `, } @@ -106,11 +116,6 @@ func rm(url string) { } func rmAll(url string) { - clnt, err := url2Client(url) - if err != nil { - errorIf(err.Trace(), "Unable to get client object for "+url) - return - } urlPartial1 := url2Dir(url) out, err := rmList(url) if err != nil { @@ -127,10 +132,35 @@ func rmAll(url string) { err = newclnt.Remove() errorIf(err, "Unable to remove : "+urlFull) } - _, err = clnt.Stat() - if err == nil { - err = clnt.Remove() - errorIf(err, "Unable to remove : "+clnt.URL().String()) +} + +func rmIncompleteUpload(url string) { + clnt, err := url2Client(url) + if err != nil { + errorIf(err.Trace(), "Unable to get client object for "+url) + return + } + err = clnt.RemoveIncompleteUpload() + errorIf(err.Trace(), "Unable to remove "+url) +} + +func rmAllIncompleteUploads(url string) { + clnt, err := url2Client(url) + if err != nil { + errorIf(err.Trace(), "Unable to get client object for "+url) + return + } + urlPartial1 := url2Dir(url) + ch := clnt.List(true, true) + for entry := range ch { + urlFull := urlPartial1 + entry.Content.Name + newclnt, e := url2Client(urlFull) + if e != nil { + errorIf(e, "Unable to create client object : "+urlFull) + continue + } + err = newclnt.RemoveIncompleteUpload() + errorIf(err, "Unable to remove : "+urlFull) } } @@ -138,15 +168,20 @@ func checkRmSyntax(ctx *cli.Context) { args, err := args2URLs(ctx.Args()) fatalIf(err.Trace(), "args2URL failed") var force bool - if len(args) == 0 { + length := len(args) + if length == 0 { cli.ShowCommandHelpAndExit(ctx, "rm", 1) // last argument is exit code. } if len(args) == 1 && args[0] == "force" { cli.ShowCommandHelpAndExit(ctx, "rm", 1) } - if args[0] == "force" { + if args[length-1] == "force" { force = true - args = args[1:] + args = args[:length-1] + length-- + } + if args[length-1] == "incomplete" { + args = args[:length-1] } // If input validation fails then provide context sensitive help without displaying generic help message. // The context sensitive help is shown per argument instead of all arguments to keep the help display @@ -154,11 +189,11 @@ func checkRmSyntax(ctx *cli.Context) { for _, arg := range args { url := client.NewURL(arg) if strings.HasSuffix(arg, string(url.Separator)) { - helpStr := "Usage : mc rm force " + arg + recursiveSeparator + helpStr := "Usage : mc rm " + arg + recursiveSeparator + " force" fatalIf(errDummy().Trace(), helpStr) } if isURLRecursive(arg) && !force { - helpStr := "Usage : mc rm force " + arg + helpStr := "Usage : mc rm " + arg + " force" fatalIf(errDummy().Trace(), helpStr) } if url.Type == client.Filesystem { @@ -167,7 +202,7 @@ func checkRmSyntax(ctx *cli.Context) { isRecursive := isURLRecursive(arg) path := stripRecursiveURL(arg) if isRecursive && (strings.HasSuffix(path, string(url.Separator)) == false) { - helpStr := "Usage : mc rm force " + path + string(url.Separator) + recursiveSeparator + helpStr := "Usage : mc rm " + path + string(url.Separator) + recursiveSeparator + " force" fatalIf(errDummy().Trace(), helpStr) } _, content, err := url2Stat(path) @@ -175,7 +210,7 @@ func checkRmSyntax(ctx *cli.Context) { fatalIf(err.Trace(), "url2stat error on "+arg) } if content.Type&os.ModeDir != 0 && !isRecursive { - helpStr := "Usage : mc rm force " + arg + string(url.Separator) + recursiveSeparator + helpStr := "Usage : mc rm " + arg + string(url.Separator) + recursiveSeparator + " force" fatalIf(errDummy().Trace(), helpStr) } continue @@ -187,15 +222,34 @@ func mainRm(ctx *cli.Context) { checkRmSyntax(ctx) args, err := args2URLs(ctx.Args()) fatalIf(err.Trace(), "args2URL failed") - if args[0] == "force" { - args = args[1:] + var incomplete bool + length := len(args) + + if args[length-1] == "force" { + args = args[:length-1] + length-- } - for _, arg := range args { - if isURLRecursive(arg) { - url := stripRecursiveURL(arg) - rmAll(url) - } else { - rm(arg) + if args[length-1] == "incomplete" { + args = args[:length-1] + incomplete = true + } + if incomplete { + for _, arg := range args { + if isURLRecursive(arg) { + url := stripRecursiveURL(arg) + rmAllIncompleteUploads(url) + } else { + rmIncompleteUpload(arg) + } + } + } else { + for _, arg := range args { + if isURLRecursive(arg) { + url := stripRecursiveURL(arg) + rmAll(url) + } else { + rm(arg) + } } } }