I tried to create a script which get a file from specific Bucket in S3,
for example: from "s3://my-bucket/veryCoolFile.img" I want to get veryCoolFile.img
what is the easiest way to do this?
-
Possible duplicate of Download file from AWS S3 using PythonAndré C. Andersen– André C. Andersen2019-10-24 18:17:55 +00:00Commented Oct 24, 2019 at 18:17
Add a comment
|
1 Answer
Python have boto3 which its API to most (if not all) the operations in 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.