2016-12-14 17 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

確實確定了它!非常感謝。 '':-) –

相關問題