python 没有明显原因的 boto3 连接错误

python boto3 connection error with no apparent cause

我在使用 boto3 连接到 AWS 的代码中遇到错误。这个错误是昨天下午才开始的,在我最后一次没有收到错误和第一次收到错误之间我没有看到任何改变。

错误是:

botocore.exceptions.EndpointConnectionError: Could not connect to the endpoint URL:

在 .aws/config 我有:

$ cat ~/.aws/config
[default]
region=us-east-1

这是我所知道的:

我认为在上次有效和第一次无效之间我没有改变任何会影响它的东西。似乎我不得不更改我这边的一些 AWS 特定配置或一些低级库,但我没有进行任何此类更改。我与一位同事交谈了 30-45 分钟,当我返回并从我离开的地方继续时,问题第一次出现了。

关于解决此问题的任何想法或想法?

完整的异常转储如下。

$ python
Python 2.7.10 (default, Jul 14 2015, 19:46:27) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import boto3
>>> boto3.client('ec2').describe_regions()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/botocore/client.py", line 200, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/Library/Python/2.7/site-packages/botocore/client.py", line 244, in _make_api_call
    operation_model, request_dict)
  File "/Library/Python/2.7/site-packages/botocore/endpoint.py", line 173, in make_request
    return self._send_request(request_dict, operation_model)
  File "/Library/Python/2.7/site-packages/botocore/endpoint.py", line 203, in _send_request
    success_response, exception):
  File "/Library/Python/2.7/site-packages/botocore/endpoint.py", line 267, in _needs_retry
    caught_exception=caught_exception)
  File "/Library/Python/2.7/site-packages/botocore/hooks.py", line 226, in emit
    return self._emit(event_name, kwargs)
  File "/Library/Python/2.7/site-packages/botocore/hooks.py", line 209, in _emit
    response = handler(**kwargs)
  File "/Library/Python/2.7/site-packages/botocore/retryhandler.py", line 183, in __call__
    if self._checker(attempts, response, caught_exception):
  File "/Library/Python/2.7/site-packages/botocore/retryhandler.py", line 250, in __call__
    caught_exception)
  File "/Library/Python/2.7/site-packages/botocore/retryhandler.py", line 273, in _should_retry
    return self._checker(attempt_number, response, caught_exception)
  File "/Library/Python/2.7/site-packages/botocore/retryhandler.py", line 313, in __call__
    caught_exception)
  File "/Library/Python/2.7/site-packages/botocore/retryhandler.py", line 222, in __call__
    return self._check_caught_exception(attempt_number, caught_exception)
  File "/Library/Python/2.7/site-packages/botocore/retryhandler.py", line 355, in _check_caught_exception
    raise caught_exception
botocore.exceptions.EndpointConnectionError: Could not connect to the endpoint URL: "https://ec2.us-east-1.amazonaws.com/"

问题已解决。事实证明,独立于任何 boto 相关的几个看似无关的操作导致 HTTP_PROXY 和 HTTPS_PROXY 环境变量设置不当,然后破坏了 boto3 和 aws cli 下的 botocore 调用。删除两个环境变量解决了问题。

我将保留它,因为我发现很难找到任何指向此错误的可能原因的信息。可能会为其他人节省一些我经历过的头发拉扯。

我刚刚遇到了类似的问题。突然间,在我仍然可以在我的 Heroku 环境中执行操作时,无法通过 django 上的 boto3 连接到我的 s3。

似乎我最近安装了 amazon CLI,我的配置不同,CLI 否决了环境变量……该死。花了我3个小时才找到。

通过aws configure我现在设置

AWS Access Key ID [****************MPIA]: "your true key here without quotes" AWS Secret Access Key [****************7DWm]: "your true secret access key here without quotes" Default region name [eu-west-1]: "your true region here without quotes" Default output format [None]: [here i did just an enter in order not to change this]

为了遇到此问题的任何人而发布此消息。

当我的连接中断时,我遇到了同样的错误 - botocore.exceptions.EndpointConnectionError: Could not connect to the endpoint URL: "https://ec2.us-east-2.amazonaws.com/"

当连接恢复后,它就可以正常工作了。此错误的可能原因可能是

  1. 连接错误
  2. 区域无法满足您的请求,因为每个请求都会到达 AWS 上的端点(更多详细信息可以在 https://docs.aws.amazon.com/general/latest/gr/rande.html#billing-pricing 上找到)

似乎 Boto3 已经成熟到可以抛出异常,因为更恰当的失败原因无法准确知道发生了什么。

此外,如果您有任何与您的配置相关的问题,那么大多数问题都封装有 ClientError 异常。