I can’t figure out how to write images to my s3 bucket. I use matplotlib and
import pandas as pd
import boto3
import ploty.express as px
path = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/monthly-car-sales.csv'
data = pd.read_csv(path)
data.rename(columns = {'Month':'ds', 'Sales':'y'}, inplace=True)
fig1 = data.plot()
#type(fig1)
###
fig2=px.line(data, x='ds', y='y')
type(fig2)
I tried to do fig1.savefig('s3://my_bucket_name/fig1.png')
and I get
‘no such file or directory s3://my_bucket…’
If i do something like data.to_csv('s3://my_bucket_name/data.csv')
my file gets written just fine. I’ve tried a variety of things with plotly using
s3_client = boto3.client('s3')
s3_client.upload_file(fig1, key='s3://my_bucket_name/fig1.png')
But I can’t get this to work either.
3
Answers
When using boto3 to upload a file, the format is:
file_name
is the local filebucket
is the name of the Amazon S3 bucket (my_bucket_name
)object_name
is the key of the Amazon S3 objectTherefore, use:
Or, you can use keywords:
Note that the value for
Filename
needs to be the name of a file on the disk, so you will need to save the data to disk first and then provide the name of that file. (It is possible to directly provide the data, but it is more complex due to the need to convert to the right data types.)See: upload_file – Boto3 documentation
You can save the image to a memory buffer, and upload that memory buffer directly to S3:
Refer to the many AWS Python Developer Tutorials in the AWS Code Library. This document contains many step by step dev instructions and functioning code in Github that you can use to learn concepts like this.
For your use case, i recommend looking at this doc.
Detect objects in images with Amazon Rekognition using the AWS SDK for Python (Boto3)
Among the concepts that you will learn are:
Upload photos to an Amazon Simple Storage Service (Amazon S3) bucket.
Use Amazon Rekognition to analyze and label the photos.
Use Amazon Simple Email Service (Amazon SES) to send email reports of
image analysis.