问题描述
有人能帮我解决这个问题的原因吗?我在本地运行此代码可以完美运行,但是在aws lambda上我得到了这个错误,甚至我增加了lambda超时功能以及内存的时间.
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.
在这段代码中,我执行了get调用的基本任务,我只是将xlsx转换为json,而在发布后,我只是将测试目录转换为zip文件.我尝试了最近几个小时在aws lambda上载它,现在我被卡住了并不断看到这个错误,任何人都可以从这种情况下帮助我,在此先感谢.
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.
这是我的代码
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
这是我的functional.js
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版本:$ LATEST2018-12-17T06:24:45.756Z 720cf48f-01c4-11e9-b715-9d54f664a1e8错误:EROFS:只读文件系统,打开'/var/task/assets/docs.zip'END RequestId:720cf48f-01c4-11e9-b715-9d54f664a1e8
START RequestId: 720cf48f-01c4-11e9-b715-9d54f664a1e8 Version: $LATEST2018-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进程在完成请求之前已退出"}
{ "errorMessage": "RequestId: 98b9e509-01c7-11e9-94dc-03cfdf0dae93 Process exited before completing request"}
推荐答案
错误似乎不言自明:
Error: EROFS: read-only file system, open '/var/task/assets/docs.zip'
/var/task
是Lambda函数 code 所在的位置,在实际的Lambda环境中,该文件系统是只读的.如果需要写入文件,则需要写入/tmp
.
/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
.
每个Lambda函数在其自己的/tmp目录中接收500MB的非永久磁盘空间.
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).
这篇关于获取错误AWS Lambda:EROFS:只读文件系统,打开'/var/task/assets/docs.zip'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!