【问题标题】:Serving images directly from GCS in GAE using Blobstore API and Images API在 GAE 中使用 Blobstore API 和图像 API 直接从 GCS 提供图像
【发布时间】:2014-01-29 20:48:52
【问题描述】:

关于 Blobstore 和 Google Cloud Storage (GCS) 的许多问题和答案都是两三年前的事情,而这些年来情况发生了巨大变化。 GCS 不再是一个独立的服务。现在是integrated 进入 Google App Engine (GAE)。

Google 似乎非常努力地推动 GCS,例如,Blobstore is deprecated

此处用于将文件写入 Blobstore 的文件 API 功能已 已弃用,并将在将来的某个时间删除,在 倾向于将文件写入 Google Cloud Storage 并使用 Blobstore 为他们服务。

我认为现在是切换到 GCS 的时候了。

例如,www.example.com 是基于 GAE 构建的站点,而 example.jpg 是存储在 GCS 上的图像,我想使用 url http://www.example.com/images/example.jpg 提供图像

这在过去是不可能的,但现在有了integration,这成为可能。

我发现了这个:

https://developers.google.com/appengine/docs/python/googlecloudstorageclient/ 说:

当 Blobstore API 与 Images API 一起使用时,您会得到一个 提供图像的强大方式,因为您可以直接提供图像 来自 GCS,绕过 App Engine 应用程序,从而节省实例时间 费用。

我确实知道如何“绕过 App Engine 应用”。有没有关于如何在使用 Blobstore API 和 Images API 提供图像时绕过 GAE 的示例?

【问题讨论】:

    标签: python google-app-engine google-cloud-storage blobstore


    【解决方案1】:

    您现在实际上根本不需要使用 BlobStore。以下将用于获取用于提供存储在 GCS 中的图像的图像 API URL:

    from google.appengine.api import images
    
    images.get_serving_url(None, filename='/gs/<bucket>/<object>'))
    

    【讨论】:

      【解决方案2】:

      如果您使用 www 作为 GAE Cname,则从“www”提供图像不是一个好主意,要提供图像,您可以创建一个新的子域。在我们的例子中,我们使用的是 cdn.example.com,我们提供我们的图片,如http://cdn.example.com/images/example.jpg

      我们怎么做。

      创建一个名为 cdn.example.com 的 GCS 存储桶,并将您的图像放在 /images 路径下。

      指定您的索引和 404 页面,就可以了。

      更多关于https://cloud.google.com/storage/docs/website-configuration?hl=en

      【讨论】:

        【解决方案3】:

        说明在这里:https://developers.google.com/appengine/docs/python/images/functions#Image_get_serving_url

        从托管在 Google Cloud Storage 中的图片开始。

        首先,使用 Blobstore API 的 create_gs_key() 函数为您的 GCS 图像对象生成 Blob 密钥。然后,将该 blob 密钥传递给 Image API 的 get_serving_url() 函数。

        Image API 将为您提供一个特殊的 URL,它会跳过您的应用引擎应用程序并直接提供图像。

        【讨论】:

        • 谢谢,但事实证明它生成的链接类似于lh4.ggpht.com/… 这不是我想要的。我需要使用我自己的自定义域 URL 来提供图像,例如 example.com/images/example.jpg 如果不推荐使用 Blobstore,这在 GAE 中似乎是不可能的。
        • 如果您的域是 CNAME 的 Google Cloud Storage 存储桶,例如“images.example.com”,您可以直接从 Google Cloud Storage 以 images.example.com/example 的形式提供它们。 .jpg但是,您不能将“www.example.com”同时用于应用引擎和谷歌云存储。
        • 很好,但是有没有办法通过跳过应用引擎直接从 GCS 提供图像以外的内容,比如视频或音频?
        • @HarshalPatil 您想查看访问控制列表 (ACL)。是的,您可以将存储桶(目录)或对象(图像)分配为公开可读。你会想要:“x-goog-acl: public-read” 文档:developers.google.com/storage/docs/accesscontrol#defaultobjects
        猜你喜欢
        • 2023-03-11
        • 1970-01-01
        • 2015-05-22
        • 1970-01-01
        • 2013-03-11
        • 2014-05-05
        • 1970-01-01
        • 2017-05-01
        • 1970-01-01
        相关资源
        最近更新 更多