【问题标题】:AWS Lambda - downloading a file, and using it in the same function - nodejsAWS Lambda - 下载文件,并在同一个函数中使用它 - nodejs
【发布时间】:2015-11-03 12:07:11
【问题描述】:

我有一些通过 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 上,当一个人下载和使用文件时,是否存在一些特定的问题,与它的路径或其他东西有关,当文件被下载时,它们会保存在哪里,实际上我什至没有看到日志下载文件的数量。

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2023-01-24
        • 1970-01-01
        • 2020-05-18
        • 2019-03-08
        • 2022-10-15
        • 1970-01-01
        • 2018-05-27
        • 1970-01-01
        • 2019-04-20
        相关资源
        最近更新 更多