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

发布于 2024-11-17 13:06:34 字数 670 浏览 6 评论 0原文

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

conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)

print conn.delete_Bucket('BucketNameHere').message

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

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

如何删除版本?

我可以使用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 中有什么可比的吗?

When I try to delete a bucket using the lines:

conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)

print conn.delete_Bucket('BucketNameHere').message

It tells me the bucket I tried to delete is not empty.

The bucket has no keys in it. But it does have versions.

How can I delete the versions?

I can see the list of versions using bucket.list_versions()

Java has a deleteVersion Method on its s3 connection. I found that code here:

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

He does this line to delete the version:

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

Is there anything comparable in boto?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

菊凝晚露 2024-11-24 13:06:34

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

Boto does support versioned buckets after version 1.9c. Here's how it works:

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!!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文