Skip to content

Instantly share code, notes, and snippets.

@poindextrose
Last active December 4, 2020 00:32
Show Gist options
  • Save poindextrose/fb2066610ee054609d7c to your computer and use it in GitHub Desktop.
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
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);
});
@amanaf-korewireless
Copy link

Hi,
I need to create a signed url of an object(uploaded with Google Developer Console) stored in gcloud storage using node.js.

Using above code i got output with a url like this

https://storage.googleapis.com/testBucketName123/tasksel.png?GoogleAccessId=keyId@iprojectName.iam.gserviceaccount.com&Expires=NaN&Signature=MmI5cQ3NINpOOP%2Fer02R0LxRZJAHplmstvofDXU139VNmZ0MsxpAOTGg6JUHBr8EztiTdR7cNNltoSb0zpM2DCRK5UpDjlrJ6WriSL2uyDsGWgfdx4M1vS0ussRm9BHAT31tqo0U5cdzgJd5rjakPMV06NUw0YYQn1oAEzE6JwThFfUZa%2FexddP763yDOQDKh5sJxW9go3JNmam%2Bk8qq0wJnVP5GNhl3JGlKW7l5AtimUwaI%2BViasX6LNmvwtoyM4%2Fg6Ebx%2BnI9%2Bk2LxWA3c8MUypeEZj0W8AWv%2BAWjf1bzeCEMTOjxV53bo1SA0zmUGjE6GDVd9PnYomwJ88v2h3Q%3D%3D

But i got output when open this url with browser is

<Error>
<Code>SignatureDoesNotMatch</Code>
<Message>
The request signature we calculated does not match the signature you provided. Check your Google secret key and signing method.
</Message>
<StringToSign>GET NaN /testBucketName123/tasksel.png</StringToSign>
</Error>

What is the actual issue.? any idea.

@luis-agm
Copy link

Did you find a solution? @manafGadgeon

@Metroxe
Copy link

Metroxe commented Dec 4, 2020

Should that be a + on line 26 instead of a *.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment