Uploading and retrieving files

Introducción a AWS Boto en Python

Maksim Pecherskiy

Data engineer

Buckets and objects

Bucket with objects

Introducción a AWS Boto en Python

A Bucket

S3 buckets

  • A bucket has a name
  • Name is a string
  • Unique name in all of S3.
  • Contains many objects

An Object

S3 objects

  • An object has a key
  • Name is full path from bucket root
  • Unique key in the bucket
  • Can only be in one parent bucket
Introducción a AWS Boto en Python

Creating the client

s3 = boto3.client(
  's3', 
  region_name='us-east-1', 
  aws_access_key_id=AWS_KEY_ID, 
  aws_secret_access_key=AWS_SECRET
)

Client communication with S3

Introducción a AWS Boto en Python

Uploading files

s3.upload_file(
  Filename='gid_requests_2019_01_01.csv', 
  Bucket='gid-requests', 
  Key='gid_requests_2019_01_01.csv')

Uploading object to a bucket

Introducción a AWS Boto en Python

Uploading files

File in S3

Introducción a AWS Boto en Python

Uploading more objects

Uploading more objects

Introducción a AWS Boto en Python

Listing objects in a bucket

response = s3.list_objects(
  Bucket='gid-requests', 
  MaxKeys=2,
  Prefix='gid_requests_2019_')

print(response)

List objects response

Introducción a AWS Boto en Python

Listing objects in a bucket

Listing objects in a bucket

Introducción a AWS Boto en Python

Listing objects in a bucket

Listing objects in a bucket

Introducción a AWS Boto en Python

Listing objects in a bucket

Listing objects in a bucket

Introducción a AWS Boto en Python

Getting object metadata

response = s3.head_object(
  Bucket='gid-requests', 
  Key='gid_requests_2018_12_30.csv')

print(response)

Head object method

Introducción a AWS Boto en Python

Getting object metadata

Object metadata response

Introducción a AWS Boto en Python

Downloading files

s3.download_file(
  Filename='gid_requests_downed.csv',
  Bucket='gid-requests', 
  Key='gid_requests_2018_12_30.csv')

Downloading files

Introducción a AWS Boto en Python

Deleting objects

s3.delete_object(
  Bucket='gid-requests', 
  Key='gid_requests_2018_12_30.csv')

Delete an object

Introducción a AWS Boto en Python

Summary

  • Buckets are like folders
  • Objects are like files
  • boto3.client()
  • s3.upload_file()
  • s3.list_objects()
  • s3.head_object()
  • s3.download_file()
  • s3.delete_object()

Buckets

Introducción a AWS Boto en Python

Let's make some objects!

Introducción a AWS Boto en Python

Preparing Video For Download...