【发布时间】:2023-11-11 21:12:02
【问题描述】:
我正在使用启用了CDN 的 DigitalOcean Spaces(S3 存储协议)。
可以通过给定形式的直接 URL 访问 s3 上的任何文件:
https://my-bucket.fra1.digitaloceanspaces.com/<file_key>
如果 CDN已启用,则可以通过额外的 CDN URL 访问该文件:
https://my-bucket.fra1.cdn.digitaloceanspaces.com/<file_key>
其中fra1 是一个区域名称。
当我使用 boto3 SDK for Python 时,文件 URL 如下(由 boto3 生成):
https://fra1.digitaloceanspaces.com/my-bucket/<file_key>
# just note that bucket name is no more a domain part!
这种格式也可以正常工作。
但是,如果启用 CDN - 文件 url 会导致错误:
EndpointConnectionError: Could not connect to the endpoint URL: https://fra1.cdn.digitaloceanspaces.com/my-bucket/<file_key>
假设 endpoint_url 已更改为
default_endpoint=https://fra1.digitaloceanspaces.com
到
default_endpoint=https://fra1.cdn.digitaloceanspaces.com
如何使用正确的 URL 连接到 CDN 而不会出错? 为什么 boto3 使用不同的 URL 格式?在这种情况下是否可以应用任何解决方法?
代码:
s3_client = boto3.client('s3',
region_name=s3_configs['default_region'],
endpoint_url=s3_configs['default_endpoint'],
aws_access_key_id=s3_configs['bucket_access_key'],
aws_secret_access_key=s3_configs['bucket_secret_key'])
s3_client.download_file(bucket_name,key,local_filepath)
boto3 guide 用于 DigitalOcean Spaces。
以下是我也尝试过但没有成功的方法:
更新 基于@Amit Singh 的回答:
正如我之前提到的,我已经用预签名的 URL 尝试过这个技巧。 我有这样的网址
https://fra1.digitaloceanspaces.com/<my-bucket>/interiors/uploaded/images/07IRgHJ2PFhVqVrJDCIpzhghqe4TwK1cSSUXaC4T.jpeg?<presigned-url-params>
存储桶名称出现在端点之后。我不得不手动将它移动到域级别:
https://<my-bucket>.fra1.cdn.digitaloceanspaces.com/interiors/uploaded/images/07IRgHJ2PFhVqVrJDCIpzhghqe4TwK1cSSUXaC4T.jpeg?<presigned-url-params>
现在我可以通过这个 URL 连接到 Digital Ocean,但是出现了另一个错误:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error>
<Code>SignatureDoesNotMatch</Code>
<RequestId>tx00000000000008dfdbc88-006005347c-604235a-fra1a</RequestId>
<HostId>604235a-fra1a-fra1</HostId>
</Error>
作为一种解决方法,我已经厌倦了使用签名s3v4:
s3_client = boto3.client('s3',
region_name=configs['default_region'],
endpoint_url=configs['default_endpoint'],
aws_access_key_id=configs['bucket_access_key'],
aws_secret_access_key=configs['bucket_secret_key'],
config= boto3.session.Config(signature_version='s3v4'))
但它仍然失败。
【问题讨论】:
标签: python amazon-s3 boto3 digital-ocean cdn