Note that there are some explanatory texts on larger screens.

plurals
  1. POGoogle Cloud Storage PUT Not Saving As Binary
    primarykey
    data
    text
    <p>When uploading to Google cloud storage using a PUT request with base64 data the image (PNG) does not display in the browser and says that it contains errors (when viewing in FF). </p> <p>After opening the image file in a text editor I can see the base64 data "data:image/png;base64,iVBORw...", so it does not get converted to binary.</p> <p>Am I meant to upload the binary or is there a way to get this to work with base64 in the HTTP body ?</p> <p>Basic Code Sample.</p> <pre> &lt;?php $theDate = Date(DATE_RFC822); $emailID = "*******.gserviceaccount.com"; $priv_key = file_get_contents("*******-privatekey.p12"); function signedURL( $filename, $bucket, $method = 'PUT' ) { global $emailID; global $priv_key; $signature = ""; $duration = 60; $certs = array(); 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\nimage/png; charset=UTF-8\n" . $expires . "\nx-goog-acl:public-read\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://' . $bucket . '/' . $filename . '?GoogleAccessId=' . $emailID . '&Expires=' . $expires . '&Signature=' . $signature); openssl_free_key($RSAPrivateKey); } $UploadURL = signedURL('test.png', 'mybucket.mydomain.net', 'PUT'); //echo $UploadURL; ?&gt; &lt;script> var base64img = "data:image/png;base64,iVB...";//snipped var xhr = new XMLHttpRequest(); xhr.open("PUT", "&lt;?php echo $UploadURL ?&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 xhr.send( base64img ); &lt;/script&gt; </pre> <p>I feel that it is possible based on things that I've read online, but I can't find any examples showing exactly the way it is done with PUT to indicate what is missing to make it work.</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.
 

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