Google 云存储桶中的图像不会立即更新

The images from Google Cloud bucket are not immediately updated

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

我们使用的 link 看起来像:

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 云端的缓存问题。如何解决?更新后如何在请求的url获取更新后的图像?

在 Google 云管理中,新图像 正确显示。

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

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