AWS 无法连接到 opensearch 无服务器,使用 python 时出现错误“Session”对象没有属性“token”

问题描述 投票:0回答:1

我正在尝试在新的 opensearch 设置上创建索引,因为我们将开始从 aws 中的 elasticsearch 迁移到 opensearch 无服务器。我有下面的配置,我能够毫无问题地获得 aws_session_token,但是我无法实际连接到 botosession。如果我在 botosession 配置中缺少某些内容,或者我是否需要为 botocore 配置某些内容,请告诉我?


botoclient = boto3.client('sts', region_name = region, aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key,verify = False)
response = botoclient.get_session_token()
aws_session_token = response["Credentials"]["SessionToken"]


session1 = boto3.Session(
    aws_access_key_id=aws_access_key_id,
    aws_secret_access_key=aws_secret_access_key,
    aws_session_token=aws_session_token
)

auth = AWSV4SignerAuth(session1, region, service)


botosession = OpenSearch(
    hosts = [{'host': host, 'port': 443}],
    http_auth = auth,
    use_ssl = True,
    verify_certs = True,
    connection_class = RequestsHttpConnection,
    pool_maxsize = 20
)


index_name = 'python-test-index'
index_body = {
  'settings': {
    'index': {
      'number_of_shards': 4
    }
  }
}

response1 = botosession.indices.create(index_name, body=index_body)
print('\nCreating index:')
print(response1)

我阅读了有关 opensearch、boto、botocore 和 boto.session 的文档,并且取得了进展,但是我被这个错误消息难住了。我认为这与 botocore 有关,但我可能是错的。我对 aws 的访问也受到限制,因此我可能缺少许可,但我认为情况并非如此。我希望能够在 opensearch 上创建一个测试索引

python boto3 opensearch
1个回答
0
投票

AWSV4SignerAuth
对象的第一个参数应该是会话的credentials,而不是会话本身。

session1 = boto3.Session(
    aws_access_key_id=aws_access_key_id,
    aws_secret_access_key=aws_secret_access_key,
    aws_session_token=aws_session_token
)
session1_creds = session1.get_credentials()

auth = AWSV4SignerAuth(session1_creds, region, service)

这是基于 OpenSearch 客户端的文档:https://opensearch.org/docs/latest/clients/python-low-level/

© www.soinside.com 2019 - 2024. All rights reserved.