

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

# 通过 Gremlin Python 使用 IAM 身份验证连接到 Amazon Neptune 数据库
<a name="gremlin-python-iam-auth"></a>

## 概述
<a name="gremlin-python-iam-auth-overview"></a>

 本指南演示了如何使用 Gremlin Python 驱动程序、签名版本 4 身份验证和 AWS 适用于 Python 的软件开发工具包 (Boto3) 连接到启用 IAM 身份验证的 Amazon Neptune 数据库。

## 创建基本连接
<a name="gremlin-python-iam-auth-basic-connection"></a>

 使用以下代码示例作为指导，了解如何使用 Gremlin Python 驱动程序通过 IAM 身份验证建立基本连接。

```
from boto3 import Session
from botocore.auth import SigV4Auth
from botocore.awsrequest import AWSRequest

from gremlin_python.process.anonymous_traversal import traversal
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection

def main():
    endpoint = 'your.cluster.endpoint.neptune.amazonaws.com'
    conn_string = 'wss://' + endpoint + ':8182/gremlin'
    default_region = 'us-east-1'
    service = 'neptune-db'

    credentials = Session().get_credentials()
    if credentials is None:
        raise Exception("No AWS credentials found")
    creds = credentials.get_frozen_credentials()
    # region set inside config profile or via AWS_DEFAULT_REGION environment variable will be loaded
    region = Session().region_name if Session().region_name else default_region

    request = AWSRequest(method='GET', url=conn_string, data=None)
    SigV4Auth(creds, service, region).add_auth(request)

    rc = DriverRemoteConnection(conn_string, 'g', headers=request.headers.items())
    g = traversal().with_remote(rc)

    # simple query to verify connection
    count = g.V().count().next()
    print('Vertex count: ' + str(count))

    # cleanup
    rc.close()

if __name__ == "__main__":
    main()
```