尝试使用 zip
命令压缩文件夹时,在 AWS lambda 上出现错误 zip:找不到命令
:
const exec = require('child_process').exec;
exec('touch /tmp/test.txt', (error, stdout, stderr) => {
console.log(stdout);
})
exec('zip /tmp/test.zip /tmp/test.txt', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
} else {
console.log(stdout);
}
})
exec('ls /tmp/', (error, stdout, stderr) => {
console.log(stdout);
})
此外,当将 zip 放入 bin 文件夹中时,它会给出权限被拒绝的错误。如何在AWS lambda上安装zip模块?
最佳答案
运行 Lambda 函数的 Amazon Linux 服务器上未安装 zip
软件包。所以有两个选择:
1) 提供 zip 应用程序二进制文件作为上传到 Lambda 的函数包的一部分。 zip 应用程序需要在 Amazon Linux 上编译或静态链接。
2) 使用对二进制可执行文件没有任何依赖性的 Node.js 库。
我个人使用过选项#2,并且可以推荐此处提供的出色的“yazl”库:
https://github.com/thejoshwolfe/yazl
你最终会做这样的事情:
var yazl = require('yazl');
var zipfile = new yazl.ZipFile();
zipfile.addBuffer(fs.readFileSync('/tmp/file.txt'), "file.txt", {
mtime: new Date(),
mode: 0100664, // -rw-rw-r--
});
zipfile.outputStream.pipe(fs.createWriteStream('/tmp/test.zip')).on("close", function() {
console.log("done zipping files");
});
zipfile.end();
关于amazon-web-services - 模块 zip 在 AWS lambda 上不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38763668/