[英]Download files from public S3 bucket with boto3
I cannot download a file or even get a listing of the public S3 bucket with boto3
. 我无法下载文件,甚至无法获得带有boto3
的公共 S3存储桶的boto3
。
The code below works with my own bucket, but not with public one: 以下代码适用于我自己的存储桶,但不适用于公共存储桶:
def s3_list(bucket, s3path_or_prefix):
bsession = boto3.Session(aws_access_key_id=settings.AWS['ACCESS_KEY'],
aws_secret_access_key=settings.AWS['SECRET_ACCESS_KEY'],
region_name=settings.AWS['REGION_NAME'])
s3 = bsession.resource('s3')
my_bucket = s3.Bucket(bucket)
items = my_bucket.objects.filter(Prefix=s3path_or_prefix)
return [ii.key for ii in items]
I get an AccessDenied
error on this code. 我在此代码上收到AccessDenied
错误。 The bucket is not in my own and I cannot set permissions there, but I am sure it is open to public read. 存储桶不是我自己的,因此我无法在其中设置权限,但是我确定它是开放给公众阅读的。
I had the similar issue in the past. 我过去也有类似的问题。 I have found a key to this bug in https://github.com/boto/boto3/issues/134 . 我在https://github.com/boto/boto3/issues/134中找到了此错误的关键。
You can use undocumented trick: 您可以使用未记录的技巧:
import botocore
def s3_list(bucket, s3path_or_prefix, public=False):
bsession = boto3.Session(aws_access_key_id=settings.AWS['ACCESS_KEY'],
aws_secret_access_key=settings.AWS['SECRET_ACCESS_KEY'],
region_name=settings.AWS['REGION_NAME'])
client = bsession.client('s3')
if public:
client.meta.events.register('choose-signer.s3.*', botocore.handlers.disable_signing)
result = client.list_objects(Bucket=bucket, Delimiter='/', Prefix=s3path_or_prefix)
return [obj['Prefix'] for obj in result.get('CommonPrefixes')]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.