The error message "No such file or directory: '/tmp/tmpnj0aocz9'" indicates that the code is unable to find the temporary file that should contain the PDF content.
Does anyone have a solution or a reason why this may happen.
def save_pdf_to_s3(pdf, pdf_type, s3_bucket, s3_bucket_path):
s3 = boto3.client('s3')
# Create a temporary file to hold the PDF content
temp_file = tempfile.NamedTemporaryFile(delete=False)
try:
# Generate the PDF content as bytes and write it to the temporary file
pdf_content = pdf.output(dest='S').encode('latin1')
temp_file.write(pdf_content)
# Upload the temporary file to S3
s3.put_object(Key=f'{s3_bucket_path}/{pdf_type}.pdf', Body=open(temp_file.name, 'rb'), Bucket=s3_bucket)
print("PDF saved to S3")
finally:
# Close and remove the temporary file
temp_file.close()
I tried to make an temp file and save the content of the pdf to that file. only it crashes at this line, because output wants to save automatically to a dir that is not there.
pdf_content = pdf.output(dest='S').encode('latin1')
Here's is an updated script that should work. Basically because now we use the
withstatement to open files. In your case the path to the temporary file was probably incorrect since you did not use theos.path.abspath()function to get it formatted properly.Sample:
The updated script:
I hope this helps!