Created
October 25, 2019 09:51
-
-
Save trmaphi/eb7cc9855d14132c6c1c4f3f50f5e9be to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var AWS = require('aws-sdk'); | |
var region = ''; // e.g. us-west-1 | |
var domain = ''; // e.g. search-domain.region.es.amazonaws.com | |
var index = 'node-test'; | |
var type = '_doc'; | |
var id = '1'; | |
var json = { | |
"title": "Moneyball", | |
"director": "Bennett Miller", | |
"year": "2011" | |
} | |
indexDocument(json); | |
function indexDocument(document) { | |
var endpoint = new AWS.Endpoint(domain); | |
var request = new AWS.HttpRequest(endpoint, region); | |
request.method = 'PUT'; | |
request.path += index + '/' + type + '/' + id; | |
request.body = JSON.stringify(document); | |
request.headers['host'] = domain; | |
request.headers['Content-Type'] = 'application/json'; | |
// Content-Length is only needed for DELETE requests that include a request | |
// body, but including it for all requests doesn't seem to hurt anything. | |
request.headers['Content-Length'] = Buffer.byteLength(request.body); | |
var credentials = new AWS.EnvironmentCredentials('AWS'); | |
var signer = new AWS.Signers.V4(request, 'es'); | |
signer.addAuthorization(credentials, new Date()); | |
var client = new AWS.HttpClient(); | |
client.handleRequest(request, null, function(response) { | |
console.log(response.statusCode + ' ' + response.statusMessage); | |
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); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment