Home > Back-end >  How can I download an image from an AWS bucket to generate a PDF from, using FPDF?
How can I download an image from an AWS bucket to generate a PDF from, using FPDF?

Time:10-18

I have a FPDF object like this:

import fpdf

pdf = FPDF()

#Cover page
pdf.add_page()
pdf.set_font('Arial', 'B', 16)
pdf.cell(175, 10, 'TEST - report',0,1,'C')

I'm trying to use a .png file from AWS S3 bucket directly to generate a PDF, like so:

pdf.image(bucket_folder_name   '/'   file_name   '.png')

Unfortunately, I get the error:

[Errno 2] No such file or directory

What is the issue?

CodePudding user response:

To use files from an S3 bucket, you need to download them first - an S3 bucket is not like a local folder for you to be able to provide a path and use the file.

Download the file first using download_file and then pass the filename to pdf.image(...).

The download_file method has the following parameters:

s3.download_file('BUCKET_NAME', 'OBJECT_NAME', 'FILE_NAME')

Something like this:

import boto3
s3 = boto3.client('s3')
...

filename_with_extension = file_name   '.png';
outputPath = '/tmp/'   filename_with_extension;

s3.download_file(bucket_folder_name, filename_with_extension, outputPath)

pdf.image(outputPath)

Important note: you must write to the /tmp directory as that is the only available file system that AWS permits you to write to (and read from).

Any other path will result in a [Errno 13] Permission denied error to indicate this.

Per docs:

Your function code can access a writable /tmp directory storage of 512 MB.

  • Related