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).
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.
Am I meant to upload the binary or is there a way to get this to work with base64 in the HTTP body ?
Basic Code Sample.
<?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;
?>
<script>
var base64img = "data:image/png;base64,iVB...";//snipped
var xhr = new XMLHttpRequest();
xhr.open("PUT", "<?php echo $UploadURL ?>");
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 );
</script>
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.