本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用 SPARQL 和 Node.js 以及签名版本 4 签名连接到 Neptune
使用签名 V4 签名和 Javascript V3 AWS 开发工具包进行查询
以下是如何使用带有签名版本 4 身份验证的 Node.js 和适用于 Javascript V3 的 S AWS DK 连接到 Neptune SPARQL 的示例:
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); } ); }
使用签名 V4 签名和适用于 Javascript V2 的 AWS SDK 进行查询
以下是如何使用带签名版本 4 身份验证的 Node.js 和适用于 Javascript V2 的 S AWS DK 连接到 Neptune SPARQL 的示例:
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); }); }