使用 SPARQL 和签名版本 4 签名连接到 Neptune Node.js - Amazon Neptune
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 SPARQL 和签名版本 4 签名连接到 Neptune Node.js

以下示例说明如何使用 Node.js 和签名版本 4 身份验证连接到 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); }); }