无法使用google-cloud-node设置Cache-Control max-age标头

时间:2017-03-14 14:18:56

标签: node.js caching google-cloud-storage google-cloud-platform firebase-storage

我在设置存储桶中图像的缓存控制max-age标头时遇到问题。这些图像实际上存储在一个firebase存储桶中,如果这有任何区别的话。

我可以成功上传图片,在响应中接收文件对象。然后我将文件的cache-control max-age标头设置为31536000,如:

substring

当我在公共网址(https://storage.googleapis.com/my-bucket-name.appspot.com/storageBucketName/imageName.png)访问图片时,缓存控制max-age标头设置为3600,这是默认设置。

奇怪的是,如果我通过 http http://storage.googleapis.com/my-bucket-name.appspot.com/storageBucketName/imageName.png)访问公共网址,则缓存控制max-age标头会按预期设置为31536000。

如何通过https为公共网址设置此标头?谢谢!

2 个答案:

答案 0 :(得分:2)

在这里找到答案:

https://github.com/GoogleCloudPlatform/google-cloud-node/issues/1087

所以对于你的代码:

const gcloud = require('google-cloud');
const gcs = gcloud.storage({credentials: myCredentials});
const storageBucket = gcs.bucket(myConfig);

storageBucket.upload('path/to/image', {
    public: true,
    destination: storageBucket.file('storageBucketName/imageName.png')
    metadata: {
        cacheControl: 'public, max-age=14400'
    }

} , (err, file, apiResponse) => {
});

或我是如何做到的:

const cf = bucket.file(fn);
fs.createReadStream(path.join(topDir, fn))
  .pipe(cf.createWriteStream({
    predefinedAcl: 'publicRead',
    metadata: {
      contentType: mime.lookup(fn) || 'application/octet-stream',
      cacheControl: 'public, max-age=315360000',
    },
    gzip: true
  }))
  .on('error', err => {
    logErr(err);
      resolve();
  })
  .on('finish', () => {
    resolve();
  });

答案 1 :(得分:0)

如果有人感兴趣-我遇到的问题是逗号和下一个值之间没有空格。

即。

ref.updateMetadata({cacheControl: 'public,max-age=3600'}); 

不起作用,

同时:

ref.updateMetadata({cacheControl: 'public, max-age=3600'}); 

会。

相关问题