node.js - AWS Lambda - 下载文件,并在同一个函数中使用它 - nodejs

标签 node.js amazon-web-services amazon-s3 aws-lambda

我有一些通过 s3(公共(public))的证书文件,我将在我的代码中下载和使用这些文件,如果我在本地的 nodejs 中编写等效代码,它运行良好,但在 AWS lambda 中它只是崩溃.

var apn = require('apn');
var https = require('https');
var fs = require('fs');

exports.handler = function(event, context) {
  console.log("Running aws apn push message function");
  console.log("==================================");
  console.log("event", event);

  var certPath = event.certPath;
  var keyPath = event.keyPath;
  var certFileName = event.certFileName;
  var keyFileName = event.keyFileName;
  var passphrase = event.passphrase;
  var apnId = event.apnId;
  var content = event.content;


var certfile = fs.createWriteStream(certFileName);
var certrequest = https.get(certPath, function(certresponse) {
  certresponse.pipe(certfile);
  console.log("downloaded the certificate");

  var keyfile = fs.createWriteStream(keyFileName);
  var keyrequest = https.get(keyPath, function(keyresponse) {
    keyresponse.pipe(keyfile);
    console.log("downloaded the key file");


  var options = { 
                      "cert":certFileName,
                      "key":keyFileName,
                      "passphrase":passphrase,
                      "batchFeedback": true,
                      "interval": 10
                      };

  var apnConnection = new apn.Connection(options);

  var myDevice = new apn.Device(apnId);
  var note = new apn.Notification();
  note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.        
  note.payload = {'COMMAND': content};       
  apnConnection.pushNotification(note, myDevice);
  console.log('message sent to ' + apnId);       

  context.done();

  });
});
}

我得到的错误与我想的访问文件有关 -

events.js:72 
 throw er; // Unhandled 'error' event 
 ^ 
Error: EACCES, open 'PushChatCert.pem'

因此,在 AWS Lambda 上,当一个人下载和使用文件时,是否存在一些特定的问题,与它的路径或其他东西有关,当文件被下载时,它们会保存在哪里,实际上我什至没有看到日志下载文件的数量。

最佳答案

您可以在 Lambda 中写入的唯一可用本地文件系统是/tmp,因此请确保您尝试写入的本地文件的路径位于/tmp 目录中,并且您应该已准备就绪。

关于node.js - AWS Lambda - 下载文件,并在同一个函数中使用它 - nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31938828/

相关文章:

node.js - 如何在 MongoDB 上为部分文本搜索创建索引?

javascript - 如何使用 Node.js 发出 https 请求? - Myscript 在 http.get 中产生 TypeError

amazon-web-services - 如何在AWS中删除弹性IP地址

amazon-web-services - 带 https 连接的 Amazon S3 SDK

python - 有什么方法可以使用 boto3 将文件直接写入 S3?

ios - Amazon iOS SDK S3 上传分段请求因神秘 URL 失败

javascript - 如何列出 MongoDB 数组内对象中的字段?

javascript - 在 package.json 中设置 process.env 变量

amazon-web-services - AWS CloudFormation 模板 - 如何使用 SQS 队列配置 Lambda 以从队列中选取项目

node.js - 从 Node lambda 调用时未从 AWS RDS 获取任何结果