gpt4 book ai didi

python - 如何使用 boto 和 python 从存储桶中删除 s3 版本

转载 作者:太空狗 更新时间:2023-10-29 17:58:18 25 4
gpt4 key购买 nike

当我尝试使用以下行删除存储桶时:

conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)

print conn.delete_Bucket('BucketNameHere').message

它告诉我我尝试删除的存储桶不为空。

桶中没有 key 。但它确实有版本。

如何删除版本?

我可以使用 bucket.list_versions() 查看版本列表

Java 在其 s3 连接上有一个 deleteVersion 方法。我在这里找到了代码:

http://bytecoded.blogspot.com/2011/01/recursive-delete-utility-for-version.html

他执行此行以删除版本:

s3.deleteVersion(new DeleteVersionRequest(bucketName, keyName, versionId));

boto有什么可比的吗?

最佳答案

Boto 在 1.9c 版本之后确实支持版本化的存储桶。这是它的工作原理:

import boto

s3 = boto.connect_s3()

#Create a versioned bucket
bucket = s3.create_bucket("versioned.example.com")
bucket.configure_versioning(True)

#Create a new key and make a few versions
key = bucket.new_key("versioned_object")
key.set_contents_from_string("Version 1")
key.set_contents_from_string("Version 2")

#Try to delete bucket
bucket.delete() ## FAILS with 409 Conflict

#Delete our key then try to delete our bucket again
bucket.delete_key("versioned_object")
bucket.delete() ## STILL FAILS with 409 Conflict

#Let's see what's in there
list(bucket.list()) ## Returns empty list []

#What's in there including versions?
list(bucket.list_versions()) ## Returns list of keys and delete markers

#This time delete all versions including delete markers
for version in bucket.list_versions():
#NOTE we're still using bucket.delete, we're just adding the version_id parameter
bucket.delete_key(version.name, version_id = version.version_id)

#Now what's in there
list(bucket.list_versions()) ## Returns empty list []

#Ok, now delete the bucket
bucket.delete() ## SUCCESS!!

关于python - 如何使用 boto 和 python 从存储桶中删除 s3 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6525270/

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