Empty file is uploaded to S3 bucket when trying to save matplotlib piechart there

Viewed 354

So i want to create a matplotlib pie chart using some data then save it to BytesIO and send to my S3 bucket to store and use this picture later. No errors appear during the operation, its successfully uploaded on my S3 with correct name but the file's size is 0 and it is completely empty, though buf is not empty if i check it via print() before uplaod.

async def generate_pie_chart():
    slices = [33,33,33]
    fig = Figure()
    pie = fig.subplots()
    pie.pie(slices)

    buf = BytesIO()
    fig.savefig(buf, format="png")

    S3_CLIENT.upload_fileobj(buf, S3_BUCKET, 'piee')
1 Answers

Just add the following line right above upload_fileobj():

buf.seek(0)

It moves the "cursor" to the beginning of the BytesIO. When upload_fileobj() is called, it can read/upload from the beginning of the file.

Related