【问题标题】:The images from Google Cloud bucket are not immediately updated来自 Google Cloud 存储桶的图像不会立即更新
【发布时间】:2016-12-14 23:20:04
【问题描述】:

更新谷歌云存储桶中的图片时,即使图片更新成功,url也会服务旧版本一段时间(几分钟,例如5分钟左右)。

我们使用的链接如下:

https://storage.googleapis.com/<bucket-name>/path/to/images/1.jpg

更新图像的代码的相关部分是:

var storageFile = bucket.file(imageToUpdatePath);
var storageFileStream = storageFile.createWriteStream({
  metadata: {
    contentType: req.file.mimetype
  }
});

storageFileStream.on('error', function(err) {
   ...
});

storageFileStream.on('finish', function() {
  // cloudFile.makePublic after the upload has finished, because otherwise the file is only accessible to the owner:
  storageFile.makePublic(function(err, data) {
    //if(err)
    //console.log(err);
    if (err) {
      return res.render("error", {
        err: err
      });
    }
    ...
  });
});
fs.createReadStream(filePath).pipe(storageFileStream);

这看起来像是 Google Cloud 端的缓存问题。如何解决?更新后如何在请求的url获取更新的图片?

在 Google Cloud 管理员中,新图像确实正确显示。

【问题讨论】:

    标签: javascript node.js caching google-cloud-storage


    【解决方案1】:

    默认情况下,公共对象最多缓存 60 分钟 - 请参阅 Cache Control and Consistency。要解决此问题,您应该在创建/上传对象时将对象的缓存控制属性设置为私有。在您上面的代码中,这将位于 metadata 块中,如下所示:

    var storageFileStream = storageFile.createWriteStream({
      metadata: {
        contentType: req.file.mimetype,
        cacheControl: 'private'
      }
    });
    

    【讨论】:

    • 确实修复了它!太感谢了。 :-)
    猜你喜欢
    • 2017-07-22
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    • 2020-05-05
    • 2017-05-22
    • 1970-01-01
    • 2014-10-09
    相关资源
    最近更新 更多