-
-
Save harshavardhana/bc4cc8a5bce26d06b382 to your computer and use it in GitHub Desktop.
Delete All the objects in the bucket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"github.com/minio/minio-go" | |
"fmt" | |
) | |
func main() { | |
accessKey := "accessKey" | |
secretKey := "secretKey" | |
url := "url" | |
myBucket := "minio-bucket1" | |
s3Client, err := minio.NewV2(url, accessKey, secretKey, true) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
// 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) | |
var arr []minio.ObjectInfo | |
// List all objects from a bucket-name with a matching prefix. | |
for object := range s3Client.ListObjects(myBucket, "", false, doneCh) { | |
if object.Err != nil { | |
fmt.Println(object.Err) | |
return | |
} | |
arr = append(arr, object) | |
} | |
var wg sync.WaitGroup | |
for index := range arr { | |
wg.Add(1) | |
go func(index int) { | |
defer wg.Done() | |
err = s3Client.RemoveObject(myBucket, arr[index].Key) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
log.Println("Success", arr[index].Key) | |
} (index) | |
} | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment