node.js - Lambda 函数错误 : EROFS: read-only file system, 打开 './tmp/test.zip' 进程在完成请求之前退出

标签 node.js amazon-s3 aws-lambda

I have download a zip file from s3 bucket then extracting the zip file and finally upload one file to s3 bucket in Lambda function using Node JS.But am getting the error

==> Error: EROFS: read-only file system, open './tmp/test.zip' "Process exited before completing> request"

exports.handler = function (callback) {

downloadZipFile(params, downloadPath, function (err) {
    if (err) {
        callback(err);
    } else {
        processZipFile(downloadPath, function (err) {
            if (err) {
                callback(err);
            } else {
                callback(null);
            }
        });

      }
  });

};

function downloadZipFile(params, downloadPath, callback) {

const file = fs.createWriteStream(downloadPath);

s3.getObject(params)
    .on('httpData', function (chunk) {

        file.write(chunk);
    })
    .on('success', function () {

        callback(null);
    })
    .on('error', function (err) {

        callback(err);
    })
    .on('complete', function () {

        file.end();
    })
    .send();
}

function processZipFile(filePath) {

const stats = fs.statSync(filePath)
const fileSizeInBytes = stats.size

if (fileSizeInBytes > 0) {

    var srcPath = filePath;
    var destPath = "./tmp";
    targz.decompress({
        src: srcPath,
        dest: destPath

    }, function (err) {
        if (err) {
            console.log(err);
        } else {
            console.log("Done!");

            UploadFile();
        }

    });
  }
}

function UploadFile() {

var body = fs.createReadStream('./tmp/SampleFile.txt')

var srcfileKey = "SampleFile.txt";
// Upload the stream
var s3obj = new AWS.S3({ params: { Bucket: bucketName, Key: srcfileKey } });
s3obj.upload({ Body: body }, function (err, data) {
    if (err) {
        console.log("An error occurred", err);
    }

    console.log("Uploaded the file at", data.Location);
 })
}

最佳答案

您需要将文件路径更改为/tmp 而不是./tmp。 Lambda 只允许你写入 /tmp 目录。

关于node.js - Lambda 函数错误 : EROFS: read-only file system, 打开 './tmp/test.zip' 进程在完成请求之前退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43389724/

相关文章:

php - Laravel 和 MySQL : Duplicate entry inside loop and error on mass insertion to table

go - 如何通过使用aws-sdk-go获取S3返回的xml格式错误响应?

python - 将 CSV 从 S3 上传到 AWS Lambda 时出错 : TypeError: 'module' object is not callable

aws-lambda - 如何使用无服务器的 serverless-aws-documentation 插件生成带有标签的 swagger 文档

node.js - 拒绝后在 catch block 中查询

python - NVM 无法在 Cygwin 中安装任何版本的 Node.js

javascript - 运行 "npm publish"时出现 404 错误

python - 从 AWS Lambda 连接到 Redshift

javascript - 为什么我得到 process.stdin undefined

javascript - 使用 .each() 从 JS 对象中获取特定项目