Get files from S3 bucket
Tags: #aws #cloud #storage #S3bucket
Author: Maxime Jublou

Input

Install packages

1
!pip install boto3 getpass4
Copied!

Import library

1
import boto3
Copied!

Variables

1
ACCESS_KEY_ID = "**********"
2
SECRET_ACCESS_KEY = "**********"
3
4
BUCKET_NAME = "naas-example"
5
BUCKET_OBJECT_KEY = 'naas_happy_hour.mp3'
Copied!

Model

Get file

1
s3 = boto3.client('s3', aws_access_key_id=ACCESS_KEY_ID, aws_secret_access_key=SECRET_ACCESS_KEY)
2
fileObj = s3.get_object(Bucket=bucketname, Key=filename)
Copied!

Generate pre-signed URL

1
file_url = s3.generate_presigned_url("get_object", Params={"Bucket": BUCKET_NAME, "Key": BUCKET_OBJECT_KEY}, ExpiresIn=604800)
Copied!

Output

Display file

1
fileOBJ
Copied!

Display pre-signed URL

1
file_url
Copied!
Copy link
Edit on GitHub