【问题标题】:Google Cloud Storage vs Google Cloud CDN谷歌云存储与谷歌云 CDN
【发布时间】:2020-09-10 05:35:41
【问题描述】:

我有几个视频内容,我通过我的 Django Google App Engine 应用程序通过我的 Google Cloud Storage 共享,这些内容带有与过期时间相关联的签名 url 机制。

def get_signed_url(self, entity):
    blob = bucket.get_blob(entity.url)
    logging.info(entity.url)
    expiration_time = timezone.now() + timedelta(minutes=120)
    signed_url = blob.generate_signed_url(expiration_time)
    logging.info(signed_url)

    return signed_url

虽然已在 [here][1] 中解释了 GCS 和 Google Cloud CDN 的可能使用关系,但这是否适用于通过 Google Cloud Storage 流式传输视频内容(MP4 或 MPEG-DASH 与 MP4),正如它所提到的拥有一个隐式 CDN 本身。

如果使用 Google CDN 是广播在线视频内容的一种更明智的方式,那么实现这一目标的最佳策略是什么?如何在我当前的 Google Cloud Storage 实施基础上使用 Google Cloud CDN?

【问题讨论】:

    标签: django google-app-engine google-cloud-platform google-cloud-storage google-cloud-cdn


    【解决方案1】:

    回答您的问题:我如何在您的 django 上使用 Google Cloud Storage 的当前实施来使用 Google Cloud CDN。您可以使用 django-storages

    在下面建立您的实现

    requirements.txt(最新版本:05-31-2020

    ...
    django-storages==1.19.1
    google-cloud-storage==1.28.1
    

    example/example/settings.py

    ...
    ...
    SB_SA_FILE = os.environ.get('STORAGE_BUCKETS_FILE',
                                'storageBucketsBackendServiceKey.json')
    STATICFILES_STORAGE = 'example.lib.storages.GoogleStaticFilesStorage'  # static
    DEFAULT_FILE_STORAGE = 'example.lib.storages.GoogleMediaFilesStorage'  # media
    GS_AUTO_CREATE_BUCKET = True
    GS_DEFAULT_ACL = 'publicRead'
    GS_CREDENTIALS = service_account.Credentials.from_service_account_file(
        f'/usr/src/app/{SB_SA_FILE}'
    )
    GS_BUCKET_NAME = os.environ.get('GS_BUCKET_NAME')
    CDN_HOSTNAME = os.environ.get('CDN_HOSTNAME', '')
    

    example/example/lib/storages.py

    from django.conf import settings
    from storages.backends.gcloud import GoogleCloudStorage
    
    
    
    class GoogleMediaFilesStorage(GoogleCloudStorage):
    
        def _save(self, name, content):
            name = f'{settings.MEDIA_URL[1:]}{name}'
            return super()._save(name, content)
    
        def url(self, name):
            """
            @brief      for implementation of CDN using image field url
            @return     Dynamic return of CDN or local URL
            """
            if settings.CDN_HOSTNAME:
                url = f'{settings.CDN_HOSTNAME}/{name}'
                return url
            return super().url(name)
    
    
    class GoogleStaticFilesStorage(GoogleCloudStorage):
        def url(self, name):
            name = f'static/{name}'
            if settings.CDN_HOSTNAME:
                url = f'{settings.CDN_HOSTNAME}/{name}'
                return url
            return super().url(name)
    

    最后,您需要使用CDN_HOSTNAME 环境变量运行您的django 应用程序。 CDN_HOSTNAME 环境变量的值必须是您的 Google Cloud 全局负载均衡器中映射的域,您所需的 Cloud Storage 设置为后端存储桶

    【讨论】:

    • 感谢 Dean,非常感谢。使用签名的 url 和访问限制怎么样?我如何使用它们 Google CDN ?
    • 嗨@SuperEye,我从来没有尝试过使用signedURLs .. 但是根据这里的文档:django-storages.readthedocs.io/en/latest/backends/gcloud.html你只需要删除GS_DEFAULT_ACL = 'publicRead'
    • 但是,这并没有提供一种机制来限制对某些文件的访问。为此,应采用signed_url 机制。
    • 哦.. 仅限特定文件.. 所以是的,我猜你是对的.. 你可能需要在你的 django 应用程序上为你的案例添加一些逻辑
    • 它应该直接通过 Storage/CDN 进行通信。假设您有一个返回图像的 API。将在客户端上传递的图像 URL 让我们说浏览器将是您在 Storge/CDN 中托管的图像的 URL
    【解决方案2】:

    虽然 Google Cloud Storage 利用了 CDN 基础架构的“部分”,但它只是一个便利层,无法控制缓存密钥、失效,并且需要公共存储桶(与签名 URL 不一致)或每个对象缓存-要设置的控制元数据。

    从存储桶中导出大量数据(例如 HLS/DASH 视频)的成本也相当高 - 对于北美,根据数量,它的范围在 0.12 美元到 0.08 美元之间。北美出口的 Cloud CDN 价格从 0.08 美元到 0.02 美元(按 PB 计算)不等。

    您还应该查看 Cloud CDN 中的 Signed URLs and Signed Cookies 支持,它允许您保护您的视频片段免遭未经授权的每个用户访问 - 类似于 GCS 签名的 URL。

    TL;DR: GCS 及其缓存模式非常方便,适用于小流量,但如果您打算提供几百 GB(更不用说更多),设置一个HTTPS 负载均衡器 + 存储桶前的 Cloud CDN 将为您提供更大的灵活性并降低成本。

    (如果有帮助,我是 Cloud CDN 的 PM!)

    【讨论】:

    • 你的意思是说“在存储桶前面设置一个HTTPS负载均衡器+云CDN”你是说在对象存储桶前面吗?..
    猜你喜欢
    • 2015-07-17
    • 2022-12-03
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    • 2017-05-11
    • 2020-09-29
    相关资源
    最近更新 更多