I'm trying to generate a PDF using reportlab and write to s3 bucket without 
saving locally

I know I'm missing something simple:

# create a stream
stream = io.BytesIO()

# generate PDF
doc = SimpleDocTemplate(stream, pagesize=letter,
                        rightMargin=72, leftMargin=72,
                        topMargin=72, bottomMargin=18)
Story = []

styles = getSampleStyleSheet()

Story.append(Paragraph('<font size=11>This is a PDF</font>', 
styles["Normal"]))

doc.build(Story)

# get buffer
pdf_buffer = stream.getbuffer()

filename = "new.pdf"
bucket_name = 'insert_bucket_name'
object_name = bucket_name

# here is where I get stuck - how should be passing the pdf_buffer to s3?

# how you typically write to s3 :
# Method 1

s3 = boto3.client('s3')
with open(filename, "rb") as f:
    s3.upload_fileobj(f, bucket_name, object_name)

# Method 2
s3.Bucket(bucket_name).put_object(Key=filename, Body=file)

Any ideas most appreciated!

Thanks!

here are some related resources:

https://stackoverflow.com/questions/43373006/django-reportlab-save-generated-pdf-directly-to-filefield-in-aws-s3
https://sciwiki.fredhutch.org/compdemos/aws-python/#about-pandas-and-dask
https://stackoverflow.com/questions/12570465/how-to-upload-a-file-to-s3-without-creating-a-temporary-local-file

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/aea27da8-0c37-4e4e-9f0a-929ca85e34bb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to