Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileNotFoundError with leading slash issue on AWS Lambda with Python

My development environment is a Windows machine. When trying to download a file from S3 locally it works no problem. When I load the function to Lambda, however, I receive a FileNotFoundError error which is caused by the Lambda requiring a leading slash in the file key.

This works locally, but does not on Lambda...

s3 = boto3.resource('s3')
new_file_key = os.path.join('tmp', file_name)
s3.Bucket('bucketname').download_file(file_key, new_file_key)

This works on Lambda, but not locally...

s3 = boto3.resource('s3')
new_file_key = os.path.join('/tmp', file_name)
s3.Bucket('bucketname').download_file(file_key, new_file_key)

What's the simplest way to handle this?

like image 717
TravisVOX Avatar asked Aug 23 '26 22:08

TravisVOX


1 Answers

It sounds like you want the file to be downloaded to

  • C:\tmp on windows
  • /tmp on your lambda container (linux)

Using this SO answer as a reference, the following should behave in a platform-agnostic manner:

s3 = boto3.resource('s3')
new_file_key = os.path.abspath(os.path.join(os.sep, 'tmp', file_name))
s3.Bucket('bucketname').download_file(file_key, new_file_key)
like image 87
Nicholas Sizer Avatar answered Aug 26 '26 10:08

Nicholas Sizer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!