Connecting to Amazon Neptune databases using IAM authentication with SPARQL and Node.js - Amazon Neptune
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Connecting to Amazon Neptune databases using IAM authentication with SPARQL and Node.js

Querying using Signature V4 signing and the Amazon SDK for Javascript V3

Here is an example of how to connect to Neptune SPARQL using Node.js with Signature Version 4 authentication and the Amazon SDK for Javascript V3:

const { HttpRequest } = require('@smithy/protocol-http'); const { fromNodeProviderChain } = require('@aws-sdk/credential-providers'); const { SignatureV4 } = require('@smithy/signature-v4'); const { Sha256 } = require('@aws-crypto/sha256-universal'); const https = require('https'); var region = 'us-west-2'; // e.g. us-west-1 var neptune_endpoint = 'your-Neptune-cluster-endpoint'; // like: 'cluster-id.region.neptune.amazonaws.com' var query = `query=PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX class: <http://aws.amazon.com/neptune/csv2rdf/class/> PREFIX resource: <http://aws.amazon.com/neptune/csv2rdf/resource/> PREFIX prop: <http://aws.amazon.com/neptune/csv2rdf/datatypeProperty/> PREFIX objprop: <http://aws.amazon.com/neptune/csv2rdf/objectProperty/> SELECT ?movies ?title WHERE { ?jel prop:name "James Earl Jones" . ?movies ?p2 ?jel . ?movies prop:title ?title } LIMIT 10`; runQuery(query); function runQuery(q) { var request = new HttpRequest({ hostname: neptune_endpoint, port: 8182, path: 'sparql', body: encodeURI(query), headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'host': neptune_endpoint + ':8182', }, method: 'POST', }); const credentialProvider = fromNodeProviderChain(); let credentials = credentialProvider(); credentials.then( (cred)=>{ var signer = new SignatureV4({credentials: cred, region: region, sha256: Sha256, service: 'neptune-db'}); signer.sign(request).then( (req)=>{ var responseBody = ''; var sendreq = https.request( { host: req.hostname, port: req.port, path: req.path, method: req.method, headers: req.headers, }, (res) => { res.on('data', (chunk) => { responseBody += chunk; }); res.on('end', () => { console.log(JSON.parse(responseBody)); }); }); sendreq.write(req.body); sendreq.end(); } ); }, (err)=>{ console.error(err); } ); }

Querying using Signature V4 signing and the Amazon SDK for Javascript V2

Here is an example of how to connect to Neptune SPARQL using Node.js with Signature Version 4 authentication and the Amazon SDK for Javascript V2:

var AWS = require('aws-sdk'); var region = 'us-west-2'; // e.g. us-west-1 var neptune_endpoint = 'your-Neptune-cluster-endpoint'; // like: 'cluster-id.region.neptune.amazonaws.com' var query = `query=PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX class: <http://aws.amazon.com/neptune/csv2rdf/class/> PREFIX resource: <http://aws.amazon.com/neptune/csv2rdf/resource/> PREFIX prop: <http://aws.amazon.com/neptune/csv2rdf/datatypeProperty/> PREFIX objprop: <http://aws.amazon.com/neptune/csv2rdf/objectProperty/> SELECT ?movies ?title WHERE { ?jel prop:name "James Earl Jones" . ?movies ?p2 ?jel . ?movies prop:title ?title } LIMIT 10`; runQuery(query); function runQuery(q) { var endpoint = new AWS.Endpoint(neptune_endpoint); endpoint.port = 8182; var request = new AWS.HttpRequest(endpoint, region); request.path += 'sparql'; request.body = encodeURI(query); request.headers['Content-Type'] = 'application/x-www-form-urlencoded'; request.headers['host'] = neptune_endpoint; request.method = 'POST'; var credentials = new AWS.CredentialProviderChain(); credentials.resolve((err, cred)=>{ var signer = new AWS.Signers.V4(request, 'neptune-db'); signer.addAuthorization(cred, 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); }); }