简体   繁体   English

通过传递凭据作为参数,使用boto3从S3存储桶下载文件

[英]Download a file from S3 Bucket using boto3 by passing credentials as parameters

As per Boto3 official documentation, we can connect S3 bucket by passing credentials as a parameters. 根据Boto3官方文档,我们可以通过传递凭据作为参数来连接S3存储桶。 But I am facing issues. 但是我面临着问题。

Working Scenario : Hardcoding Key ID & Secret Key 工作方案:硬编码密钥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)

Non Working Scenario : Passing as parameters 非工作方案:作为参数传递

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)

I am facing below error for non-working scenario. 对于非工作方案,我面临以下错误。

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

http://boto3.readthedocs.io/en/latest/guide/configuration.html http://boto3.readthedocs.io/en/latest/guide/configuration.html

session = boto3.Session(aws_access_key_id=AccessKey, aws_secret_access_key=SecretKey)
s3 = session.resource('s3')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM