Skip to content

Instantly share code, notes, and snippets.

@MonksterFX
Last active August 19, 2020 07:50
Show Gist options
  • Save MonksterFX/687dc09addbf2b1d119d45674f994daf to your computer and use it in GitHub Desktop.
Save MonksterFX/687dc09addbf2b1d119d45674f994daf to your computer and use it in GitHub Desktop.
generate signed url for google cloud storage in javascript
const { Storage } = require('@google-cloud/storage');
const storage = new Storage({
keyFilename: '<file_path>',
projectId: '<projectId>',
});
async function generateSignedUrl() {
// These options will allow temporary write access to the file
const options = {
version: 'v4',
action: 'write',
expires: Date.now() + 15 * 60 * 1000, // 15 minutes
contentType: 'image/jpeg',
};
// Get a v4 signed URL for uploading file
const [url] = await storage
.bucket('<bucket-name>')
.file('<upload path>')
.getSignedUrl(options);
console.log('Generated PUT signed URL:');
console.log(url);
return url;
}
generateSignedUrl().catch((err) => console.error(err));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment