Python 使用 Boto3 将本地主机端点连接到 DynamoDB Local

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31948742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:47:08  来源:igfitidea点击:

Localhost Endpoint to DynamoDB Local with Boto3

pythonamazon-dynamodbdynamo-local

提问by R J

Although Amazon provides documentation regarding how to connect to dynamoDB localwith 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 methodinside boto.dynamodb2.layer1, but this creates an incompatibility between live and test environments that use the boto3 protocol to manage dynamoDB.

尽管 Amazon 提供了有关如何使用 Java、PHP 和 .Net连接到本地 dynamoDB 的文档,但没有说明如何使用 Python 连接到 localhost:8000。网络上的现有文档指出在boto.dynamodb2.layer1 中使用了DynamoDBConnection 方法,但这会导致使用 boto3 协议管理 dynamoDB 的实时环境和测试环境之间不兼容。

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

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

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

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

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

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 optiondoes 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?

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

回答by Kyle Knapp

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

它确实支持 DynamoDB Local。您只需要设置适当的端点,就像使用其他语言 SDK 一样

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

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

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

回答by Damian Wilbur

Note: You will want to extend the above response to include region. I have appended to Kyle's code above. If your initial attempt is greeted with a region error, this will return the appropriate '[]' response.

注意:您需要扩展上述响应以包含区域。我已经附加到上面凯尔的代码。如果您的初始尝试遇到区域错误,这将返回适当的“[]”响应。

import boto3

## For a Boto3 client ('client' is for low-level access to Dynamo service API)
ddb1 = boto3.client('dynamodb', endpoint_url='http://localhost:8000', region_name='us-west-2')
response = ddb1.list_tables()
print(response)

# For a Boto3 service resource ('resource' is for higher-level, abstracted access to Dynamo)
ddb2 = boto3.resource('dynamodb', endpoint_url='http://localhost:8000', region_name='us-west-2')
print(list(ddb2.tables.all()))

回答by Aman Agarwal

use dummy access key and id otherwise it will throw an exception on running the methods.

使用虚拟访问密钥和 id 否则它会在运行方法时抛出异常。

import boto3

dynamodb = boto3.session('dynamodb',
                          aws_access_key_id="anything",
                          aws_secret_access_key="anything",
                          region_name="us-west-2",
                          endpoint_url="http://localhost:8000")
import boto3

dynamodb = boto3.session('dynamodb',
                          aws_access_key_id="anything",
                          aws_secret_access_key="anything",
                          region_name="us-west-2",
                          endpoint_url="http://localhost:8000")

回答by Supawat Pusavanno

This is the tutorial python DynamoDb. It depicts how to connect to local instance.

这是教程 python DynamoDb。它描述了如何连接到本地实例。

http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/GettingStarted.Python.01.html

http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/GettingStarted.Python.01.html

It seems like the minimum required parameters are the following with the help of aws configuration (below).

在 aws 配置(如下)的帮助下,似乎所需的最低参数如下。

dynamodb = boto3.resource('dynamodb', endpoint_url='http://localhost:8000/')

The region, access key and secret key parameters can be omitted when configure profile parameters using aws configurecommand (require install aws cli). However, you can create aws configuration files manually in your home (in case, you don't want to use aws cli).

使用aws configure命令(需要安装 aws cli)配置配置文件参数时,可以省略区域、访问密钥和密钥参数。但是,您可以在家中手动创建 aws 配置文件(以防万一,您不想使用 aws cli)。

file ~/.aws/config

文件 ~/.aws/config

[default]
output = json
region = anywhere

file ~/.aws/credentials

文件 ~/.aws/credentials

[default]
aws_access_key_id = whatever_id
aws_secret_access_key = whatever_key 

You can consult the aws configuration in http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html

您可以在http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html 中查阅 aws 配置

Note in the local DynamoDb development region, aws_access_key_idand aws_secret_access_keyvalues in those files can be anything. But if you want to use aws cli with the AWS then you must put the valid region, valid id and keys. They are available when you register to the AWS services.

注意在当地DynamoDb发展regionaws_access_key_idaws_secret_access_key在这些文件中的值可以是任何东西。但是,如果您想将 aws cli 与 AWS 一起使用,则必须输入有效区域、有效 ID 和密钥。它们在您注册 AWS 服务时可用。

More information, when you call

更多信息,当您致电时

db = boto3.client('dynamodb')

The host the boto3 connect will base on the regionparameter e.g. region=us-west-1when call above api, it will connect to dynamodb.us-west-1.amazonaws.com. However, when pass parameter endpoint_urlthe regionwill not be used. For more AWS endpoint list please go to http://docs.aws.amazon.com/general/latest/gr/rande.html.

boto3 连接的主机将基于region参数,例如region=us-west-1当调用上面的 api 时,它将连接到dynamodb.us-west-1.amazonaws.com. 然而,当传递参数endpoint_urlregion将不会被使用。有关更多 AWS 端点列表,请访问http://docs.aws.amazon.com/general/latest/gr/rande.html