Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create a file in my S3 lambda function?

I am creating a java function for AWS Lambda that brings in a file from AWS S3 like this:

InputStream videoObjectStream = awsS3Video.getObjectContent();

I am also utilizing FFmpegFrameGrabber, which requires that I specify a file path whenever I create a new frameGrabber, i.e.: FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(filePath)

I am trying to convert the InputStream into a temporary file in my Lambda function, but it is not allowing me to create a file. Here is my code to convert the videoObjectStream into a file:

byte[] inputBuffer = null;

        try {
            inputBuffer = IOUtils.toByteArray(videoObjectStream);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        System.out.println("The length of the byte array is " + inputBuffer.length);


        try {
            FileOutputStream videoOS = new FileOutputStream(videoDetails.get("videoFileKey"), false);
            videoOS.write(inputBuffer);
            videoOS.close();
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        File tempVideoFile = new File(videoDetails.get("videoFileKey"));

        if (tempVideoFile.exists()) {
            System.out.println("The file exists");
        } else {
            System.out.println("The file does not exist");
        }

Then, I get the following stack trace, saying that this is a read-only file system:

java.io.FileNotFoundException: currentPath1490660005410.mp4 (Read-only file system)
    at java.io.FileOutputStream.open0(Native Method)
    at java.io.FileOutputStream.open(FileOutputStream.java:270)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:133)
    at com.amazonaws.lambda.LambdaFunctionHandler.convertVideo(LambdaFunctionHandler.java:67)
    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)
    at lambdainternal.EventHandlerLoader$PojoMethodRequestHandler.handleRequest(EventHandlerLoader.java:456)
    at lambdainternal.EventHandlerLoader$PojoHandlerAsStreamHandler.handleRequest(EventHandlerLoader.java:375)
    at lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:1139)
    at lambdainternal.AWSLambda$2.call(AWSLambda.java:94)
    at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:290)
    at lambdainternal.AWSLambda.<clinit>(AWSLambda.java:57)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at lambdainternal.LambdaRTEntry.main(LambdaRTEntry.java:94)

Is there any way around this? I need to manipulate the video data but cannot without making it into a file first. Any suggestions are welcome. Thank you.

like image 348
hermt2 Avatar asked Mar 31 '17 16:03

hermt2


People also ask

Can AWS Lambda create files?

Create the file for the function you update and deploy later in this tutorial. A Lambda function can use any runtime supported by AWS Lambda. For more information, see AWS Lambda runtimes. Create a text file and save it as myDateTimeFunction.

Can I store a file in Lambda?

With increased AWS Lambda ephemeral storage, you get access to a secure, low-latency ephemeral file system up to 10 GB. You can continue to use up to 512 MB for free and are charged for the amount of storage you configure over the free limit for the duration of invokes.


1 Answers

You have to create the file in /tmp. That's the only location you are allowed to write to in the Lambda environment.

like image 133
Mark B Avatar answered Oct 14 '22 17:10

Mark B