【问题标题】:GAE Python GCS filename accesses old fileGAE Python GCS文件名访问旧文件
【发布时间】:2015-03-11 12:27:13
【问题描述】:

在我的 GAE Python 应用程序中,我正在编写代码以将图像存储在 GCS 中。

我把图片写成如下:

bucket_name = os.environ.get(u'BUCKET_NAME', app_identity.get_default_gcs_bucket_name())
filename = u'/{}/{}/image'.format(bucket_name, str(object.key.id()))
mimetype = self.request.POST[u'image_file'].type
gcs_file = cloudstorage.open(filename, 'w', content_type=mimetype,
                                    options={'x-goog-acl': 'public-read'})
gcs_file.write(self.request.get(u'image_file'))
gcs_file.close()

当我第一次使用这段代码写一个特定的文件名时,我可以用它的文件名访问那个文件:

https://storage.googleapis.com/<app>.appspot.com/<id>/image

而且我也可以在 GCS 存储浏览器上点击名称“图片”查看图片。

耶!一切似乎都奏效了。

但是当我将不同的图像上传到相同的文件名时,会发生一些令人困惑的事情:当我在浏览器中显示文件名时,无论是通过 标记还是作为单独浏览器选项卡中的 URL,旧图像出现。然而,当我通过 GCS 存储浏览器显示“图像”时,它显示的是新图像。

顺便说一下,作为一个额外的数据点,虽然我在打开文件进行写入时指定了public-read,但 GCS 存储浏览器页面上该文件的“公开共享”列是空白的。

我尝试在open 语句之前删除文件,即使w 应该充当覆盖,但它没有任何区别。

谁能解释文件名如何继续访问文件的旧版本,即使 GCS 存储浏览器显示新版本,更重要的是,我需要做什么才能使文件名访问新版本?

编辑:

继续研究这个问题,在https://cloud.google.com/storage/docs/accesscontrol找到如下语句:

If you need to ensure that updates become visible immediately, you should set
a Cache-Control header of "Cache-Control:private, max-age=0, no-transform" on
such objects.

但是,我看不到如何使用 Cloudstorage“打开”命令或以任何其他方式从我的 Python 程序中执行此操作。因此,如果这是解决方案,有人可以告诉我如何为我正在创建的这些图像文件设置 Cache-Control 标头吗?

【问题讨论】:

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


    【解决方案1】:

    这里是一个打开设置缓存控制的例子:

    with gcs.open(new_zf.gcs_filename, 'w', content_type=b'multipart/x-zip',
                      options={b'x-goog-acl': b'public-read', b'cache-control': b'private, max-age=0, no-cache'}) as nzf:
    

    取自this respository

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-23
    • 2017-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    相关资源
    最近更新 更多