简体   繁体   English

AWS DynamoDB Python连接错误

[英]AWS DynamoDB Python Connection Error

Unable to run sample code on AWS Dynamodb. 无法在AWS Dynamodb上运行示例代码。

I am trying to run the AWS sample python code that they provide on there website. 我正在尝试运行他们在该网站上提供的AWS示例python代码。 Here is my python file: 这是我的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)

I then I run the python file in terminal and I get the following error: 然后,我在终端中运行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'))

This happens when Dynamodb isn't running. 当Dynamodb不运行时,会发生这种情况。 Check localhost:8000/shell to verify it's running. 检查localhost:8000/shell以验证它是否正在运行。

Ensure all the prerequisites given in docs are satisfied. 确保满足文档中给出的所有先决条件

Before you begin this tutorial, you need to do the following: 在开始本教程之前,您需要执行以下操作:

  • Download and run DynamoDB on your computer. 在计算机上下载并运行DynamoDB。 For more information, see Download and Run DynamoDB . 有关更多信息,请参阅下载并运行DynamoDB
  • Sign up for Amazon Web Services and create access keys. 注册Amazon Web Services并创建访问密钥。 You need these credentials to use AWS SDKs. 您需要这些凭证才能使用AWS开发工具包。 To create an AWS account, go to http://aws.amazon.com/ , choose Create an AWS Account, and then follow the online instructions. 要创建一个AWS账户,请访问http://aws.amazon.com/ ,选择创建一个AWS账户,然后按照在线说明进行操作。
  • Create an AWS credentials file. 创建一个AWS凭证文件。 For more information, see Configuration in the Boto 3 documentation. 有关更多信息,请参阅Boto 3文档中的“配置”。
  • Install Python 2.6 or later. 安装Python 2.6或更高版本。 For more information, see https://www.python.org/downloads . 有关更多信息,请参见https://www.python.org/downloads

If you want to connect cloud instance instead of localhost then refer following region based endpoints. 如果要连接云实例而不是localhost,请参考以下基于区域的端点。

https://docs.aws.amazon.com/general/latest/gr/rande.html#ddb_region 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