我在为存储桶中的图像设置缓存控制最大年龄 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/