Last active
August 19, 2020 07:50
-
-
Save MonksterFX/687dc09addbf2b1d119d45674f994daf to your computer and use it in GitHub Desktop.
generate signed url for google cloud storage in javascript
This file contains 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'); | |
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