[英]Localhost Endpoint to DynamoDB Local with Boto3
盡管Amazon提供了有關如何使用Java,PHP和.Net連接到本地dynamoDB的文檔,但沒有描述如何使用Python連接到localhost:8000。 Web上的現有文檔指向在boto.dynamodb2.layer1內部使用DynamoDBConnection方法 ,但這在使用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設置端點。 不幸的是, 該選項當前似乎不可用。
有什么方法可以使用boto3定義dynamoDB本地終結點(就像其他語言一樣)嗎? 還是亞馬遜計划支持此功能的機會?
它確實支持DynamoDB Local。 您只需要設置適當的端點即可,例如可以使用其他語言的SDK
以下是如何通過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()))
注意:您將需要擴展以上響應以包括區域。 我已經在上面添加了Kyle的代碼。 如果您的初次嘗試遇到區域錯誤,這將返回適當的“ []”響應。
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()))
這是python DynamoDb教程。 它描述了如何連接到本地實例。
http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/GettingStarted.Python.01.html
似乎在aws配置的幫助下(以下)需要的最低參數如下。
dynamodb = boto3.resource('dynamodb', endpoint_url='http://localhost:8000/')
使用aws configure
命令(需要install aws cli)配置配置文件參數時,可以省略區域,訪問密鑰和秘密密鑰參數。 但是,您可以在家里手動創建aws配置文件(以防您不想使用aws cli)。
文件〜/ .aws / config
[default]
output = json
region = anywhere
文件〜/ .aws / credentials
[default]
aws_access_key_id = whatever_id
aws_secret_access_key = whatever_key
您可以在http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html中查閱AWS配置
注意在當地DynamoDb發展region
, aws_access_key_id
和aws_secret_access_key
這些文件中的值可以是任何東西。 但是,如果要在AWS上使用aws cli,則必須放置有效區域,有效ID和密鑰。 當您注冊到AWS服務時,它們可用。
致電時提供更多信息
db = boto3.client('dynamodb')
在api上方調用時,boto3連接的主機將基於region
參數,例如region=us-west-1
,它將連接到dynamodb.us-west-1.amazonaws.com
。 但是,當傳遞參數endpoint_url
,將不使用該region
。 有關更多AWS終端節點列表,請訪問http://docs.aws.amazon.com/general/latest/gr/rande.html 。
使用虛擬訪問密鑰和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")
這是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`
"""
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.