Last active
January 26, 2018 17:02
-
-
Save justinph/917c69df7894932a14d3100facb2dd72 to your computer and use it in GitHub Desktop.
set cache headers on uploaded file via cloud function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "gcs-setmeta", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "Justin <[email protected]>", | |
"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