dongzhuange2625 2017-10-17 17:35
浏览 263
已采纳

如何使用适用于GOlang的AWS开发工具包清空S3存储桶?

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集