gpt4 book ai didi

node.js - 如何通过 NodeJS 调用 elasticsearch api?

转载 作者:搜寻专家 更新时间:2023-10-31 23:40:02 24 4
gpt4 key购买 nike

我的任务是对 Elasticsearch api 进行 POST api 调用,

https://search-test-search-fqa4l6ubylznt7is4d5yxlmbxy.us-west-2.es.amazonaws.com/klove-ddb/recipe/_search

我之前没有任何对 AWS 服务进行 API 调用的经验。

所以,我试过了 -

axios.post('https://search-test-search-fqa4l6ubylznt7is4d5yxlmbxy.us-west-2.es.amazonaws.com/klove-ddb/recipe/_search')
.then(res => res.data)
.then(res => console.log(res));

但我收到了{"Message":"User: anonymous is not authorized to perform: es:ESHttpPost"}

我还检查了一些 IAM 角色并将 AWSESFullAccess 策略添加到我的配置文件中。

我仍然无法解决任何问题。

请帮帮我。

最佳答案

您看到错误的原因 User: anonymous is not authorized to perform: es:ESHttpPost 是因为您在请求数据时没有让 ElasticSearch 知道您是谁 - 这就是为什么它说 '匿名'。

有几种验证方式,最简单的是使用 elasticsearch library .使用此库,您将向库提供 IAM 角色/用户的一组凭证(访问 key 、 key )。它将使用它来创建签名请求。签名的请求会让 AWS 知道实际是谁在发出请求,因此它不会以匿名方式接收,而是以您自己的方式接收。

另一种方法是将您的访问策略调整为基于 IP:

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "es:*",
"Condition": {
"IpAddress": {
"aws:SourceIp": [
"AAA.BBB.CCC.DDD"
]
}
},
"Resource": "YOUR_ELASTICSEARCH_CLUSTER_ARN"
}
]
}

此特定政策将对任何拥有您在此处提供的 ip(范围)的人开放。不过,它会让您免去必须签署请求的麻烦。

帮助使用 AWS ES 设置 elasticsearch-js 的库是 this one

一个工作示例如下:

const AWS = require('aws-sdk')
const elasticsearch = require('elasticsearch')
const awsHttpClient = require('http-aws-es')

let client = elasticsearch.Client({
host: '<YOUR_ES_CLUSTER_ID>.<YOUR_ES_REGION>.es.amazonaws.com',
connectionClass: awsHttpClient,
amazonES: {
region: '<YOUR_ES_REGION>',
credentials: new AWS.Credentials('<YOUR_ACCESS_KEY>', '<YOUR_SECRET_KEY>')
}
});

client.search({
index: 'twitter',
type: 'tweets',
body: {
query: {
match: {
body: 'elasticsearch'
}
}
}
})
.then(res => console.log(res));

关于node.js - 如何通过 NodeJS 调用 elasticsearch api?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50973930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com