2
votes

I'm having trouble setting the cache-control max-age header for images in my storage bucket. The images are actually stored in a firebase storage bucket, if that makes any difference.

I can successfully upload an image receive the file object in the response. I then set the cache-control max-age header for the file to 31536000 like:

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

When I visit the image at the public url (https://storage.googleapis.com/my-bucket-name.appspot.com/storageBucketName/imageName.png) the cache-control max-age header is set to 3600, which is the default.

Oddly enough if I visit the public url over http (http://storage.googleapis.com/my-bucket-name.appspot.com/storageBucketName/imageName.png) the cache-control max-age header is set to 31536000, as expected.

How can I set this header for the public url available over https? Thanks!

2

2 Answers

3
votes

Found the answer here:

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

so for your code:

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) => {
});

or how I do it:

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
votes

If anyone is interested - the issue I had was no space between the comma and the next value.

ie.

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

will not work,

while:

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

will.