问题描述
这里有个简单的问题? ...
Simple question here? ...
如何使用 boto 检查AWS存储桶是否存在? ...最好通过提供路径? ...
How can I check with boto that a AWS bucket exists? ... preferably by providing the path? ...
这是我想采用的方法:
def bucket_exists(self, bucket_name):
connection = boto.s3.connection.S3Connection('<aws access key>', '<aws secret key>')
buckets = connection.get_all_buckets()
for bucket in buckets:
bucket_name = bucket.name
# Bucket existence logic here
# submit boto request
ie:. exists = boto.get_bucket(bucket_name, validate=True)
if exists:
return True
else:
return False
在上面的代码中,我感兴趣的是查找此AWS账户拥有的存储桶中是否存在存储桶...
In the code above I am interested to find if a bucket exists amongst the buckets that this AWS Account owns ...
是否有更好的方法来找出存储桶是否存在?我将如何实施更好的方法?
IS there a better way to find out if a bucket exists? How would I implement a better way?
谢谢
推荐答案
您可以尝试加载存储桶(就像现在所做的那样).默认情况下,该方法设置为验证存储桶是否存在.
You could just try and load the bucket (as you are doing now). By default the method is set to validate if the bucket exists.
您也可以尝试查找"存储桶.该方法将引发S3ResponseError.
You could also just try to "lookup" the bucket. The method will raise an S3ResponseError.
以其他方式,您必须进行API调用,所以我认为您可以根据个人喜好(无论是处理异常还是简单地检查None).
Ether way, you'll have to make an API call so I think you're left to personal preference here (whether you like dealing with exceptions, or simply checking for None).
所以您在这里有几个选择:
So you have a couple of options here:
bucket = connection.lookup('this-is-my-bucket-name')
if bucket is None:
print "This bucket doesn't exist."
或者:
try:
bucket = connection.get_bucket('this-is-my-bucket-name')
except S3ResponseError:
print "This bucket doesn't exist."
这篇关于如何检查AWS S3存储桶是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!