Created
August 10, 2019 17:48
-
-
Save brydavis/b882862be43f1f75e8ac9e13541c039d to your computer and use it in GitHub Desktop.
Simple S3 operations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# install boto3 | |
# pip install --upgrade boto3 | |
# setup IAM and get credentials | |
# https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html | |
# https://console.aws.amazon.com/iam/home | |
""" | |
- Create a User | |
- Give user a name and set "Programmatic access" only for Access Type | |
- Next, in permission setting, select "Attach Existing Policies Directly" | |
- Select "AmazonS3FullAccess" | |
- Copy Key ID and Key (one time only!) | |
- Let's set up the credentials file (~.aws/credentials) | |
""" | |
import boto3 | |
import logging | |
from botocore.exceptions import ClientError | |
s3 = boto3.client('s3') | |
region = "us-west-2" # defaults to us-east-1 | |
s3 = boto3.client('s3', region_name=region) | |
# download data from somewhere using requests | |
import requests | |
file_url = "https://data.teleona.com/iris.csv" | |
r = requests.get(file_url, allow_redirects=True) | |
with open(file_url.split("/")[-1], 'wb') as file: | |
file.write(r.content) | |
# download data from other buckets using boto3 | |
BUCKET_NAME = 'your_bucket_name' # replace with your bucket name | |
KEY = 'iris.csv' # replace with your object key | |
try: | |
s3.download_file(BUCKET_NAME, KEY, KEY) | |
except ClientError as e: | |
if e.response['Error']['Code'] == "404": | |
print("The object does not exist.") | |
else: | |
raise | |
# create a bucket | |
bucket_name = "your_name_demo_bucket" # has to be unique like [first name].[last name].demo.bucket | |
try: | |
location = {'LocationConstraint': region} | |
s3.create_bucket( | |
Bucket=bucket_name, | |
CreateBucketConfiguration=location, | |
) | |
except ClientError as e: | |
logging.error(e) | |
# upload data to your bucket | |
try: | |
response = s3.upload_file( | |
"iris.csv", | |
bucket_name, | |
"iris.csv" | |
) | |
except ClientError as e: | |
logging.error(e) | |
# list your buckets | |
response = s3.list_buckets() | |
print('Existing buckets:') | |
for bucket in response['Buckets']: | |
print(f'{bucket["Name"]}') | |
# list all objects in a bucket | |
bucket_name = "your_bucket_name" | |
response = s3.list_objects(Bucket=bucket_name) | |
print("Existing objects in : {bucket_name}\n") | |
for obj in response['Contents']: | |
print(f'{obj}\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment