簡體   English   中英

AWS DynamoDB Python連接錯誤

[英]AWS DynamoDB Python Connection Error

無法在AWS Dynamodb上運行示例代碼。

我正在嘗試運行他們在該網站上提供的AWS示例python代碼。 這是我的python文件:

    from __future__ import print_function # Python 2/3 compatibility
    import boto3

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


   table = dynamodb.create_table(
       TableName='Movies',
       KeySchema=[
           {
               'AttributeName': 'year',
               'KeyType': 'HASH'  #Partition key
           },
           {
               'AttributeName': 'title',
               'KeyType': 'RANGE'  #Sort key
           }
       ],
       AttributeDefinitions=[
           {
               'AttributeName': 'year',
               'AttributeType': 'N'
           },
           {
               'AttributeName': 'title',
               'AttributeType': 'S'
           },

       ],
       ProvisionedThroughput={
           'ReadCapacityUnits': 10,
           'WriteCapacityUnits': 10
       }
   )

   print("Table status:", table.table_status)

然后,我在終端中運行python文件,並收到以下錯誤:

File "MoviesCreateTable.py", line 32, in <module>
    'WriteCapacityUnits': 10
File "/home/name/.local/lib/python2.7/site-packages/boto3/resources/factory.py", line 520, in do_action
    response = action(self, *args, **kwargs)
File "/home/name/.local/lib/python2.7/site-packages/boto3/resources/action.py", line 83, in __call__
    response = getattr(parent.meta.client, operation_name)(**params)
File "/home/name/.local/lib/python2.7/site-packages/botocore/client.py", line 159, in _api_call
    return self._make_api_call(operation_name, kwargs)
File "/home/name/.local/lib/python2.7/site-packages/botocore/client.py", line 483, in _make_api_call
    operation_model, request_dict)
File "/home/name/.local/lib/python2.7/site-packages/botocore/endpoint.py", line 141, in make_request
    return self._send_request(request_dict, operation_model)
File "/home/name/.local/lib/python2.7/site-packages/botocore/endpoint.py", line 170, in _send_request
    success_response, exception):
File "/home/name/.local/lib/python2.7/site-packages/botocore/endpoint.py", line 249, in _needs_retry
    caught_exception=caught_exception, request_dict=request_dict)
File "/home/name/.local/lib/python2.7/site-packages/botocore/hooks.py", line 227, in emit
    return self._emit(event_name, kwargs)
File "/home/name/.local/lib/python2.7/site-packages/botocore/hooks.py", line 210, in _emit
    response = handler(**kwargs)
File "/home/name/.local/lib/python2.7/site-packages/botocore/retryhandler.py", line 183, in __call__
    if self._checker(attempts, response, caught_exception):
File "/home/name/.local/lib/python2.7/site-packages/botocore/retryhandler.py", line 251, in __call__
    caught_exception)
File "/home/name/.local/lib/python2.7/site-packages/botocore/retryhandler.py", line 277, in _should_retry
    return self._checker(attempt_number, response, caught_exception)
File "/home/name/.local/lib/python2.7/site-packages/botocore/retryhandler.py", line 317, in __call__
    caught_exception)
File "/home/name/.local/lib/python2.7/site-packages/botocore/retryhandler.py", line 223, in __call__
    attempt_number, caught_exception)
File "/home/name/.local/lib/python2.7/site-packages/botocore/retryhandler.py", line 359, in _check_caught_exception
    raise caught_exception
botocore.vendored.requests.exceptions.ConnectionError: ('Connection aborted.', error(111, 'Connection refused'))

當Dynamodb不運行時,會發生這種情況。 檢查localhost:8000/shell以驗證它是否正在運行。

確保滿足文檔中給出的所有先決條件

在開始本教程之前,您需要執行以下操作:

  • 在計算機上下載並運行DynamoDB。 有關更多信息,請參閱下載並運行DynamoDB
  • 注冊Amazon Web Services並創建訪問密鑰。 您需要這些憑證才能使用AWS開發工具包。 要創建一個AWS賬戶,請訪問http://aws.amazon.com/ ,選擇創建一個AWS賬戶,然后按照在線說明進行操作。
  • 創建一個AWS憑證文件。 有關更多信息,請參閱Boto 3文檔中的“配置”。
  • 安裝Python 2.6或更高版本。 有關更多信息,請參見https://www.python.org/downloads

如果要連接雲實例而不是localhost,請參考以下基於區域的端點。

https://docs.aws.amazon.com/general/latest/gr/rande.html#ddb_region

dynamodb = boto3.resource('dynamodb', region_name='us-west-2', endpoint_url="http://dynamodb.us-west-2.amazonaws.com")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM