本地主机端点到 DynamoDB 本地与 Boto3

2022-01-15 00:00:00 python amazon-dynamodb dynamo-local

问题描述

虽然 Amazon 提供了有关如何连接到 dynamoDB local 对于 Java、PHP 和 .Net,没有描述如何使用 Python 连接到 localhost:8000.网络上的现有文档指出在 boto.dynamodb2 中使用 DynamoDBConnection 方法.layer1,但这会在使用 boto3 协议管理 dynamoDB 的实时环境和测试环境之间造成不兼容.

Although Amazon provides documentation regarding how to connect to dynamoDB local with Java, PHP and .Net, there is no description of how to connect to localhost:8000 using Python. Existing documentation on the web points to the use of the DynamoDBConnection method inside boto.dynamodb2.layer1, but this creates an incompatibility between live and test environments that use the boto3 protocol to manage dynamoDB.

在 boto3 中,您可以使用以下构造函数和设置到环境中的变量向 dynamo 发出请求:

In boto3, you can make a request to dynamo using the following constructor and variables set into the environment:

client = boto3.client('dynamodb')
table = client.list_tables()

而 boto.dynamodb2.layer1 包需要您构建以下内容:

Whereas the boto.dynamodb2.layer1 package requires you to construct the following:

client = DynamoDBConnection(
    host='localhost',
    port=8000,
    aws_access_key_id='anything',
    aws_secret_access_key='anything',
    is_secure=False)
table = client.list_tables()

虽然可以创建基于本地环境确定正确构造函数的逻辑,但我对构建一组将每个构造函数视为相同的方法持谨慎态度.相反,我更愿意对所有内容都使用 boto3,并能够在环境变量中为 dynamoDB 设置端点.不幸的是,该选项目前似乎不可用.

Although it is possible to create logic which determines the proper constructor based upon the local environment, I am wary of building a set of methods which treat each constructor as the same. Instead, I would prefer to use boto3 for everything and to be able to set the endpoint for dynamoDB in the environmental variables. Unfortunately, that option does not appear to be currently be available.

有没有办法使用 boto3 来定义 dynamoDB 本地端点(像其他语言一样)?或者亚马逊是否有计划支持此功能?

Is there any way to use boto3 to define a dynamoDB local endpoint (like the other languages)? Or any chance that Amazon will plan to support this feature?


解决方案

它确实支持 DynamoDB Local.您只需要设置适当的端点,例如您可以使用其他 语言 SDK

It does support DynamoDB Local. You just need to set the appropriate endpoint such as you can do with other language SDKs

以下是如何通过 DynamoDB Local 使用 boto3 的客户端和资源接口的代码片段:

Here is a code snippet of how you can use boto3's client and resource interface via DynamoDB Local:

import boto3

# For a Boto3 client.
ddb = boto3.client('dynamodb', endpoint_url='http://localhost:8000')
response = ddb.list_tables()
print(response)

# For a Boto3 service resource
ddb = boto3.resource('dynamodb', endpoint_url='http://localhost:8000')
print(list(ddb.tables.all()))

相关文章