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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With