Last active
December 4, 2020 00:32
-
-
Save poindextrose/fb2066610ee054609d7c to your computer and use it in GitHub Desktop.
Example on how to create a signed URL using Node.js for uploading a file to Google Cloud Storage
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
var gcloud = require('gcloud'); | |
var uuid = require('uuid'); | |
// Google Cloud Storage Bucket Name | |
const BUCKET_NAME = 'bucket-name'; | |
// Google Developer Console project ID | |
const PROJECT_ID = 'project-1234'; | |
/* Google Developer Console -> API Manager -> Credentials -> | |
Add credentials -> Service account -> JSON -> Create */ | |
const KEY_FILENAME = 'project-0d3d97832ca7.json' // relative path | |
const SECONDS = 1000; // seconds in milliseconds | |
const URL_VALID_DURATION = 30 * SECONDS; | |
var gcs = gcloud.storage({ | |
projectId: PROJECT_ID, | |
keyFilename: KEY_FILENAME | |
}); | |
var filename = uuid.v4(); // v4 is a random uuid | |
var file = gcs.bucket(BUCKET_NAME).file(filename); | |
file.getSignedUrl({ | |
// More documention on options at | |
// https://googlecloudplatform.github.io/gcloud-node/#/docs/v0.24.1/storage/file?method=getSignedUrl | |
action: 'write', | |
expires: Date.now() * URL_VALID_DURATION | |
}, function (err, url) { | |
if (err) { | |
console.error(err); | |
return; | |
} | |
console.log("URL"); | |
console.log("-----"); | |
console.log(url); | |
console.log("-----"); | |
console.log("PUT to this URL to upload to Google Cloud Storage as " + filename); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should that be a
+
on line 26 instead of a*
.