如何使用golang批量删除minio中的对象

发布于 2025-01-14 23:35:24 字数 1080 浏览 1 评论 0原文

我正在尝试按照此处:

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)
}

我可以通过bucketnameprefixnameListObjects。然而,我正在努力寻找一种方法,可以通过例如我想要删除的对象名称片段或任何其他方式来 ListObjects 。所以我的问题是:如何为给定存储桶中的任意 objectNames 正确生成 ListObjects ?或者还有其他方法可以通过名称删除对象吗?谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏至、离别 2025-01-21 23:35:24
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
}

并用以下方式调用它:

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

我尝试过并且它有效,以防您仍然需要它

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文