Skip to content

Instantly share code, notes, and snippets.

@krishnasrinivas
Created April 11, 2016 14:58
Show Gist options
  • Save krishnasrinivas/99e9da7824d2324e62834afcfcaad120 to your computer and use it in GitHub Desktop.
Save krishnasrinivas/99e9da7824d2324e62834afcfcaad120 to your computer and use it in GitHub Desktop.
remove objects recursively
package main
import (
"fmt"
"log"
"github.com/minio/minio-go"
)
func main() {
// Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY, my-bucketname and my-prefixname
// are dummy values, please replace them with original values.
// Requests are always secure (HTTPS) by default. Set insecure=true to enable insecure (HTTP) access.
// This boolean value is the last argument for New().
// New returns an Amazon S3 compatible client object. API copatibality (v2 or v4) is automatically
// determined based on the Endpoint value.
s3Client, err := minio.New("s3.amazonaws.com", "YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY", false)
if err != nil {
fmt.Println(err)
return
}
// Create a done channel to control 'ListObjects' go routine.
doneCh := make(chan struct{})
// Indicate to our routine to exit cleanly upon return.
defer close(doneCh)
// List all objects from a bucket-name with a matching prefix.
for object := range s3Client.ListObjects("my-bucketname", "", true, doneCh) {
if object.Err != nil {
fmt.Println(object.Err)
return
}
err = s3Client.RemoveObject("my-bucketname", object.Key)
if err != nil {
log.Fatalln(err)
}
fmt.Println("removed ", object.Key)
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment