Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

node.js - Getting error AWS Lambda : EROFS: read-only file system, open '/var/task/assets/docs.zip'

Can any one help me that why i got this issue I run this code locally it runs perfectly but at aws lambda i got this error even i increase the time over lambda time out function as well memory.

In this code i do a basic task for get call i just convert a xlsx to json and in post i just convert a test dir to zip file.I tried it from last few hrs for uploading at aws lambda now I am stuck and seeing continously this error can anyone help me out from this situation thanks in advance.

here is my code

index.js

"use strict"
const fs = require("fs");
const path = require("path");
const ctrlFuns = require("./functionality");
const output = fs.createWriteStream(path.join(__dirname, 
"./assets/docs.zip"));
const archiver = require("archiver");
const zipArchive = archiver("zip", {
gzip: true,
zlib: {
    level: 9
} // Sets the compression level.
});

exports.handleHttpRequest = function (event, context, callback) {

  if (event.http_method == "GET") {
    ctrlFuns.xlsxToJson().then((jsonObjs) => {
        callback(null, {
            users: jsonObjs,
        });
    }).catch((err) => {
        callback(err);
    });
} 
else if (event.http_method == "POST") {
    fs.readFile(path.join(__dirname + "/test/test.xlsx"), (err, file) => {
        if (err) {
            callback(err);
        } else {

            //pipe archive data to the file
            zipArchive.pipe(output);

            zipArchive.append(file, {
                name: "test.xlsx",
                prefix: "test-data" //used for folder name in zip file
            });

            // to catch this error explicitly
            zipArchive.on("error", (err) => {
                callback(err);
            });


            //to perform end tasks while zip converted
            zipArchive.on("end", () => {
                fs.readFile(path.join(__dirname + "/assets/docs.zip"), (err, 
    success) => {
                    if (err) {
                        callback(err);
                    } else {
                        callback(null, success.toString("base64"));
                    }
                });
            });
            //filnalizing the zip file for user use
            zipArchive.finalize();
        }
    });
} 
else {
    callback(null, "run default case");
}
} //handler-closes

here is my functionality.js

/**
 * OBJECTIVE: TO CREATE THE FUNCTINALITY
 */
"use strict"

const XLSX = require("xlsx");
const fs = require("fs");
const path = require("path");


var ctrlFuns = {};

ctrlFuns.xlsxToJson = function () {
 return new Promise((resolve, reject) => {
    fs.readFile(path.join(__dirname + "/test/test.xlsx"), (err, file) => {
        if (err) {
            reject(err);
        } else {
            let workbook = XLSX.read(file.buffer, {
                type: "buffer"
            });

            //if workbook is null
            if (!workbook) {
                reject("Workbook not found.");
            }

            /* Getting first workbook sheetName */
            let first_sheet_name = workbook.SheetNames[0];

            /* Get worksheet */
            let worksheet = workbook.Sheets[first_sheet_name];

            /**Convert Into JSON */
            resolve(XLSX.utils.sheet_to_json(worksheet, {
                raw: true
            }));
        }
    });
})

 } //fun-closes

 module.exports = ctrlFuns;

when I saw the logs at cloud watch then i got:

START RequestId: 720cf48f-01c4-11e9-b715-9d54f664a1e8 Version: $LATEST 2018-12-17T06:24:45.756Z 720cf48f-01c4-11e9-b715-9d54f664a1e8 Error: EROFS: read-only file system, open '/var/task/assets/docs.zip' END RequestId: 720cf48f-01c4-11e9-b715-9d54f664a1e8

with below error message:

{ "errorMessage": "RequestId: 98b9e509-01c7-11e9-94dc-03cfdf0dae93 Process exited before completing request" }

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The error seems self-explanatory:

Error: EROFS: read-only file system, open '/var/task/assets/docs.zip' 

/var/task is where your Lambda function code is located, and in the actual Lambda environment, that filesystem is read-only. If you need to write to a file, you need to write to /tmp.

Q: What if I need scratch space on disk for my AWS Lambda function?

Each Lambda function receives 500MB of non-persistent disk space in its own /tmp directory.

https://aws.amazon.com/lambda/faqs/

Note that you also need to clean up after yourself and remove any temporary files you created, because once a function finishes executing, its container is available for reuse by a later invocation of the same function... which means this same temp space may persist for a short time and be seen again (but only by this same function).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...