Boto API调用返回“指定的存储桶不存在”,但在控制台可以看到并列出
首先,给大家一些背景信息,这段代码是一个更大脚本的一部分。我现在使用的是.boto配置文件来存储我的凭证。我可以通过boto API调用列出所有的存储桶,也可以创建存储桶,但这一段代码让我遇到了一些问题,看起来是没问题的(其实这段代码在之前的版本中是可以正常工作的,现在似乎出现了卡住的情况)。
#!/usr/bin/python
import boto
from boto.s3.connection import S3Connection
conn = S3Connection()
n = "zapbucketx"
rs = conn.get_all_buckets()
for b in rs:
if b.name == n:
print b.name + n
try:
conn.delete_bucket('n')
print "Bucket did not contian keys and was deleted!"
except:
print "WARNING: Bucket Contains Keys!"
ans = raw_input("Do you want to continue? (y/n)")
mod_ans = str(ans.upper())
if mod_ans == 'Y':
#print "got here"
full_bucket = conn.get_bucket(n)
print "[+] Deleting keys..."
for key in full_bucket.list():
key.delete()
print "[+] Keys deleted, Deleting Bucket"
conn.delete_bucket(n)
print "[+] Bucket deleted"
exit(0)
elif mod_ans == 'N':
print "No bucket was deleted."
exit(0)
else:
print "Please answer 'y' or 'n'."
exit(0)
exit(0)
else:
print "That bucket does not exist!"
exit(0)
它似乎卡在了:
if b.name == n:
因为我在“else”语句中得到了自己的“错误”:
print "That bucket does not exist!"
我在不同的实例上尝试过这段代码,并且仔细检查了文件中的凭证(但因为我可以列出存储桶,所以看起来这不是凭证的问题)。
如果能得到任何帮助,我将非常感激,谢谢!
1 个回答
1
for b in rs:
if b.name == n:
...
else:
print "That bucket does not exist!"
exit(0)
这段代码似乎假设账户上只有一个存储桶。如果有多个存储桶,那么这段代码能否正常工作就不一定了:可能会先返回存储桶 zapbucketx
,也可能不会。
看起来你真正想要的是
else:
continue
这样循环就会继续检查存储桶列表,直到找到 zapbucketx。
另外,这行代码是错的:
conn.delete_bucket('n')
你应该用 conn.delete_bucket(n)
。
你可能还想在 delete_bucket
调用后加个退出,否则循环会继续到下一个存储桶名称,然后就会报错说存储桶不存在。
还有,你在成功删除的消息里有个拼写错误:“contian”。