AWS lambda上没有模块zip

时间:2016-08-04 09:35:09

标签: amazon-web-services aws-lambda

尝试使用zip: command not found命令压缩文件夹时,在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模块?

1 个答案:

答案 0 :(得分:0)

运行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();