Uploading and retrieving files

Introduction to AWS Boto in Python

Maksim Pecherskiy

Data engineer

Buckets and objects

Bucket with objects

Introduction to AWS Boto in 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
Introduction to AWS Boto in 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

Introduction to AWS Boto in 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

Introduction to AWS Boto in Python

Uploading files

File in S3

Introduction to AWS Boto in Python

Uploading more objects

Uploading more objects

Introduction to AWS Boto in Python

Listing objects in a bucket

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

print(response)

List objects response

Introduction to AWS Boto in Python

Listing objects in a bucket

Listing objects in a bucket

Introduction to AWS Boto in Python

Listing objects in a bucket

Listing objects in a bucket

Introduction to AWS Boto in Python

Listing objects in a bucket

Listing objects in a bucket

Introduction to AWS Boto in Python

Getting object metadata

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

print(response)

Head object method

Introduction to AWS Boto in Python

Getting object metadata

Object metadata response

Introduction to AWS Boto in Python

Downloading files

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

Downloading files

Introduction to AWS Boto in Python

Deleting objects

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

Delete an object

Introduction to AWS Boto in 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

Introduction to AWS Boto in Python

Let's make some objects!

Introduction to AWS Boto in Python

Preparing Video For Download...