使用Boto3将Localhost端点连接到DynamoDB Local

时间:2015-08-11 17:56:47

标签: python amazon-dynamodb dynamo-local

尽管亚马逊提供了有关如何使用Java,PHP和.Net连接dynamoDB local的文档,但没有关于如何使用Python连接到localhost:8000的说明。 Web上的现有文档指向使用boto.dynamodb2.layer1中的DynamoDBConnection method,但这会在使用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='anything',
    aws_secret_access_key='anything',
    is_secure=False)
table = client.list_tables()

虽然可以创建基于本地环境确定正确构造函数的逻辑,但我对构建一组将每个构造函数视为相同的方法持谨慎态度。相反,我更喜欢使用boto3来处理所有事情,并且能够在环境变量中设置dynamoDB的端点。很遗憾,that option目前似乎无法使用。

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

5 个答案:

答案 0 :(得分:44)

它确实支持DynamoDB Local。您只需设置适当的端点,例如您可以使用其他language SDKs

以下是如何通过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()))

答案 1 :(得分:16)

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

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

答案 2 :(得分:8)

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

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

在aws配置的帮助下,似乎所需的最小参数如下所示。

model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='softmax'))

使用dynamodb = boto3.resource('dynamodb', endpoint_url='http://localhost:8000/') 命令(require install aws cli)配置配置文件参数时,可以省略区域,访问密钥和密钥参数。但是,您可以在家中手动创建aws配置文件(如果您不想使用aws cli)。

file~ / .aws / config

aws configure

file~ / .aws / credentials

[default]
output = json
region = anywhere

您可以参考aws配置 http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html

请注意,本地DynamoDb开发[default] aws_access_key_id = whatever_id aws_secret_access_key = whatever_key 中,这些文件中的regionaws_access_key_id值可以是任何值。但是如果你想在AWS上使用aws cli,那么你必须放入有效的区域,有效的id和密钥。注册AWS服务时可以使用它们。

更多信息,请致电

aws_secret_access_key

boto3 connect的主机将基于db = boto3.client('dynamodb') 参数,例如region在api上方调用时,它将连接到region=us-west-1。但是,当传递参数dynamodb.us-west-1.amazonaws.com时,将不使用endpoint_url。有关更多AWS端点列表,请转至http://docs.aws.amazon.com/general/latest/gr/rande.html

答案 3 :(得分:7)

使用虚拟访问密钥和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")

答案 4 :(得分:0)

这是boto3配置参考(API):

https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html

    def resource(self, service_name, region_name=None, api_version=None,
                 use_ssl=True, verify=None, endpoint_url=None,
                 aws_access_key_id=None, aws_secret_access_key=None,
                 aws_session_token=None, config=None):
        """
        Create a resource service client by name.

        :type service_name: string
        :param service_name: The name of a service, e.g. 's3' or 'ec2'. You
            can get a list of available services via
            :py:meth:`get_available_resources`.

        :type region_name: string
        :param region_name: The name of the region associated with the client.
            A client is associated with a single region.

        :type api_version: string
        :param api_version: The API version to use.  By default, botocore will
            use the latest API version when creating a client.  You only need
            to specify this parameter if you want to use a previous API version
            of the client.

        :type use_ssl: boolean
        :param use_ssl: Whether or not to use SSL.  By default, SSL is used.
            Note that not all services support non-ssl connections.

        :type verify: boolean/string
        :param verify: Whether or not to verify SSL certificates.  By default
            SSL certificates are verified.  You can provide the following
            values:

            * False - do not validate SSL certificates.  SSL will still be
              used (unless use_ssl is False), but SSL certificates
              will not be verified.
            * path/to/cert/bundle.pem - A filename of the CA cert bundle to
              uses.  You can specify this argument if you want to use a
              different CA cert bundle than the one used by botocore.

        :type endpoint_url: string
        :param endpoint_url: The complete URL to use for the constructed
            client. Normally, botocore will automatically construct the
            appropriate URL to use when communicating with a service.  You
            can specify a complete URL (including the "http/https" scheme)
            to override this behavior.  If this value is provided,
            then ``use_ssl`` is ignored.

        :type aws_access_key_id: string
        :param aws_access_key_id: The access key to use when creating
            the client.  This is entirely optional, and if not provided,
            the credentials configured for the session will automatically
            be used.  You only need to provide this argument if you want
            to override the credentials used for this specific client.

        :type aws_secret_access_key: string
        :param aws_secret_access_key: The secret key to use when creating
            the client.  Same semantics as aws_access_key_id above.

        :type aws_session_token: string
        :param aws_session_token: The session token to use when creating
            the client.  Same semantics as aws_access_key_id above.

        :type config: botocore.client.Config
        :param config: Advanced client configuration options. If region_name
            is specified in the client config, its value will take precedence
            over environment variables and configuration values, but not over
            a region_name value passed explicitly to the method.  If
            user_agent_extra is specified in the client config, it overrides
            the default user_agent_extra provided by the resource API. See
            `botocore config documentation
            <https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html>`_
            for more details.

        :return: Subclass of :py:class:`~boto3.resources.base.ServiceResource`
        """