python – 如何使用boto3删除AWS桶中的文件夹及其内容

关于如何删除文件夹的内容,
documentation有点模棱两可.如果你看看它是如何为boto3完成的,键没有在boto3前面部分中定义,它只在boto2示例中定义.

什么是灵活的(超过1000个文件)成语删除文件夹的内容?

最佳答案 我不认为你可以在boto2中的一个成语中删除1000个项目.但是,从boto3的角度来看,您可以尝试以下方法:

s3 = boto3.resource('s3')
bucket = s3.Bucket('bucket-name')
bucket.objects.filter(Prefix="path/to/dir").delete()

以上测试并正在运行

>>> import boto3
>>> s3 = boto3.resource('s3')
>>> b = s3.Bucket('MY_BUCKET_NAME')
>>> b.objects.filter(Prefix="test/stuff")
s3.Bucket.objectsCollection(s3.Bucket(name='MY_BUCKET_NAME'), s3.ObjectSummary)
>>> list(b.objects.filter(Prefix="test/stuff"))
[s3.ObjectSummary(bucket_name='MY_BUCKET_NAME', key=u'test/stuff/new')]
>>> b.objects.filter(Prefix="test/stuff").delete()
[{u'Deleted': [{u'Key': 'test/stuff/new'}], 'ResponseMetadata': {'HTTPStatusCode': 200, 'RetryAttempts': 0, 'HostId': 'BASE64_ID_1', 'RequestId': 'REQ_ID', 'HTTPHeaders': {'x-amz-id-2': 'BASE64_ID_2', 'server': 'AmazonS3', 'transfer-encoding': 'chunked', 'connection': 'close', 'x-amz-request-id': 'REQ_ID', 'date': 'Fri, 12 May 2017 21:21:47 GMT', 'content-type': 'application/xml'}}}]
>>>
点赞