如何使用boto和python从bucket中删除s3版本

2024-09-29 17:09:42 发布

您现在位置:Python中文网/ 问答频道 /正文

当我尝试使用以下行删除bucket时:

conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)

print conn.delete_Bucket('BucketNameHere').message

它告诉我我试图删除的bucket不是空的。在

桶里没有钥匙。但它确实有版本。在

如何删除版本?在

我可以看到使用的版本列表bucket.list\u版本()

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

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

他这样做是为了删除版本:

^{pr2}$

博图有什么可比的吗?在


Tags: key版本awsidsecrets3bucketaccess
1条回答
网友
1楼 · 发布于 2024-09-29 17:09:42

Boto在1.9c版本之后确实支持版本控制的bucket,其工作原理如下:

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!!

相关问题 更多 >

    热门问题