简体   繁体   English

使用Boto3到DynamoDB Local的Localhost端点

[英]Localhost Endpoint to DynamoDB Local with Boto3

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. 尽管Amazon提供了有关如何使用Java,PHP和.Net连接到本地dynamoDB的文档,但没有描述如何使用Python连接到localhost:8000。 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. Web上的现有文档指向在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. 相反,我希望对所有内容都使用boto3,并能够在环境变量中为dynamoDB设置端点。 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)? 有什么方法可以使用boto3定义dynamoDB本地终结点(就像其他语言一样)吗? Or any chance that Amazon will plan to support this feature? 还是亚马逊计划支持此功能的机会?

It does support DynamoDB Local. 它确实支持DynamoDB Local。 You just need to set the appropriate endpoint such as you can do with other language SDKs 您只需要设置适当的端点即可,例如可以使用其他语言的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()))

Note: You will want to extend the above response to include region. 注意:您将需要扩展以上响应以包括区域。 I have appended to Kyle's code above. 我已经在上面添加了Kyle的代码。 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()))

This is the tutorial python DynamoDb. 这是python DynamoDb教程。 It depicts how to connect to local instance. 它描述了如何连接到本地实例。

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 configure command (require install aws cli). 使用aws configure命令(需要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配置文件(以防您不想使用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_id and aws_secret_access_key values in those files can be anything. 注意在当地DynamoDb发展regionaws_access_key_idaws_secret_access_key这些文件中的值可以是任何东西。 But if you want to use aws cli with the AWS then you must put the valid region, valid id and keys. 但是,如果要在AWS上使用aws cli,则必须放置有效区域,有效ID和密钥。 They are available when you register to the AWS services. 当您注册到AWS服务时,它们可用。

More information, when you call 致电时提供更多信息

db = boto3.client('dynamodb')

The host the boto3 connect will base on the region parameter eg region=us-west-1 when call above api, it will connect to dynamodb.us-west-1.amazonaws.com . 在api上方调用时,boto3连接的主机将基于region参数,例如region=us-west-1 ,它将连接到dynamodb.us-west-1.amazonaws.com However, when pass parameter endpoint_url the region will not be used. 但是,当传递参数endpoint_url ,将不使用该region For more AWS endpoint list please go to http://docs.aws.amazon.com/general/latest/gr/rande.html . 有关更多AWS终端节点列表,请访问http://docs.aws.amazon.com/general/latest/gr/rande.html

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

Here is the boto3 Config Reference (API): 这是boto3配置参考(API):

https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html 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`
        """

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM