在 Heroku 上使用 Boto 时,无法连接到名称中带有句点的 S3 存储桶

Posted

技术标签:

【中文标题】在 Heroku 上使用 Boto 时,无法连接到名称中带有句点的 S3 存储桶【英文标题】:Can't connect to S3 buckets with periods in their name, when using Boto on Heroku 【发布时间】:2015-02-23 11:33:50 【问题描述】:

我们在尝试使用 Boto 连接到 S3 存储桶时遇到证书错误。奇怪的是,只有在 Heroku 上运行时访问名称中带有句点的存储桶时才会出现这种情况。

from boto.s3.connection import S3Connection
conn = S3Connection(our_s3_key, our_s3_secret)
bucket = conn.get_bucket('ourcompany.images')

引发以下错误:

证书错误:主机名“ourcompany.images.s3.amazonaws.com” 与“*.s3.amazonaws.com”、“s3.amazonaws.com”中的任何一个都不匹配

但是相同的代码在本地运行时可以正常工作,如果存储桶名称是“ourcompany-images”而不是“ourcompany.images”,也可以在 Heroku 上运行

【问题讨论】:

【参考方案1】:

根据relevant github issue,将这个添加到配置中:

[s3]
calling_format = boto.s3.connection.OrdinaryCallingFormat

或者,在实例化 S3Connection 时指定 calling_format

from boto.s3.connection import OrdinaryCallingFormat

conn = S3Connection(access_key, secret_key, calling_format=OrdinaryCallingFormat())

代码在本地为您工作,但在 heroku 上不起作用,很可能是因为使用了不同的 python 版本。我怀疑你在 heroku 上使用 2.7.9 运行时,它有 enabled certificate checks for stdlib http clients。

【讨论】:

美丽-谢谢! 对于所有使用流行的 Django-Storages 包装器的人,这是通过应用程序的 settings.py 完成的,如下所述:django-storages.readthedocs.org/en/latest/backends/… 和这里:bitbucket.org/david/django-storages/issue/181/… 可以说这些链接“解释”了问题!但布拉德·博德在bitbucket.org/david/django-storages/issues/181/… 上的评论对我来说是解决这个问题的原因。 在 Python 3.5.0 中,还必须指定区域才能使用 conn = s3.connect_to_region('us-west-2', calling_format=OrdinaryCallingFormat()) 有没有办法为 boto3 解决这个问题?

以上是关于在 Heroku 上使用 Boto 时,无法连接到名称中带有句点的 S3 存储桶的主要内容,如果未能解决你的问题,请参考以下文章

无法在 heroku 上使用 node.js 连接到 mongolab

无法连接到主机。 Heroku 和不和谐机器人问题

Pipenv无法在远程服务器(Heroku)上安装boto3

无法连接到服务器:PostgreSQL -Heroku

无法使用 pgAdmin III 客户端连接到 Heroku DB

无法将 Heroku 连接到自定义 Google 域