[英]Why does AWS Lambda Node.js example include javascript objects with keys like “S:” and “N:”?
[英]simple node.js example in aws lambda
我正在嘗試使用 aws lambda 發送一個簡單的請求。
我的模塊結構如下:
mylambda
|-- index.js
|-- node_modules
| |-- request
我將文件壓縮並上傳到 lambda。
然后我調用它,它返回以下錯誤。 "errorMessage": "Cannot find module 'index'"
這是 index.js 文件的內容
var request = require('request');
exports.handler = function(event, context) {
var headers = { 'User-Agent': 'Super Agent/0.0.1', 'Content-Type': 'application/x-www-form-urlencoded' }
// Configure the request
var options = {
url: 'https://myendpoint',
method: 'POST',
headers: headers,
form: {'payload': {"text":""} }
}
// Start the request
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
}
})
console.log('value1 =', event.key1);
context.succeed(event.key1); // Echo back the first key value
};
任何幫助表示贊賞,謝謝
現在一切正常,我不得不在高級設置中增加超時秒數,因為它花費的時間超過 3 秒。
此外,我還必須確保正確安裝了我的節點模塊。 在試圖找出問題所在時,我搞砸了請求模塊。
為了重新安裝模塊,我刪除了然后重新安裝了request 。
npm init
"request" : "*"
,npm install
。 壓縮 zip 並上傳,現在一切正常。 :)您只需壓縮和上傳子文件夾,而不是根文件夾。 您必須按照您的示例壓縮以下文件夾,然后上傳:
|-- index.js
|-- node_modules
|-- request
任務:編寫一個 aws lamda 函數:
我如何看到我們在做什么:
我們在 aws 編輯器中編寫代碼並運行它
沒有按預期運行,在那里放了很多控制台(因為我們無法調試我們的代碼)
等待幾秒鍾,然后在另一個窗口中看到控制台,不斷更改窗口,直到我們解決問題
4.更換窗戶需要大量的時間和精力。
為什么我們不能?
我們可以。
示例代碼:
let fs = require('fs'); const aws = require("aws-sdk"); const s3 = new aws.S3(), async = require('async'); aws.config = { "accessKeyId": "xyz", "secretAccessKey": "xyz", "region": "us-east-1" }; fs.readFile('path to your code file', 'utf-8', async (err, code) => { if (err) return res.status(500).send({ err }); async function uploadToS3(docs) { (only this function has to go into aws editor) let func = new Function('docs', "aws", "s3", 'async', `${code}`); return func(docs, aws, s3, async); } let resp = await uploa`enter code here`dToS3(req.files.docs);(this line will call aws lambda function from our server) return res.send({ resp }); });
我在文件中編寫的代碼:
docs = Array.isArray(docs) ? docs : [docs] let funArray = []; docs.forEach((value) => { funArray.push(function (callback) { s3.upload({ Bucket: "xxx", Body: value.data, Key: "anurag" + "/" + new Date(), ContentType: value.mimetype }, function (err, res) { if (err) { return callback(err, null); } return callback(null, res); }); }); }); return new Promise((resolve, reject) => { async.parallel(funArray, (err, data) => { resolve(data); }); });
益處:
筆記:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.