amazon-web-services - 模块 zip 在 AWS lambda 上不可用

标签 amazon-web-services aws-lambda

尝试使用 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/

相关文章:

node.js - 使用 AWS Lambda 函数和 Node.js 从 S3 存储桶中提取 zip 文件并上传到另一个存储桶

ruby-on-rails - 我可以安全地将 Amazon 的 Elasticsearch 与 Rails searchkick gem 一起使用吗?

grails - 从Grails获取S3上的图像时“权限被拒绝”

amazon-web-services - AWS CLI : Get parent user details after sts-assume-role

amazon-s3 - 使用 CloudFormation 在 S3 存储桶中创建 Lambda 通知

java - 在部署 AWS Lambda 时设置 bean 属性 'mongoTemplate' 时无法解析对 bean 'mongoOperations' 的引用

amazon-web-services - docker 不创建文件

mysql - 使用mysqldump导入到RDS

php - 实例化从 AwsClient 子类扩展的类时出错

javascript - 在 Javascript 中将字符从 ISO-8859-1 转换为 UTF-8 编码时保留字符表示