不进入 AWS HttpClient.handleRequest 在 lambda 和 Nodejs 中进行弹性搜索
Not going into AWS HttpClient.handleRequest to elasticsearch in lambda, Nodejs
我知道基本上有人提出并回答了同样的问题,但是,尝试实施答案并没有得到解决。这是原始问题:
我尝试将 async/await 放在请求的多个不同部分,但其中 none 的工作方式与 link.[=15 中的其中一条评论中提到的一样=]
情况是我有一个 lambda 函数来侦听 S3 存储桶中的事件,当事件发生时它应该为 elasticsearch 服务中的文档编制索引。将 PUT 请求发送到 es 时会发生此问题。
我已经使用 S3 存储桶完成了测试事件并且它有效,但由于某种原因,当我 运行 一个实际事件到我的 S3 存储桶时,它会 hang/not 进入 handleRequest 函数。
这是我的代码:
Index.js
const AWS = require('aws-sdk');
const s3 = new AWS.S3()
const elastic_client = require('elastic.js');
exports.handler = async (event, context) => {
const Bucket = event.Records[0].s3.bucket.name;
const Key = event.Records[0].s3.object.key;
const data = await s3.getObject({ Bucket, Key }).promise();
for (const quote_doc of data.Body) {
elastic_client.indexQuote(quote_doc);
}
}
elastic.js
var AWS = require('aws-sdk');
require('dotenv').config();
var region = process.env.AWS_REGION;
var domain = process.env.AWS_ELASTIC_DOMAIN;
function indexQuote(quote) {
var endpoint = new AWS.Endpoint(domain);
var request = new AWS.HttpRequest(endpoint, region);
var index = 'quotes';
var type = '_doc';
var id = quote.QuoteId;
request.method = 'PUT';
request.path += index + '/' + type + '/' + id;
request.body = JSON.stringify(quote);
request.headers['host'] = domain;
request.headers['Content-Type'] = 'application/json';
request.headers['Content-Length'] = Buffer.byteLength(request.body);
var credentials = new AWS.EnvironmentCredentials('AWS');
credentials.accessKeyId = process.env.AWS_ACCESS_KEY_ID;
credentials.secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY;
var signer = new AWS.Signers.V4(request, 'es');
signer.addAuthorization(credentials, new Date());
var client = new AWS.HttpClient();
client.handleRequest(request, null, function(response) { // Here is where it gets hung up
console.log(response.statusCode + ' ' + response.statusMessage); // Never outputs this
var responseBody = '';
response.on('data', function (chunk) {
responseBody += chunk;
});
response.on('end', function (chunk) {
console.log('Response body: ' + responseBody);
});
}, function(error) {
console.log('Error: ' + error);
});
}
让我感到困惑的是,当我做一个测试事件时它工作正常,当我在我自己的计算机上本地索引它时它工作正常,但只是不进入 handleRequest。任何 help/direction 不胜感激,谢谢。
编辑:
package.json
{
"dependencies": {
"aws-sdk": "*",
"aws-xray-sdk": "^3.2.0",
"dotenv": "^8.2.0"
}
}
尝试将 handleRequest 函数包装在 Promise 中。你的函数 indexQuote() 看起来几乎一样,但最后它会 return 一个 Promise
function indexQuote(quote) {
...
return new Promise((resolve, reject) => {
client.handleRequest(request, null,
response => {
const { statusCode, statusMessage, headers } = response;
let body = '';
response.on('data', chunk => {
body += chunk;
});
response.on('end', () => {
const data = {
statusCode,
statusMessage,
headers
};
if (body) {
data.body = body;
}
resolve(data);
});
},
err => {
reject(err);
});
});
然后您可以等待并检查结果:
const result = await indexQuote(quote);
console.log("Index result: " + result);
我知道基本上有人提出并回答了同样的问题,但是,尝试实施答案并没有得到解决。这是原始问题:
我尝试将 async/await 放在请求的多个不同部分,但其中 none 的工作方式与 link.[=15 中的其中一条评论中提到的一样=]
情况是我有一个 lambda 函数来侦听 S3 存储桶中的事件,当事件发生时它应该为 elasticsearch 服务中的文档编制索引。将 PUT 请求发送到 es 时会发生此问题。 我已经使用 S3 存储桶完成了测试事件并且它有效,但由于某种原因,当我 运行 一个实际事件到我的 S3 存储桶时,它会 hang/not 进入 handleRequest 函数。 这是我的代码:
Index.js
const AWS = require('aws-sdk');
const s3 = new AWS.S3()
const elastic_client = require('elastic.js');
exports.handler = async (event, context) => {
const Bucket = event.Records[0].s3.bucket.name;
const Key = event.Records[0].s3.object.key;
const data = await s3.getObject({ Bucket, Key }).promise();
for (const quote_doc of data.Body) {
elastic_client.indexQuote(quote_doc);
}
}
elastic.js
var AWS = require('aws-sdk');
require('dotenv').config();
var region = process.env.AWS_REGION;
var domain = process.env.AWS_ELASTIC_DOMAIN;
function indexQuote(quote) {
var endpoint = new AWS.Endpoint(domain);
var request = new AWS.HttpRequest(endpoint, region);
var index = 'quotes';
var type = '_doc';
var id = quote.QuoteId;
request.method = 'PUT';
request.path += index + '/' + type + '/' + id;
request.body = JSON.stringify(quote);
request.headers['host'] = domain;
request.headers['Content-Type'] = 'application/json';
request.headers['Content-Length'] = Buffer.byteLength(request.body);
var credentials = new AWS.EnvironmentCredentials('AWS');
credentials.accessKeyId = process.env.AWS_ACCESS_KEY_ID;
credentials.secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY;
var signer = new AWS.Signers.V4(request, 'es');
signer.addAuthorization(credentials, new Date());
var client = new AWS.HttpClient();
client.handleRequest(request, null, function(response) { // Here is where it gets hung up
console.log(response.statusCode + ' ' + response.statusMessage); // Never outputs this
var responseBody = '';
response.on('data', function (chunk) {
responseBody += chunk;
});
response.on('end', function (chunk) {
console.log('Response body: ' + responseBody);
});
}, function(error) {
console.log('Error: ' + error);
});
}
让我感到困惑的是,当我做一个测试事件时它工作正常,当我在我自己的计算机上本地索引它时它工作正常,但只是不进入 handleRequest。任何 help/direction 不胜感激,谢谢。
编辑:
package.json
{
"dependencies": {
"aws-sdk": "*",
"aws-xray-sdk": "^3.2.0",
"dotenv": "^8.2.0"
}
}
尝试将 handleRequest 函数包装在 Promise 中。你的函数 indexQuote() 看起来几乎一样,但最后它会 return 一个 Promise
function indexQuote(quote) {
...
return new Promise((resolve, reject) => {
client.handleRequest(request, null,
response => {
const { statusCode, statusMessage, headers } = response;
let body = '';
response.on('data', chunk => {
body += chunk;
});
response.on('end', () => {
const data = {
statusCode,
statusMessage,
headers
};
if (body) {
data.body = body;
}
resolve(data);
});
},
err => {
reject(err);
});
});
然后您可以等待并检查结果:
const result = await indexQuote(quote);
console.log("Index result: " + result);