Skip to content

Instantly share code, notes, and snippets.

@khalido
Created October 15, 2017 22:26
Show Gist options
  • Save khalido/b70d1c07cab9f0de1c969ffa248f17d2 to your computer and use it in GitHub Desktop.
Save khalido/b70d1c07cab9f0de1c969ffa248f17d2 to your computer and use it in GitHub Desktop.
[upload image to a s3 bucket]
import boto3 # Amazon's aws library for python 3
import numpy as np
import matplotlib.pyplot as plt
# create a connection to s3
s3 = boto3.resource('s3',
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key)
# you need a bucket, make one and put its name here
bucket = "some_bucket_name"
# lets plot something so we have an image to upload
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
# save the plot to a static folder
image_name = "test.png"
plt.savefig("static/" + image_name)
# upload image to aws s3
# warning, the ACL here is set to public-read
img_data = open("static/" + image_name, "rb")
s3.Bucket(bucket).put_object(Key=image_name, Body=img_data,
ContentType="image/png", ACL="public-read")
# Generate the URL to get 'key-name' from 'bucket-name'
url = "http://" + bucket + ".s3.amazonaws.com/" + image_name
# this url should open the uploaded image as an image in a browser window
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment