使用boto3从S3 Bucket下载文件,方法是将凭据作为参数传递

时间:2016-08-11 06:04:18

标签: python python-3.x amazon-s3 aws-sdk boto3

根据Boto3官方文档,我们可以通过传递凭证作为参数来连接S3存储桶。但我面临着问题。

工作场景:硬编码密钥ID&密钥

s3r = boto3.resource('s3', aws_access_key_id='XXXXXXXXXXXXXXXXXXXX',
    aws_secret_access_key='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
buck = s3r.Bucket('bucket name')
buck.download_file(filename,filename)

非工作场景:作为参数传递

AccessKey = 'XXXXXXXXXXXXXXXXXXXX'
SecretKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
s3r = boto3.resource('s3', aws_access_key_id=AccessKey, aws_secret_access_key=SecretKey)
buck = s3r.Bucket('bucket name')
buck.download_file(filename,filename)

对于非工作方案,我面临以下错误。

botocore.exceptions.ClientError: An error occurred (403) when calling the HeadObject operation: Forbidden

1 个答案:

答案 0 :(得分:0)