'How to bulk remove objects in minio with golang

I'm trying to bulk remove objects in minio as described here:

objectsCh := make(chan minio.ObjectInfo)

// Send object names that are needed to be removed to objectsCh
go func() {
    defer close(objectsCh)
    // List all objects from a bucket-name with a matching prefix.
    for object := range minioClient.ListObjects(context.Background(), "my-bucketname", "my-prefixname", true, nil) {
        if object.Err != nil {
            log.Fatalln(object.Err)
        }
        objectsCh <- object
    }
}()

opts := minio.RemoveObjectsOptions{
    GovernanceBypass: true,
}

for rErr := range minioClient.RemoveObjects(context.Background(), "my-bucketname", objectsCh, opts) {
    fmt.Println("Error detected during deletion: ", rErr)
}

Where I can ListObjects by bucketname and prefixname. However I'm struggling to find an approach where I can ListObjects by for example a slice of object names which I want to remove or any other way. So my question is: how can I properly generate a ListObjects for arbitrary objectNames in a given bucket? Or is there any other way to do remove objects by their names? Thanks.



Solution 1:[1]

func DeleteItemInMinio(ctx context.Context, item []string) (string, error) {

    minioClient, err := minio.New("test.com", os.Getenv("MINIO_ACCESS_KEY"), os.Getenv("MINIO_SECRET_KEY"), true)
    if err != nil {
        log.Println(err)
    }

    for _, val := range item {
        err = minioClient.RemoveObject("my-bucketname", val)
        if err != nil {
            panic(err)
        }
    }

    return "success", nil
}

and call it with :

r.POST("/test/delete", func(c *gin.Context) {
        item := []string{"golang.png", "phplogo.jpg"}
        execute.DeleteItemInMinio(context.Background(), item)
    })

i tried it and it works, in case you still need it

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ferdinan97