【问题标题】:Express caching Image stream from Google Cloud Storage快速缓存来自 Google Cloud Storage 的图像流
【发布时间】:2015-10-06 18:16:29
【问题描述】:

我正在使用 express 4 提供图像流:

app.get('/v1/images/:Uid/', function(req, res) {
    var gcsbucket = gcs.bucket('bucket'),
    remoteReadStream = gcsbucket.file(req.params.Uid+'.jpg').createReadStream();
    remoteReadStream.pipe(res);
});

(图片存储在谷歌云存储中)

我正在尝试找到一种方法来告诉浏览器缓存我正在提供的图像。 我尝试添加:

res.set('Cache-Control', 'public, max-age=31557600');

但是当我在http://server/v1/images/1234 访问 API 时,这并没有改变任何东西,它总是重新加载图像。

在使用流服务器端时,告诉浏览器缓存响应的正确方法是什么?

【问题讨论】:

    标签: node.js express stream google-cloud-storage


    【解决方案1】:

    这确实有效:

    app.get('/v1/images/:Uid/', function(req, res) {
        var gcsbucket = gcs.bucket('bucket'),
        remoteReadStream = gcsbucket.file(req.params.Uid+'.jpg').createReadStream();
        res.set('Cache-Control', 'public, max-age=31557600');
        remoteReadStream.pipe(res);
    });
    

    奇怪的是,当您直接访问 API 时,浏览器从不缓存。但是,当您将图像嵌入到 html 页面中时,图像会被缓存:

    <img src="http://server/v1/images/1234">
    

    这对我的用例来说已经足够了,但是如果有人有原因呢?

    【讨论】:

      猜你喜欢
      • 2019-06-19
      • 2020-08-15
      • 2021-01-06
      • 1970-01-01
      • 1970-01-01
      • 2019-04-13
      • 2016-06-11
      • 1970-01-01
      相关资源
      最近更新 更多