【问题标题】:Boto3 is giving botocore.exceptions.ConnectTimeoutError: Connect timeout on endpoint URL: on Linux serverBoto3 给出 botocore.exceptions.ConnectTimeoutError: Connect timeout on endpoint URL: on Linux server
【发布时间】:2022-01-21 11:23:10
【问题描述】:

以下代码在 Windows 上运行,但在 Linux 服务器上出现错误。我可以通过使用防火墙、ping 和 telnet 从 linux 服务器访问端点。

import boto3
from botocore.client import Config
import boto3

config = Config(connect_timeout=5, retries={'max_attempts': 0})
aws_access_key_id = "aws_access_key_id"
aws_secret_access_key = "aws_secret_access_key"
host = "http://s3path",)

session = boto3.Session()

s1 = session.resource('s3', config=config)
s3 = boto3.client('s3',endpoint_url=host, aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key,config=config)

# Print out bucket names
contents = s3.list_objects_v2(Bucket='bucket',  MaxKeys=1000, Prefix='prefix')['Contents']
print(contents)```

Error:
raise ConnectTimeoutError(endpoint_url=request.url, error=e)
botocore.exceptions.ConnectTimeoutError: Connect timeout on endpoint URL: 

【问题讨论】:

  • 澄清一下 - 你能 ping s3.aws.com-datahub 吗?或者那是一个示例网址?
  • 请忽略这些值aws_access_key_id = "aws_access_key_id", aws_secret_access_key = "aws_secret_access_key", host = "hostname"。因为我提到了虚拟值。

标签: python python-3.x linux amazon-s3 boto3


【解决方案1】:

我无法使用您的代码进行测试,因为您没有包含失败的部分,但我可以告诉您,您通常不需要 endpoint_url。这是文档中的相关部分

endpoint_url (string) -- 用于构造的完整 URL 客户。通常,botocore 会自动构建 与服务通信时使用的适当 URL

这是我的有效代码版本。从 AWS Cloud Shell 内部运行。

import boto3
from botocore.client import Config

bucket_name = "your-bucket-name"
config = Config(connect_timeout=5, retries={'max_attempts': 0})
session = boto3.Session()

s3_client = session.client('s3', config=config)

# Print out bucket names
contents = s3_client.list_objects_v2(Bucket=bucket_name)['Contents']
print(contents)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-07
    • 2020-02-06
    • 2016-06-06
    • 1970-01-01
    • 2019-05-18
    • 2018-10-04
    • 2020-02-21
    • 2021-06-25
    相关资源
    最近更新 更多