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

jem*_*111 2 caching node.js google-cloud-storage google-cloud-platform firebase-storage

我在为存储桶中的图像设置缓存控制最大年龄标头时遇到问题。如果有什么区别的话,图像实际上存储在 firebase 存储桶中。

我可以成功上传图像并接收响应中的文件对象。然后,我将文件的缓存控制 max-age 标头设置为 31536000,如下所示:

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')
} , (err, file, apiResponse) => {
    file.setMetadata({
        cacheControl: 'public, max-age=31536000'
     });
});
Run Code Online (Sandbox Code Playgroud)

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

奇怪的是,如果我通过httphttp://storage.googleapis.com/my-bucket-name.appspot.com/storageBucketName/imageName.png )访问公共网址,则缓存控制最大年龄标头设置为31536000,正如预期的那样。

如何为通过 https 提供的公共 url 设置此标头?谢谢!

Jac*_*ott 5

在这里找到了答案:

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) => {
});
Run Code Online (Sandbox Code Playgroud)

或者我是怎么做的:

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();
  });
Run Code Online (Sandbox Code Playgroud)