在我的 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 header 吗?
最佳答案
这是一个打开设置缓存控件的示例:
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:
关于python - GAE Python GCS 文件名访问旧文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27924005/