使用Boto3到DynamoDB Local的Localhost端点 [英] Localhost Endpoint to DynamoDB Local with Boto3

查看:162
本文介绍了使用Boto3到DynamoDB Local的Localhost端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

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

而boto.dynamodb2.layer1包要求您构造以下内容:

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

尽管可以创建确定由于需要根据本地环境选择合适的构造函数,因此我担心构建一套将每个构造函数视为相同的方法。相反,我希望对所有内容都使用boto3,并能够在环境变量中为dynamoDB设置端点。不幸的是,该选项目前似乎不可用。 p>

是否可以使用boto3定义dynamoDB本地终结点(就像其他语言一样)?还是亚马逊计划支持此功能的任何机会?

解决方案

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



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

  import boto3 

#对于Boto3客户端。
ddb = boto3.client('dynamodb',endpoint_url ='http:// localhost:8000')
响应= ddb.list_tables()
打印(响应)

#对于Boto3服务资源
ddb = boto3.resource('dynamodb',endpoint_url ='http:// localhost:8000')
print(list(ddb.tables.all() ))


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.

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()

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()

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.

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?

解决方案

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

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()))

这篇关于使用Boto3到DynamoDB Local的Localhost端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆