[英]Python: how to download a file from an S3 bucket
I have some data stored in an AWS
S3
bucket.我有一些数据存储在
AWS
S3
存储桶中。
If from terminal I do:如果从终端我做:
aws s3 ls s3://myBucket/folder/ --profile myProfile
2020-04-23 01:04:09 96858539 2020-01-01-file.csv.gz
If I try to download the file using boto3
如果我尝试使用
boto3
下载文件
import boto3
session = boto3.session.Session(profile_name='myProfile')
s3 = boto3.resource('s3')
f1 = '2020-01-01-file.csv.gz'
s3.meta.client.download_file('myBucket', 'folder/%s'%f1, f1)
I get the following error我收到以下错误
ClientError: An error occurred (403) when calling the HeadObject operation: Forbidden
The reason why it doesn't work for you is that you create boto3 session
它对您不起作用的原因是您创建了 boto3
session
session = boto3.session.Session(profile_name='myProfile')
session = boto3.session.Session(profile_name='myProfile')
and you do not use it .而你不使用它。 So instead of
boto3.resource('s3')
, should try session.resource('s3')
所以代替
boto3.resource('s3')
,应该尝试session.resource('s3')
But anyway, the boto3 docs have an entire section called Downloading Files .但无论如何,boto3 文档都有一个名为Downloading Files的完整部分。 It shows two examples with explanation:
它显示了两个带有解释的示例:
import boto3
s3 = boto3.client('s3')
s3.download_file('BUCKET_NAME', 'OBJECT_NAME', 'FILE_NAME')
or或者
s3 = boto3.client('s3')
with open('FILE_NAME', 'wb') as f:
s3.download_fileobj('BUCKET_NAME', 'OBJECT_NAME', f)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.