Friday, April 15, 2022

[SOLVED] How can I download an image from an AWS bucket to generate a PDF from, using FPDF?

Issue

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?


Solution

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:

You can configure each Lambda function with its own ephemeral storage between 512MB and 10,240MB, in 1MB increments. The ephemeral storage is available in each function’s /tmp directory.



Answered By - Ermiya Eskandary
Answer Checked By - Marie Seifert (WPSolving Admin)