Goal: To empty an existing S3 bucket using the AWS SDK for GOlang.
4条回答 默认 最新
- duanfei8399 2017-10-17 18:00关注
NOTE: These are code snippets that might require YOU to make changes on YOUR SIDE to make it run.
You will need to implement the below method:
//EmptyBucket empties the Amazon S3 bucket func (s awsS3) EmptyBucket(bucket string) error { log.Info("removing objects from S3 bucket : ", bucket) params := &s3.ListObjectsInput{ Bucket: aws.String(bucket), } for { //Requesting for batch of objects from s3 bucket objects, err := s.Client.ListObjects(params) if err != nil { return err } //Checks if the bucket is already empty if len((*objects).Contents) == 0 { log.Info("Bucket is already empty") return nil } log.Info("First object in batch | ", *(objects.Contents[0].Key)) //creating an array of pointers of ObjectIdentifier objectsToDelete := make([]*s3.ObjectIdentifier, 0, 1000) for _, object := range (*objects).Contents { obj := s3.ObjectIdentifier{ Key: object.Key, } objectsToDelete = append(objectsToDelete, &obj) } //Creating JSON payload for bulk delete deleteArray := s3.Delete{Objects: objectsToDelete} deleteParams := &s3.DeleteObjectsInput{ Bucket: aws.String(bucket), Delete: &deleteArray, } //Running the Bulk delete job (limit 1000) _, err = s.Client.DeleteObjects(deleteParams) if err != nil { return err } if *(*objects).IsTruncated { //if there are more objects in the bucket, IsTruncated = true params.Marker = (*deleteParams).Delete.Objects[len((*deleteParams).Delete.Objects)-1].Key log.Info("Requesting next batch | ", *(params.Marker)) } else { //if all objects in the bucket have been cleaned up. break } } log.Info("Emptied S3 bucket : ", bucket) return nil }
UPDATE : The latest version of AWS SDK for GO has resolved the prior issue I had.
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥20 Keil uVision5创建project没反应
- ¥15 mmseqs内存报错
- ¥15 vika文档如何与obsidian同步
- ¥15 华为手机相册里面的照片能够替换成自己想要的照片吗?
- ¥15 陆空双模式无人机飞控设置
- ¥15 sentaurus lithography
- ¥100 求抖音ck号 或者提ck教程
- ¥15 关于#linux#的问题:子进程1等待子进程A、B退出后退出(语言-c语言)
- ¥20 web页面如何打开Outlook 365的全球离线通讯簿功能
- ¥15 io.jsonwebtoken.security.Keys