Create signed URL for Google Cloud Storage using NodeJS


Stuck Kennedy

I'm trying to create a signature for a privately stored file in Google Cloud Storage; so I can distribute a limited time link.

Currently doing this and the signature is too short...where am I going wrong?

var crypto = require("crypto");

var ttl = new Date().getTime() + 3600;
var id = 'the_target_file.txt';
var bucketName = 'bucket_name';
var POLICY_JSON = "GET\n" + "\n" + "\n" + ttl + "\n" + '/' + bucketName + '/' + id;

// stringify and encode the policy
var stringPolicy = JSON.stringify(POLICY_JSON);
var base64Policy = Buffer(stringPolicy, "utf-8").toString("base64");

// sign the base64 encoded policy
var privateKey = "MY_PRIVATE_KEY";
var sha256 = crypto.createHmac("sha256", privateKey);
var signature = sha256.update(new Buffer(base64Policy, "utf-8")).digest("base64");

console.log ( signature );
Stuck Kennedy

Realised I was doing something wrong...I was hashing the policy string, not signing it. Now the following code gives me the correct output.

var crypto = require("crypto");
var fs = require("fs");

var expiry = new Date().getTime() + 3600;
var key = 'the_target_file';
var bucketName = 'bucket_name';
var accessId = 'my_access_id';
var stringPolicy = "GET\n" + "\n" + "\n" + expiry + "\n" + '/' + bucketName + '/' + key; 
var privateKey = fs.readFileSync("gcs.pem","utf8");
var signature = encodeURIComponent(crypto.createSign('sha256').update(stringPolicy).sign(privateKey,"base64"));   
var signedUrl = "https://" + bucketName + ".commondatastorage.googleapis.com/" + key +"?GoogleAccessId=" + accessId + "&Expires=" + expiry + "&Signature=" + signature;

console.log(signedUrl);

For completeness... here's a PHP version that does the same thing I use to check the results

$expiry = time() + 3600;
$key = 'the_target_file';
$bucketName = 'bucket_name';
$accessId = 'my_access_id';
$stringPolicy = "GET\n\n\n".$expiry."\n/".$bucketName."/".$key;
$fp = fopen('gcs.pem', 'r');
$priv_key = fread($fp, 8192);
fclose($fp);
$pkeyid = openssl_get_privatekey($priv_key,"password"); 
if (openssl_sign( $stringPolicy, $signature, $pkeyid, 'sha256' )) {
    $signature = urlencode( base64_encode( $signature ) );    
    echo 'https://'.$bucketName.'.commondatastorage.googleapis.com/'.
              $key.'?GoogleAccessId='.$accessId.'&Expires='.$expiry.'&Signature='.$signature;
}

Related


Create signed URL for Google Cloud Storage using NodeJS

Stuck Kennedy I'm trying to create a signature for a privately stored file in Google Cloud Storage; so I can distribute a limited time link. Currently doing this and the signature is too short...where am I going wrong? var crypto = require("crypto"); var ttl

Creating signed URLs for Google Cloud Storage using NodeJS

stukennedy I'm trying to create a signature for a privately stored file in Google Cloud Storage; so that I can distribute a time-limited link. Currently doing this and it makes a signature that's too short ... where am I going wrong? var crypto = require("cryp

Creating signed URLs for Google Cloud Storage using NodeJS

stukennedy I'm trying to create a signature for a privately stored file in Google Cloud Storage; so that I can distribute a time-limited link. Currently doing this and it makes a signature that's too short ... where am I going wrong? var crypto = require("cryp

Upload file to Google Cloud Storage signed URL using Python

Raj Chaudhary Can someone help me with the code, preferably using the Requests library to upload a file to a Google Cloud Storage signed URL in Python? Lundin Custer You can check out this sample code on Github . It shows a portable way to achieve this using P

Upload file to Google Cloud Storage signed URL using Python

Raj Chaudhary Can someone help me with the code, preferably using the Requests library to upload a file to a Google Cloud Storage signed URL in Python? Lundin Custer You can check out this sample code on Github . It shows a portable way to achieve this using P

Upload file to Google Cloud Storage signed URL using Python

Raj Chaudhary Can someone help me with the code, preferably using the Requests library to upload a file to a Google Cloud Storage signed URL in Python? Lundin Custer You can check out this sample code on Github . It shows a portable way to achieve this using P

upload file to google cloud storage with signed url using ajax

David I try to upload a file to my Google Cloud Storage using signed url. I have set CORS parameters on my bucket using gsutil Browser sends ajax request to Django backend to get signed url Django backend makes request to GCS to create signed url The signed ur

Upload file to Google Cloud Storage signed URL using Python

Raj Chaudhary Can someone help me with the code, preferably using the Requests library to upload a file to a Google Cloud Storage signed URL in Python? Lundin Custer You can check out this sample code on Github . It shows a portable way to achieve this using P

Upload file to Google Cloud Storage signed URL using Python

Raj Chaudhary Can someone help me with the code, preferably using the Requests library to upload a file to a Google Cloud Storage signed URL in Python? Lundin Custer You can check out this sample code on Github . It shows a portable way to achieve this using P

Provide callback URL in Google Cloud Storage signed URL

username When uploading to GCS (Google Cloud Storage) using BlobStore's createUploadURL function , I can provide a callback along with header data that will be sent to the callback URL. There doesn't seem to be a way to use GCS's signed URLs I know there are "

Provide callback URL in Google Cloud Storage signed URL

username When uploading to GCS (Google Cloud Storage) using BlobStore's createUploadURL function , I can provide a callback along with header data that will be sent to the callback URL. There doesn't seem to be a way to use GCS's signed URLs I know there are "

Provide callback URL in Google Cloud Storage signed URL

username When uploading to GCS (Google Cloud Storage) using BlobStore's createUploadURL function , I can provide a callback along with header data that will be sent to the callback URL. There doesn't seem to be a way to use GCS's signed URLs I know there are "

Provide callback URL in Google Cloud Storage signed URL

username When uploading to GCS (Google Cloud Storage) using BlobStore's createUploadURL function , I can provide a callback along with header data that will be sent to the callback URL. There doesn't seem to be a way to use GCS's signed URLs I know there are "

Provide callback URL in Google Cloud Storage signed URL

username When uploading to GCS (Google Cloud Storage) using BlobStore's createUploadURL function , I can provide a callback along with header data that will be sent to the callback URL. There doesn't seem to be a way to use GCS's signed URLs I know there are "