-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?

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.