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