获取错误 AWS Lambda:EROFS:只读文件系统,打开“/var/task/assets/docs.zip" [英] Getting error AWS Lambda : EROFS: read-only file system, open '/var/task/assets/docs.zip'
问题描述
任何人都可以帮助我解释为什么我会遇到这个问题我在本地运行这段代码它运行得很好但是在 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 call 做了一个基本任务,我只是将 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
这是我的function.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;
当我在 cloud watch 上看到日志时,我得到了:
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'结束请求 ID:720cf48f-01c4-11e9-b715-9d54f664a1e8
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
带有以下错误消息:
{"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
.
问:如果我的 AWS Lambda 函数需要磁盘上的暂存空间怎么办?
每个 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"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!