boto3:启用 CDN 时如何与 DigitalOcean S3 Spaces 交互

问题描述

我正在使用启用 CDN 的 DigitalOcean Spaces(S3 存储协议)。

s3 上的任何文件都可以通过给定形式的直接 URL 访问:

https://my-bucket.fra1.digitaloceanspaces.com/<file_key>

如果 CDN 已启用,则可以通过其他 CDN URL 访问该文件:

https://my-bucket.fra1.cdn.digitaloceanspaces.com/<file_key>

其中 fra1region_name

当我使用 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 空间。

这是我也尝试过的,但没有奏效:

更新 基于@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,我现在可以连接到数字海洋,但发生了另一个错误:

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'))

但它仍然失败。

解决方法

boto3 是 Amazon S3 而非 Digital Ocean Spaces 的客户端库。因此,boto3 将无法识别 CDN URL fra1.cdn.digitaloceanspaces.com,因为它是由 Digital Ocean 提供的,并且带有 CDN 的 URL 不是受支持的 URI 模式之一。我不完全了解 CDN 的内部工作原理,所以我猜想实现这种重定向到正确的 URL 可能会遇到挑战。

现在已经很清楚了,让我们看看如何获​​得预签名的 CDN URL。假设您的 CDN URL 是 https://fra1.cdn.digitaloceanspaces.com,您的空间名称是 my-space。我们希望获得存储在空间中的对象 my-example-object 的预签名 URL。

import os
import boto3
from botocore.client import Config

# Initialize the client
session = boto3.session.Session()
client = session.client('s3',region_name='fra1',endpoint_url='https://fra1.digitaloceanspaces.com',# Remove `.cdn` from the URL
                        aws_access_key_id=os.getenv('SPACES_KEY'),aws_secret_access_key=os.getenv('SPACES_SECRET'),config=Config(s3={'addressing_style': 'virtual'}))

# Get a presigned URL for object 
url = client.generate_presigned_url(ClientMethod='get_object',Params={'Bucket': 'my-space','Key': 'my-example-object'},ExpiresIn=300)

print(url)

预签名的 URL 将类似于:

https://my-space.fra1.digitaloceanspaces.com/my-example-object?AWSAccessKeyId=EXAMPLE7UQOTHDTF3GK4&Content-Type=text&Expires=1580419378&Signature=YIXPlynk4BALXE6fH7vqbnwjSEw%3D

手动或以编程方式在中间添加 cdn,以防万一,您的最终网址将变为:

https://my-space.fra1.cdn.digitaloceanspaces.com/my-example-object?AWSAccessKeyId=EXAMPLE7UQOTHDTF3GK4&Content-Type=text&Expires=1580419378&Signature=YIXPlynk4BALXE6fH7vqbnwjSEw%3D

这是您的 CDN 网址。

,

根据@Amit Singh 的回答,我对这个问题进行了额外的研究。

找到了对我有帮助的答案 herehere

为了使 boto3 预签名网址正常工作,我对 clientgenerate_presigned_url() 参数进行了以下更新。

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',retries={
                             'max_attempts': 10,'mode': 'standard'
                         },s3={'addressing_style': "virtual"},))
...

response = s3_client.generate_presigned_url('get_object',Params={'Bucket': bucket_name,'Key': object_name},ExpiresIn=3600,HttpMethod=None
                                            )

之后,应该在区域名称之后添加.cdn域部分。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...