gpt4 book ai didi

amazon-web-services - 如何使用适用于 Golang 的 AWS 开发工具包清空 S3 存储桶?

转载 作者:IT王子 更新时间:2023-10-29 02:02:39 26 4
gpt4 key购买 nike

目标:使用适用于 Golang 的 AWS 开发工具包清空现有的 S3 存储桶。

最佳答案

AWS SDK 现在有 BatchDeleteIterator 可以完成这项工作。通过 Amazon docs 提供的示例.

package main

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"

"fmt"
"os"
)

// go run s3_delete_objects BUCKET

func main() {
if len(os.Args) != 2 {
exitErrorf("Bucket name required\nUsage: %s BUCKET", os.Args[0])
}

bucket := os.Args[1]

// Initialize a session in us-west-2 that the SDK will use to load
// credentials from the shared credentials file ~/.aws/credentials.
sess, _ := session.NewSession(&aws.Config{
Region: aws.String("us-west-2")},
)

// Create S3 service client
svc := s3.New(sess)

// Setup BatchDeleteIterator to iterate through a list of objects.
iter := s3manager.NewDeleteListIterator(svc, &s3.ListObjectsInput{
Bucket: aws.String(bucket),
})

// Traverse iterator deleting each object
if err := s3manager.NewBatchDeleteWithClient(svc).Delete(aws.BackgroundContext(), iter); err != nil {
exitErrorf("Unable to delete objects from bucket %q, %v", bucket, err)
}

fmt.Printf("Deleted object(s) from bucket: %s", bucket)
}

func exitErrorf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}

关于amazon-web-services - 如何使用适用于 Golang 的 AWS 开发工具包清空 S3 存储桶?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46795963/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com