Skip to content
Advertisement

AWS s3, lambda. How do you download image from tmp that has a prefix?

I am currently learning AWS, mostly s3 and lambda services. The idea is to save an image in one bucket, resize it and move to another bucket. I have searched for dozen tutorials and finally made it work. However, I have not found(or don`t know how to search) for an example of how to deal with images with prefixes.

This is the code I am using:

JavaScript

It all works perfectly if my image is named just ‘test.jpg’. However, my real images are stored in multiple directories seperated by year, month, day. And it looks something like this: ‘2020/06/10/test.jpg’. But even if I upload an image with one prefix, for example: ‘test/test.jpg’ and try to use my resize function, I get this error:

JavaScript

Obviously this is not correct, because tmp folder does not have folders itself. But how do I get the image then? I tried using just the image name to check if the file exists like this:

JavaScript

Obviously the image does not exist:

JavaScript

So what is the correct solution to this problem? I am fairly new to this whole AWS thing and getting stuck constantly… Its starting to get really annoying and im losing hope.

Advertisement

Answer

The syntax for upload_file() is:

JavaScript

Therefore, this line in your code:

JavaScript

should be:

JavaScript

So, I’m surprised that your code is working at all, even with no directories.

The reason that your code is failing with multiple directories is due to these lines:

JavaScript

If the key is foo/bar then it will attempt to download a file to /tmp/foo/bar. However, the /tmp/foo directory does not exist. Unlike Amazon S3, operating systems normally want a directory to exist before writing a file to that location.

I notice that your code is based on Tutorial: Using an Amazon S3 trigger to create thumbnail images – AWS Lambda. In that sample code, you’ll notice that it contains this line:

JavaScript

This removes subdirectories from the path, thus avoiding the problem.

Advertisement