Amazon S3以递归方式删除子文件夹

时间:2018-05-31 00:12:16

标签: amazon-web-services amazon-s3 aws-lambda aws-sdk boto3

我有一个带有嵌套文件夹和文件结构的S3存储桶。我知道S3有一个扁平的概念,但在我清楚地陈述我的问题时请耐心等待。

我想这样做:

delete bucket/do-not-delete-folder/delete-this-folder-recursively

我已经倾注了S3文档,并按照python - Amazon S3 boto - how to delete folder? - Stack Overflow给出的答案。

我尝试了两种不同的解决方案,它们产生两种不同的输出,你猜对了,没有人做我想做的事情。

功能1:

这会删除" do-not-delete-folder"及其所有内容递归。我只想删除子文件夹而不是顶级文件夹。

我用以下参数表示:

DeleteAWSBucketObjects(my-bucket, do-not-delete-folder/delete-this-folder/)

功能是:

def DeleteAWSBucketObjects(bucketName, prefixStr):

    s3 = boto3.resource('s3')
    bucket = s3.Bucket(bucketName)

    try:
        client = boto3.client('s3')
        response = client.list_objects_v2(Bucket=bucketName, Prefix=prefixStr)
        if 'Contents' in response:
            for item in response['Contents']:
                while response['KeyCount'] == 1000:
                    response = client.list_objects_v2(Bucket=bucketName,
                                StartAfter=response['Contents'][0]['Key'],)
                    for item in response['Contents']:
                        client.delete_object(Bucket=bucketName, Key=item['Key'])
    except ClientError as e:
        error_code = int(e.response['Error']['Code'])
        print ("DeleteAWSBucketObjects : hit an exception")
        print (error_code)

    return

功能2:

取自上述链接。我用与上面提到的相同的参数来称呼它。

奇怪的是,这会导致Access-denied错误。我在Lambda和相同的AWS账户中的相同代码文件中运行它,所以我不确定为什么一个版本的函数删除所有内容而另一个版本没有删除任何内容的权限。

DeleteAWSBucketObjectsV2(my-bucket, do-not-delete-folder/delete-this-folder/)

功能:

def DeleteAWSBucketObjectsV2(bucketName, prefixStr):

    s3 = boto3.resource('s3')
    bucket = s3.Bucket(bucketName)
    objects_to_delete = s3.meta.client.list_objects(Bucket=bucketName, Prefix=prefixStr)

    delete_keys = {'Objects' : []}
    delete_keys['Objects'] = [{'Key' : k} for k in [obj['Key'] for obj in objects_to_delete.get('Contents', [])]]
    for i in delete_key:
        print("delete_key_array !!!")
        print(str(i))
    try:
        s3.meta.client.delete_objects(Bucket="MyBucket", Delete=delete_keys)
    except ClientError as e:
        error_code = int(e.response['Error']['Code'])
        print ("DeleteAWSBucketObjects : hit an exception")
        print (error_code)

1 个答案:

答案 0 :(得分:0)

我从您的问题中了解到,您要删除名为 delete-this-folder 的子文件夹,因此这是一种简单的方法:

import boto3
s3 = boto3.resource('s3')
bucket=s3.Bucket('mausamrest');
obj = s3.Object('mausamrest','do-not-delete-folder/delete-this-folder/')
counter=-1


for obj in bucket.objects.filter(Prefix='do-not-delete-folder/delete-this-folder/'):
    print(obj.key)
    try:
        obj.delete()
        counter=counter+1
    except:
        print("error occurred while deleting")

if(counter==-1):
    print("subfolder doesn't exists")
if(counter!=-1):
    print("1 subfolder and "+str(counter)+" items in it deleted") 

此处 mausamrest 是广告管理名称,不要删除文件夹/删除此文件夹/ 是子目录