[PYTHON] Addictive points when downloading files using boto on AWS Lambda

The problem that a mysterious character string comes along

in this way

s3 = boto3.client("s3")
pdf_filepath = 'tmp/' + key.split('/')[-1]
print(pdf_filepath)
# => tmp/20191016101246759.pdf
s3.download_file(bucket, key, pdf_filepath)

If you run the code that downloads the file from S3 [ERROR] FileNotFoundError: [Errno 2] No such file r directory: 'tmp/20191016101246759.pdf.47cf5CFA' A mysterious character string is attached like this, and it cannot be downloaded due to an error.

File restrictions that can be changed in Lambda

At Lambda -Only files under / tmp can be changed </ b> - You cannot create a hierarchical structure under / tmp (you cannot create a directory under / tmp) </ b>

There is a limitation. Therefore, if you try to download to a location other than / tmp, or if you try to download by separating into directories such as /tmp/food/chocolate.jpg, an error will occur.

Solution

In this case, I'm trying to save to tmp /

s3 = boto3.client("s3")
pdf_filepath = '/tmp/' + key.split('/')[-1]
print(pdf_filepath)
# => /tmp/20191016101246759.pdf
s3.download_file(bucket, key, pdf_filepath)

I was able to download it safely by specifying it directly under / tmp.

Recommended Posts