-3

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?

Lupidon
  • 599
  • 2
  • 17
  • Possible duplicate of [Download file from AWS S3 using Python](https://stackoverflow.com/questions/50100221/download-file-from-aws-s3-using-python) – André C. Andersen Oct 24 '19 at 18:17

1 Answers1

0

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.

Bizzu
  • 449
  • 3
  • 17