2016-12-14 67 views
0

更新Google云端存储分区中的图片时,即使图片更新成功,网址也会为旧版本提供一段时间(几分钟,例如5分钟左右)。来自Google云端存储分区的图片不会立即更新

链接我们使用的模样:

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云端管理员中,新图片确实显示正确。

回答

3

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

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

确实确定了它!非常感谢。 '':-) –

相关问题