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

时间:2015-08-11 10:05:24

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

我在s3(公共)上有一些证书文件,我将在我的代码中下载并使用这些文件,如果我在本地的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上有一些特定问题,当一个人下载文件并使用它时,与其路径或其他内容有关,文件在下载时保留在哪里,实际上我甚至看不到下载文件的日志。

2 个答案:

答案 0 :(得分:37)

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

答案 1 :(得分:1)

请注意,截至去年(2020 年)Lambda 也支持 EFS 作为挂载,因此您可以写入 EFS 挂载点。对你的情况来说太过分了……但它可能对处理大文件的人有帮助https://aws.amazon.com/blogs/compute/using-amazon-efs-for-aws-lambda-in-your-serverless-applications/