gpt4 book ai didi

python-3.x - 无法从版本控制的 s3 存储桶中删除项目

转载 作者:行者123 更新时间:2023-12-04 00:50:24 25 4
gpt4 key购买 nike

我正在尝试从 s3 存储桶中删除特定的 .jpg 文件。我尝试了许多不同的方法,例如 delete_object 和其他几个堆栈溢出答案。它们都没有正常工作,导致程序每次都以不同的退出代码崩溃。下面的代码不会使程序崩溃,但存储桶保持不变。有谁知道如何解决这个问题?

s3 = boto3.resource('s3')
s3.Object('my-bucket', 'label_file').delete()

打印调用响应时,这是打印到终端的消息。


{
'ResponseMetadata': {
'RequestId': 'FJEOSIJFIO2J31',
'HostId': 'VEt+tpQ+DSAW5UTx/+kGgmE0JX43345lX2ULHMDSDrKye1PsTA3Eo=',
'HTTPStatusCode': 314,
'HTTPHeaders': {
'x-amz-id-2': 'VEt+tpQo2DAS6z+m8o3215UTx/+kGgmE0JX2DSi3ULHMDrKyevPsTA3Eo=',
'x-amz-request-id': 'T255WdZ732sVDWJF',
'date': 'Wed, 07 Apr 2021 19:28:04 GMT',
'x-amz-version-id': '4gi8EtDS27ZNy221tkziO',
'x-amz-delete-marker': 'true',
'server': 'AmazonS3'
},
'RetryAttempts': 0
},
'DeleteMarker': True,
'VersionId': '4gioU7FD36t5hcX21IOtkziO'
}

最佳答案

您的 S3 存储桶启用了版本控制,因为您:

      'x-amz-delete-marker': 'true',
...
'DeleteMarker': True,

在响应中。

A delete marker in Amazon S3 is a placeholder (or marker) for aversioned object that was named in a simple DELETE request. Becausethe object is in a versioning-enabled bucket, the object is notdeleted. But the delete marker makes Amazon S3 behave as if it isdeleted.

所以,基本上,您必须删除标记,然后删除文件。

If you want to delete a delete marker, it must have a version ID, andyou must specify that ID in a DELETE Object versionId request. If youuse a DELETE request to delete a delete marker (without specifying theversion ID of the delete marker), Amazon S3 does not delete the deletemarker, but instead, inserts another delete marker.

More on removing the delete marker

要删除删除标记和版本化文件,您可以使用以下代码:

警告!这将有效地清除您的存储桶!

def remove_versions(target_bucket: str) -> None:
s3 = boto3.resource("s3", region_name="your_s3_bucket_region_goes_here")
bucket = s3.Bucket(target_bucket)
object_version_iterator = bucket.object_versions.all()

for object_version in object_version_iterator:
key = object_version.object_key
object_id = object_version.id
print(f"Deleting key: {key} with id: {object_id}")
object_version.delete()

print(f"Done. {target_bucket} has been purged.")

关于python-3.x - 无法从版本控制的 s3 存储桶中删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67047895/

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