Note that there are some explanatory texts on larger screens.

plurals
  1. POGoogle Storage REST PUT With Signed URLs
    primarykey
    data
    text
    <p>I'm trying to upload the base64 data of an image directly through javascript to Google Storage using signed URLs as authentication, which is apparently possible to do.</p> <p>According to developers.google.com/storage/docs/reference-methods#putobject there are only six headers that need to be set for this to work. Also for the header 'Authorization' I'm attempting to use the last option here:</p> <p>developers.google.com/storage/docs/reference-headers#authorization </p> <p>Which is 'A signature' developers.google.com/storage/docs/authentication#service_accounts</p> <p>The only thing I want to use PHP for is to get the signature. Here is what I have been trying to get working with no success.</p> <p>PHP &amp; JS page/code</p> <pre><code>&lt;?php $theDate = Date(DATE_RFC822); function signedURL( $filename, $bucket, $method = 'PUT' ) { $signature = ""; $duration = 30; $emailID = "980000000000-ytyertyr@developer.gserviceaccount.com"; $certs = array(); $priv_key = file_get_contents("9999999999999999999999999999-privatekey.p12"); if (!openssl_pkcs12_read($priv_key, $certs, 'notasecret')) { echo "Unable to parse the p12 file. OpenSSL error: " . openssl_error_string(); exit(); } $expires = time() + $duration; $to_sign = ( $method . "\n\n\n" . $expires . "\n" . "/" . $bucket . "/" . $filename ); $RSAPrivateKey = openssl_pkey_get_private($certs["pkey"]); if (!openssl_sign( $to_sign, $signature, $RSAPrivateKey, 'sha256' )) { error_log( 'openssl_sign failed!' ); $signature = 'failed'; } else { $signature = urlencode( base64_encode( $signature ) ); } return ( 'http://storage.googleapis.com/' . $bucket . '/' . $filename . '?GoogleAccessId=' . $emailID . '&amp;Expires=' . $expires . '&amp;Signature=' . $signature ); openssl_free_key($RSAPrivateKey); } ?&gt; &lt;script&gt; var base64img = 'data:image/png;base64,AAABAAIAICA....snip...A'; var xhr = new XMLHttpRequest(); //PUT test - PUT status "(Canceled)" - OPTION status 200 (OK) xhr.open("PUT", "&lt;?php echo signedURL('test.png', 'mybucket'); ?&gt;"); //xhr.setRequestHeader("Content-type", "image/png"); xhr.setRequestHeader("x-goog-acl", "public-read"); //try to set public read on file xhr.setRequestHeader("Content-Length", base64img.length); // Chrome throws error (Refused to set unsafe header "Content-Length" ) xhr.send( base64img ); //GET test.txt temp file - working and returning 200 status (signing must be working ?) /* xhr.open("GET", "&lt;?php echo signedURL('test.txt', 'mybucket', 'GET'); ?&gt;"); xhr.send(); */ // &lt;/script&gt; </code></pre> <p>Cors xml (seems to be fine) - I've set a wildcard only while testing and a low cache/maxage time</p> <pre><code>&lt;?xml version="1.0" ?&gt; &lt;CorsConfig&gt; &lt;Cors&gt; &lt;Origins&gt; &lt;Origin&gt;*&lt;/Origin&gt; &lt;/Origins&gt; &lt;Methods&gt; &lt;Method&gt;GET&lt;/Method&gt; &lt;Method&gt;HEAD&lt;/Method&gt; &lt;Method&gt;OPTIONS&lt;/Method&gt; &lt;Method&gt;PUT&lt;/Method&gt; &lt;/Methods&gt; &lt;ResponseHeaders&gt; &lt;ResponseHeader&gt;accept-encoding&lt;/ResponseHeader&gt; &lt;ResponseHeader&gt;cache-control&lt;/ResponseHeader&gt; &lt;ResponseHeader&gt;content-length&lt;/ResponseHeader&gt; &lt;ResponseHeader&gt;content-type&lt;/ResponseHeader&gt; &lt;ResponseHeader&gt;expect&lt;/ResponseHeader&gt; &lt;ResponseHeader&gt;if-modified-since&lt;/ResponseHeader&gt; &lt;ResponseHeader&gt;origin&lt;/ResponseHeader&gt; &lt;ResponseHeader&gt;range&lt;/ResponseHeader&gt; &lt;ResponseHeader&gt;referer&lt;/ResponseHeader&gt; &lt;ResponseHeader&gt;x-goog-acl&lt;/ResponseHeader&gt; &lt;ResponseHeader&gt;x-goog-api-version&lt;/ResponseHeader&gt; &lt;/ResponseHeaders&gt; &lt;MaxAgeSec&gt;900&lt;/MaxAgeSec&gt; &lt;/Cors&gt; &lt;/CorsConfig&gt; </code></pre> <p>I've tested the GET method on a file and get a 200 status back now (\n\n - fix)</p> <p><strong>Update:</strong></p> <p>Looking in Firefox it does return a 403, unlike Chrome.</p> <p><img src="https://i.stack.imgur.com/rQCU4.png"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload