【问题标题】:boto3: How to interract with DigitalOcean S3 Spaces when CDN is enabledboto3:启用 CDC 时如何与 DigitalOcean S3 Spaces 交互
【发布时间】: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


    【解决方案1】:

    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,以防您需要,这样您的最终 URL 将变为:

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

    这是您的 CDN 网址。

    【讨论】:

    • 感谢您的详细解答。请检查我上面问题中的更新部分。我已经尝试过你的建议,bug 还有签名的另一个问题。
    • @arturkuchynski 更新了我对 URL 更改的答案。您只需将 config=Config(s3={'addressing_style': 'virtual'}) 参数传递给您的 S3 客户端即可生成它。
    【解决方案2】:

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

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

    为了使 boto3 预签名 URL 正常工作,我对 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 域部分应添加在区域名称之后。

    【讨论】: