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

Aditya K
  • 135
  • 2
  • 4
  • 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. – Jignesh M. Khatri Sep 09 '22 at 05:00
  • the file will be uploaded to S3, once it's uploaded the file will be deleted – Aditya K Sep 09 '22 at 05:01
  • You can always upload byte array to S3 instead of file. No need to create a file. You can refer it here - https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/examples-s3-objects.html#upload-object – Jignesh M. Khatri Sep 09 '22 at 05:03
  • But is it possible to stream a List to InputStream? – Aditya K Sep 09 '22 at 05:16
  • Yes, it can be serialized. Check here - https://stackoverflow.com/questions/5618978/convert-arrayliststring-to-byte – Jignesh M. Khatri Sep 09 '22 at 05:20
  • 1
    Does this answer your question? [Error "Read-only file system" in AWS Lambda when downloading a file from S3](https://stackoverflow.com/questions/39383465/error-read-only-file-system-in-aws-lambda-when-downloading-a-file-from-s3) – stdunbar Sep 09 '22 at 13:07
  • So I tried writing it directly to a the path /tmp/Some_File.txt. got the exception while uploading to S3, no such file or directory. It seems like the required contents were written to this path but when I'm trying to upload a file from this path to S3 I'm getting this exception – Aditya K Sep 09 '22 at 13:34
  • I'm adding the stack trace in the update – Aditya K Sep 09 '22 at 13:42
  • 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. – stdunbar Sep 09 '22 at 15:02
  • My mistake, I had commented out the code where I write contents to the file, fixed that and it's working as expected. – Aditya K Sep 10 '22 at 18:35

0 Answers0