[英]Is there a easy way to get files from S3 by python? [on hold]
I tried to create a script which get a file from specific Bucket in S3,我试图创建一个脚本,从 S3 中的特定存储桶获取文件,
for example: from
"s3://my-bucket/veryCoolFile.img"
I want to get veryCoolFile.img例如:从
"s3://my-bucket/veryCoolFile.img"
我想得到veryCoolFile.img
what is the easiest way to do this?最简单的方法是什么?
You can use Amazon's tool called Boto http://boto.cloudhackers.com/en/latest/您可以使用名为Boto http://boto.cloudhackers.com/en/latest/的亚马逊工具
Python have boto3 which its API to most (if not all) the operations in AWS. Python 有 boto3,它的 API 用于 AWS 中的大多数(如果不是全部)操作。 the way to do your mission could be done by the next code:
执行任务的方法可以通过以下代码完成:
def get_file_fore_s3(bucket,file_name):
AWS_ACCESS_KEY_ID = <YOUR_ACCESS_KEY_ID>
AWS_SECRET_ACCESS_KEY = <YOUR_AWS_SECRET_ACCESS_KEY>
s3_client = boto3.client('s3', aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
s3_resource = boto3.resource('s3', aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
all_objects = s3_client.list_objects(Bucket= bucket)
for object in all_objects['Contents']:
current_file_name = object['Key]
if file_name == current_file_name:
f = open(file_name, "w+")
f.close()
s3_resource.meta.client.download_file(bucket, file_name,file_name)
The idea behind the solution is to get the bucket element (which describe by all_objects im my code) and iterate over the "Content" of the bucket.该解决方案背后的想法是获取存储桶元素(由我的代码中的 all_objects 描述)并迭代存储桶的“内容”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.