skip to Main Content

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


  1. When using boto3 to upload a file, the format is:

    response = s3_client.upload_file(file_name, bucket, object_name)
    
    • file_name is the local file
    • bucket is the name of the Amazon S3 bucket (my_bucket_name)
    • object_name is the key of the Amazon S3 object

    Therefore, use:

    s3_client = boto3.client('s3')
    s3_client.upload_file(fig1_filename, 'my_bucket_name', 'fig1.png')
    

    Or, you can use keywords:

    s3_client.upload_file(Filename=fig1_filename, Bucket='my_bucket_name', Key='fig1.png')
    

    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

    Login or Signup to reply.
  2. You can save the image to a memory buffer, and upload that memory buffer directly to S3:

    import io
    
    # Save the output to a Bytes IO object
    png_data = io.BytesIO()
    fig1.savefig(png_data)
    # Seek back to the start so boto3 uploads from the start of the data
    bits.seek(0)
    
    # Upload the data to S3
    s3 = boto3.client('s3')
    s3.put_object(Bucket="example-bucket", Key="results.png", Body=png_data)
    
    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search