Links

Get files from S3 bucket

Tags: #aws #cloud #storage #S3bucket #operations #snippet #url
Author: Maxime Jublou
Description: This notebook provides a step-by-step guide to retrieving files from an Amazon Web Services (AWS) S3 bucket, allowing users to easily access their data stored in the cloud.

Input

Install packages

!pip install boto3 getpass4

Import library

import boto3

Variables

ACCESS_KEY_ID = "**********"
SECRET_ACCESS_KEY = "**********"
BUCKET_NAME = "naas-example"
BUCKET_OBJECT_KEY = "naas_happy_hour.mp3"

Model

Get file

s3 = boto3.client(
"s3", aws_access_key_id=ACCESS_KEY_ID, aws_secret_access_key=SECRET_ACCESS_KEY
)
fileObj = s3.get_object(Bucket=bucketname, Key=filename)

Generate pre-signed URL

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

Output

Display file

fileOBJ

Display pre-signed URL

file_url