gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 03:44:29 28 4
gpt4 key购买 nike

我正在使用启用了 CDN 的 DigitalOcean Spaces(S3 存储协议(protocol)) .
s3 上的任何文件都可以通过给定形式的直接 URL 访问:

https://my-bucket.fra1.digitaloceanspaces.com/<file_key>
如果 CDN 已启用 ,可以通过附加的 CDN URL 访问该文件:
https://my-bucket.fra1.cdn.digitaloceanspaces.com/<file_key>
在哪里 fra1地区名 .
当我使用 boto3 Python SDK,文件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.comdefault_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)
博托3 guide用于 digital ocean 空间。
这是我也尝试过的方法,但没有成功:
  • 生成presigned url's

  • 更新
    基于@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'))
    但它仍然失败。

    最佳答案

    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 .我们想获得一个对象的预签名 URL my-example-object存储在空间中。

    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 网址。

    关于python - boto3:启用 CDN 时如何与 DigitalOcean S3 Spaces 交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65394319/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com