这里简单的问题?...
如何通过boto检查AWS存储桶是否存在?...最好通过提供路径?...
这是我想采取的方法:
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账户拥有的存储桶中是否存在存储桶...
是否有更好的方法来找出存储桶是否存在?我将如何实施更好的方法?
谢谢
您可以尝试加载存储桶(就像现在一样)。默认情况下,该方法设置为验证存储桶是否存在。
您也可以尝试“查找”存储桶。该方法将引发S3ResponseError。
以这种方式,您将必须进行API调用,因此我认为您可以根据自己的喜好(无论您是想处理异常还是只是检查None)。
因此,您在这里有几个选择:
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."
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句