当在AWS ElasticSearch服务上进行身份验证时,可能会遇到“身份验证最终失败”错误。这个错误通常是由于以下原因之一导致的:
以下是使用AWS SDK for Python(Boto3)在ElasticSearch服务上进行身份验证的示例代码:
import boto3
from elasticsearch import Elasticsearch, RequestsHttpConnection
from requests_aws4auth import AWS4Auth
# 配置AWS凭证
session = boto3.Session(region_name='your_region')
credentials = session.get_credentials()
awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, session.region_name, 'es', session_token=credentials.token)
# 连接到Elasticsearch
host = 'your_es_endpoint'
port = 443
es = Elasticsearch(
hosts=[{'host': host, 'port': port}],
http_auth=awsauth,
use_ssl=True,
verify_certs=True,
connection_class=RequestsHttpConnection
)
# 执行操作
res = es.search(index="your_index", body={"query": {"match_all": {}}})
print(res)
请确保替换示例代码中的以下值:
your_region
:您的AWS区域(例如,us-west-2)your_es_endpoint
:您的ElasticSearch服务的端点your_index
:您要搜索的索引名称以上是解决“AWS ElasticSearch服务上的身份验证最终失败错误”的一般方法。请根据实际情况检查您的访问密钥和权限设置,并根据需要对示例代码进行调整。
上一篇:AWS ElasticSearch服务上的身份验证最终失败。
下一篇:AWS Elasticsearch服务使用.NET HttpClient返回403响应 - HTTP请求头中的大小写错误。