4

I have written AWS Lambda code where I need to store an image in /tmp location of aws lambda. Below is my code:

String fileLocation = "loc1/loc2/";
String imageNameWithoutExt = "image1";
//creating directories first below storing the image
boolean status = new File("/tmp/"+fileLocation).mkdirs();
if(status == true){
File targetFile = File.createTempFile(imageNameWithoutExt,".jpg",new File("/tmp/"+fileLocation));
    FileOutputStream outStream = new FileOutputStream(targetFile);
    outStream.write(buffer);
    outStream.close();
}else{
    System.out.println("unable to create directory inside /tmp/");
}

And in response, it is printing the else statement:

unable to create directory inside /tmp/

What modification I need to make to write and read the files from /tmp location. Any help would be appreciated.

1 Answer 1

3

In this line of code, you are not setting the filename:

//write file in /tmp folder of aws Lambda
File targetFile = new File("/tmp/");

I think maybe you aren't showing all your code, because I don't see where the String image1.jpg in the error message would be coming from, but that filename needs to be added to the parameter you are passing the File constructor.

Sign up to request clarification or add additional context in comments.

7 Comments

Hi, Thanks for the prompt reply. I have edited the code as required. Also, I'm getting the fileNotFoundException for the same line as you pointed out, but I'm providing the file name in the argument. Even then I'm getting the error.
Basically the issue is that I'm not able to write file inside /tmp folder.
I suggest doing some very basic debugging first. Try printing/logging the value of "/tmp/"+fileNameForDirUpload and verify it looks correct. I think you may have too many slashes in the combined String.
The printed value for "/tmp/"+fileNameForDirUpload is absolutely correct. Still not able to write any file inside /tmp. I have edited the code again to make the things simplify. If you can have a look and figure out the issue. Help will be appreciated.
I found that AWS mount tmp, and only allocate 1M to it. How amazon manage to get top rating is beyond me, but it seems I am surrounded by the flat earth society.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.