Google Cloud Storage sets cache control with URL subscription

We use signed URLs to download from the browser. I could not figure out how to set the cache control header at boot time.

We use the gcloud-node library to sign URLs:

var bucket = gcs.bucket('mybucket');
var file = bucket.file('image.jpg');

var expireDate = new Date
expireDate.setDate(expireDate.getDate() + 1);

file.getSignedUrl({
  action: 'write',
  expires: expireDate,
  contentType: 'image/jpeg'
}, function (err, signedUrl) {
  if (err) {
    console.error('SignedUrl error', err);
  } else {
    console.log(signedUrl);
  }
});

How to configure Cache-Control headers when uploading a file to GCS?
The download code is executed in the browser:

var signedUrl = ...; // get from nodejs server
var fileList = this.files;

var file = fileList[0];

jQuery.ajax({
  url: signedUrl,
  type: 'PUT',
  data: file,
  processData: false,
  contentType: 'image/jpeg'
})
0
source share
2 answers

It is possible, but the documentation is terrible. To get started, you need to configure CORS in the basket you load into:

gsutil cors set cors.json gs://bucket-name

Where it cors.jsoncontains something like:

[{
    "maxAgeSeconds": 3600,
    "method": ["GET", "PUT", "POST"],
    "origin": [
        "http://localhost:3000"
    ],
    "responseHeader": ["Content-Type", "Cache-Control"]
}]

"Cache-Control" "responseHeader". , , Cache-Control. fetch :

fetch(uploadUrl, {
    method: 'PUT',
    body: blob,
    headers: {
        'Content-Type': blob.type,
        'Cache-Control': 'public, max-age=31536000',
    },
});
0

All Articles