Skip to content

Instantly share code, notes, and snippets.

@justinph
Last active January 26, 2018 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinph/917c69df7894932a14d3100facb2dd72 to your computer and use it in GitHub Desktop.
Save justinph/917c69df7894932a14d3100facb2dd72 to your computer and use it in GitHub Desktop.
set cache headers on uploaded file via cloud function
const storage = require('@google-cloud/storage')();
exports.setMeta = function (event, doneCb) {
const file = event.data;
console.log(`Examing file ${file.name}.`);
const gcsBucket = storage.bucket(file.bucket);
const gcsFile = gcsBucket.file(file.name);
if (file.metageneration === '1') {
console.log(`File ${file.name} has been freshly uploaded.`);
return gcsFile
.setMetadata({
cacheControl: 'max-age=604800; stale-if-error=86400; stale-while-revalidate=30, public',
})
.then(() => {
console.log(`Metadata set on ${file.name}.`);
doneCb();
})
.catch((e) => {
console.error(`Error, metadata not set on ${file.name}.`, e);
});
} else {
console.log(`File ${file.name} not new, not setting metadata.`);
doneCb();
}
};
{
"name": "gcs-setmeta",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Justin <justin@fiddlyio.com>",
"license": "ISC",
"dependencies": {
"@google-cloud/storage": "^1.5.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment