AWS Lambda – 下载文件,并将其用于相同的function – nodejs

我有一些证书文件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在下载文件并使用文件时遇到了一些特定的问题:与文件path有关,文件保存在何处,实际上我甚至没有看到文件日志下载。

       

网上收集的解决方案 "AWS Lambda – 下载文件,并将其用于相同的function – nodejs"