0

I have a spring boot application that is being exported as a .zip file and used as a lambda function. I'm generating a file and adding content to it.

file = new File(fileName);// fileName is just a String.txt
context.getLogger().log("File Path1: "+file.getAbsolutePath());
FileUtils.writeLines(file, failedMessages); //failedMessages is a List<String>
context.getLogger().log("File Path2: "+file.getAbsolutePath());

The logs:

File Path1: /var/task/Platform_Status_Failed_2022_09_09.txt

Exception caught java.io.FileNotFoundException: Platform_Status_Failed_2022_09_09.txt (Read-only file system)

Not sure how to fix this. Should I create a temporary folder and save the file and write to that path or is there any other simpler fix?

Update:

Tried using the /tmp path of lambda. As per my logs, it seems that the contents are written to this location but when I'm trying to upload this file to S3 I get an Exception:

Unable to calculate MD5 hash: /tmp/Platform_Status_Failed_2022_09_09.txt (No such file or directory): java.lang.BootstrapMethodError
java.lang.BootstrapMethodError: Unable to calculate MD5 hash: /tmp/Platform_Status_Failed_2022_09_09.txt (No such file or directory)
    at com.paytmmoney.bo.equities.lambda.handler.LambdaMethodHandler.uploadToS3(LambdaMethodHandler.java:171)
    at com.paytmmoney.bo.equities.lambda.handler.LambdaMethodHandler.uploadFileToS3(LambdaMethodHandler.java:88)
    at com.paytmmoney.bo.equities.lambda.handler.LambdaMethodHandler.handleRequest(LambdaMethodHandler.java:58)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
Caused by: com.amazonaws.SdkClientException: Unable to calculate MD5 hash: /tmp/Platform_Status_Failed_2022_09_09.txt (No such file or directory)
    at com.amazonaws.services.s3.AmazonS3Client.getInputStream(AmazonS3Client.java:1849)
    at com.amazonaws.services.s3.AmazonS3Client.uploadObject(AmazonS3Client.java:1767)
    at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1746)
    at com.paytmmoney.bo.equities.lambda.handler.LambdaMethodHandler.uploadToS3(LambdaMethodHandler.java:159)
    ... 6 more
Caused by: java.io.FileNotFoundException: /tmp/Platform_Status_Failed_2022_09_09.txt (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at com.amazonaws.util.Md5Utils.computeMD5Hash(Md5Utils.java:97)
    at com.amazonaws.util.Md5Utils.md5AsBase64(Md5Utils.java:104)
    at com.amazonaws.services.s3.AmazonS3Client.getInputStream(AmazonS3Client.java:1845)
    ... 9 more

10
  • But why you need to write a file in Lambda env? Lambda is not a persistent layer, it will switch off once your task is completed and the data on it will be vanished. Commented Sep 9, 2022 at 5:00
  • the file will be uploaded to S3, once it's uploaded the file will be deleted Commented Sep 9, 2022 at 5:01
  • You can always upload byte array to S3 instead of file. No need to create a file. You can refer it here - docs.aws.amazon.com/sdk-for-java/latest/developer-guide/… Commented Sep 9, 2022 at 5:03
  • 1
    Does this answer your question? Error "Read-only file system" in AWS Lambda when downloading a file from S3 Commented Sep 9, 2022 at 13:07
  • 1
    The file is obviously not written where you think it as it cannot be opened. We really need a bit more code to understand what's going on. Commented Sep 9, 2022 at 15:02

0

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.