Create signed URLs for Google Cloud Storage using node.js for direct download from browser

actual code: https://github.com/HenrikJoreteg/google-cloud-signedurl-test-case

I am trying to add the ability of my API to return signed URLs for direct upload to Google Cloud Storage from a client.

Serverside, I use the gcloud SDK for this:

 const gcloud = require('gcloud') const gcs = gcloud.storage({ projectId: 'my project', keyFilename: __dirname + '/path/to/JSON/file.json' }) const bucket = gcs.bucket('bucket-name') bucket.file('IMG_2540.png').getSignedUrl({ action: 'write', expires: Date.now() + 60000 }, (error, signedUrl) => { if (error == null) { console.log(signedUrl) } }) 

Then in the browser I have <input type='file'/> with which I selected the file, then I will try to send it to the URL generated from my server side script as follows:

 function upload(blobOrFile, url) { var xhr = new XMLHttpRequest(); xhr.open('PUT', url, true); xhr.onload = function(e) { console.log('DONE!') }; xhr.upload.onprogress = function(e) { if (e.lengthComputable) { console.log((e.loaded / e.total) * 100) } }; xhr.send(blobOrFile); } // grab the `File` object dropped (which incidentally // matches the file name used when generating the signed URL upload($('[name=file]').files[0], 'URL GENERATED FROM SERVER-SIDE SCRIPT HERE'); 

What's happening?

Answer:

 <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>PUT image/png 1476631908 /bucket-name/IMG_2540.png</StringToSign> </Error> 

I reloaded the JSON key file to make sure it exists and has the appropriate permissions for this bucket, and I don't get any errors or anything when creating the signed URL.

The client code seems to initiate the download correctly (I see that the process updates are complete), then I get the 403 error above. File names match, content types seem to match expected values, expiration time seems reasonable.

The official SDK created the url, so it seems that everything will be alright.

I'm stuck, any help was appreciated.

+5
source share
1 answer

As Philippe Roberts, aka @LatentFlip, noted in my github registry that contains this case, adding content to the signature took care of that.

https://github.com/HenrikJoreteg/google-cloud-signedurl-test-case/pull/1/commits/84290918e7b82dd8c1f22ffcd2c7cdc06b08d334

Also, it looks like Google people are going to update docs / bugs to be more helpful: https://github.com/GoogleCloudPlatform/google-cloud-node/issues/1695

+2
source

Source: https://habr.com/ru/post/1258293/


All Articles