Node.js &引用;无法读取属性';预签名到期日';“未定义”的定义;
基本概述,我得到了一个AWS Lambda,其中运行一个Node.js应用程序,通过http调用将JSON发送到我的AWS弹性搜索数据库 所以,我从一个小错误开始:Node.js &引用;无法读取属性';预签名到期日';“未定义”的定义;,node.js,amazon-web-services,
elasticsearch,aws-lambda,aws-elasticsearch,Node.js,Amazon Web Services,
elasticsearch,Aws Lambda,Aws Elasticsearch,基本概述,我得到了一个AWS Lambda,其中运行一个Node.js应用程序,通过http调用将JSON发送到我的AWS弹性搜索数据库 所以,我从一个小错误开始:AWS:{“Message”:“User:anonymous无权执行:es:eshttpost”}过了一段时间,我终于明白了AWS不喜欢未签名的请求 现在,我在这一个结巴 Response: { "errorMessage": "Cannot read property 'presigned-expires' of undefin
AWS:{“Message”:“User:anonymous无权执行:es:eshttpost”}
过了一段时间,我终于明白了AWS不喜欢未签名的请求
现在,我在这一个结巴
Response:
{
"errorMessage": "Cannot read property 'presigned-expires' of undefined",
"errorType": "TypeError",
"stackTrace": [
"V4.isPresigned (/var/runtime/node_modules/aws-sdk/lib/signers/v4.js:206:32)",
"V4.addAuthorization (/var/runtime/node_modules/aws-sdk/lib/signers/v4.js:27:14)",
"Promise (/var/task/index.js:18:16)",
"new Promise (<anonymous>)",
"exports.handler (/var/task/index.js:6:12)"
]
}
您的请求可能缺少标题,请参见下文
var AWS = require('aws-sdk');
var path = require('path');
/* == Globals == */
var esDomain = {
region: 'us-east-1',
endpoint: 'my-domain-search-endpoint',
index: 'myindex',
doctype: 'mytype'
};
var endpoint = new AWS.Endpoint(esDomain.endpoint);
/*
* The AWS credentials are picked up from the environment.
* They belong to the IAM role assigned to the Lambda function.
* Since the ES requests are signed using these credentials,
* make sure to apply a policy that allows ES domain operations
* to the role.
*/
var creds = new AWS.EnvironmentCredentials('AWS');
/*
* Post the given document to Elasticsearch
*/
function postToES(doc, context) {
var req = new AWS.HttpRequest(endpoint);
req.method = 'POST';
req.path = path.join('/', esDomain.index, esDomain.doctype);
req.region = esDomain.region;
req.headers['presigned-expires'] = false;
req.headers['Host'] = endpoint.host;
req.body = doc;
var signer = new AWS.Signers.V4(req , 'es'); // es: service code
signer.addAuthorization(creds, new Date());
var send = new AWS.NodeHttpClient();
send.handleRequest(req, null, function(httpResp) {
var respBody = '';
httpResp.on('data', function (chunk) {
respBody += chunk;
});
httpResp.on('end', function (chunk) {
console.log('Response: ' + respBody);
context.succeed('Lambda added document ' + doc);
});
}, function(err) {
console.log('Error: ' + err);
context.fail('Lambda failed with error ' + err);
});
}
我从github项目aws样本amazon elasticsearch lambda样本中提取了这个:
var AWS = require('aws-sdk');
var path = require('path');
/* == Globals == */
var esDomain = {
region: 'us-east-1',
endpoint: 'my-domain-search-endpoint',
index: 'myindex',
doctype: 'mytype'
};
var endpoint = new AWS.Endpoint(esDomain.endpoint);
/*
* The AWS credentials are picked up from the environment.
* They belong to the IAM role assigned to the Lambda function.
* Since the ES requests are signed using these credentials,
* make sure to apply a policy that allows ES domain operations
* to the role.
*/
var creds = new AWS.EnvironmentCredentials('AWS');
/*
* Post the given document to Elasticsearch
*/
function postToES(doc, context) {
var req = new AWS.HttpRequest(endpoint);
req.method = 'POST';
req.path = path.join('/', esDomain.index, esDomain.doctype);
req.region = esDomain.region;
req.headers['presigned-expires'] = false;
req.headers['Host'] = endpoint.host;
req.body = doc;
var signer = new AWS.Signers.V4(req , 'es'); // es: service code
signer.addAuthorization(creds, new Date());
var send = new AWS.NodeHttpClient();
send.handleRequest(req, null, function(httpResp) {
var respBody = '';
httpResp.on('data', function (chunk) {
respBody += chunk;
});
httpResp.on('end', function (chunk) {
console.log('Response: ' + respBody);
context.succeed('Lambda added document ' + doc);
});
}, function(err) {
console.log('Error: ' + err);
context.fail('Lambda failed with error ' + err);
});
}