node.js - 无法使用 google-cloud-node 设置 Cache-Control max-age header

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

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

我可以成功上传图像并接收响应中的文件对象。然后,我将文件的缓存控制 max-age header 设置为 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'
     });
});

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

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

如何为通过 https 提供的公共(public) URL 设置此 header ?谢谢!

最佳答案

在这里找到答案:

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();
  });

关于node.js - 无法使用 google-cloud-node 设置 Cache-Control max-age header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42788488/

相关文章:

java - 是否不支持 meta http-equiv 值缓存控制?

google-cloud-storage - 我可以从网址上传文件到Google云端存储吗?

google-cloud-storage - 它是更改现有对象存储类别的计费操作吗?

node.js - 如何在 feathers/express 中使用 webpack-dev-middleware?

javascript - 来自另一个模块的 Angularjs 路由 Controller

node.js - 无法在express.js中获取正确的页面

php - 随机获取云存储错误 : UNAUTHORIZED

javascript - Node.js 中的面向对象设计

java - simple-spring-memcached 升级到 4.1.1 导致 WrappedCacheException

python - Google appengine 是否缓存外部请求?