Signed uploads

From Anon, 1 Week ago, written in Plain Text, viewed 30 times.
URL http://codebin.org/view/22ec81f3 Embed
Download Paste or View Raw
  1. // this is a JS function
  2. // @param file object HTML file object
  3. // @param done function callback function
  4. function getSignedUrl(file, done) {
  5.         $.ajax({
  6.                 type: "POST",
  7.                 url: "/sign_url",
  8.                 data: {bucket_name: bucketName, file_name: file.name, type: file.type},
  9.                 success: function(data) {
  10.                         if(data.success) {
  11.                                 file.uploadURL = data.data;
  12.                                 uploadFile(file)
  13.                         } else {
  14.                                 done(data.message)
  15.                         }
  16.                 }
  17.         });
  18. }
  19.  
  20. // this is a PHP function that getSignedUrl() calls to generate a signed PUT URL that a user can upload a file to
  21. // @param file_name string
  22. // @param bucket_name string
  23. // @param content_type string
  24. // @param method string
  25. // @param duration int
  26. function createSignedUrl($file_name, $bucket_name, $content_type = '', $method = 'PUT', $duration = 300) {
  27.     $expires      = time() + $duration;
  28.     $file_name    = date('Y/m/d/') . md5(microtime() . uniqid() . $file_name) . '-' . urlencode($file_name);
  29.     $signature    = '';
  30.     $to_sign      = ($method . "\n\n" . $content_type . "\n" . $expires . "\n" . '/' . $bucket_name . '/' . $file_name);
  31.    
  32.     // this is your service account’s private key
  33.     $private_key  = get_service_key();
  34.    
  35.     // this is your service account’s email address
  36.     $service_account_name = ‘’;
  37.  
  38.     if(!openssl_sign( $to_sign, $signature, $private_key, 'sha256' ))
  39.     {
  40.         return false;
  41.     }
  42.     else
  43.     {
  44.         $signature = urlencode(base64_encode($signature));
  45.     }
  46.  
  47.     return 'https://storage.googleapis.com/' . $bucket_name . '/' . $file_name .
  48.         '?GoogleAccessId=' . $service_account_name .
  49.         '&Expires=' . $expires .
  50.         '&Signature=' . $signature;
  51. }
  52.  
  53. // don’t have a JS function to share for this one, but you just use the URL generated by PHP and use JS to PUT to that URL. Pseudo-code...
  54. Function uploadFile(file) {
  55.    fetch(file.uploadURL, { // Your PUT endpoint
  56.     method: 'PUT',
  57.     headers: {
  58.       // Content-Type may need to be completely **omitted**
  59.       // or you may need something
  60.       "Content-Type": File.type,
  61.     },
  62.     body: file // This is your file object
  63.   }).then(
  64.     response => response.json() // if the response is a JSON object
  65.   ).then(
  66.     success => console.log(success) // Handle the success response object
  67.   ).catch(
  68.     error => console.log(error) // Handle the error response object
  69.   );
  70. }

Reply to "Signed uploads"

Here you can reply to the paste above