javaTry
javaTry

Reputation: 1303

The best way to send file to GCS wihout user confirmation

I am developing an application that needs to send files to Google Cloud Storage.

The webapp will have a HTML page that the user choose files to do upload.

The user do not have Google Account.

The amount files to send is 5 or less.

I do not want to send files to GAE and GAE send to GCS. I would like that my user to do upload directly to GCS.

I did this code for upload:

function sentStorage() {
           var file =  document.getElementById("myFile").files[0];
           url = 'https://www.googleapis.com/upload/storage/v1/b/XXX/o?uploadType=resumable&name=' + file.name;
             xhr = new XMLHttpRequest();

           var token = 'ya29.XXXXXXXXXXXXXXX'; 
           xhr.open('POST', url);
           xhr.setRequestHeader('Content-Type', file.type);

           // resumable
           //url = 'https://www.googleapis.com/upload/storage/v1/b/XXXXXX/o?uploadType=resumable&name=' + file.name;
           //xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
           //xhr.setRequestHeader('Content-Length', file.size);


           xhr.setRequestHeader('x-goog-project-id', 'XXXXXXXXXX');
           xhr.setRequestHeader('Authorization', 'Bearer ' + token);

           xhr.send(file);

           xhr.onreadystatechange = function () {
               if (xhr.readyState === 4) {
                   var response = JSON.parse(xhr.responseText);
                   if (xhr.status === 200) {
                       alert('codigo 200');
                   } else {
                     var message = 'Error: ' + response.error.message;
                     console.log(message);
                       alert(message);

                   }
               }
           };
       }

I get a serviceaccount information (Google Console) and generate a token Bearer for it. I used a python file that read the "json account information" and generate the token.

My requisit is that user do not need to confirm any Google Account information for send files, this obligation is from my application. (Users do not have Google Account) and the html page send the files directly to GCS without send to GAE or GCE, so, I need to use HTML form or Javascript. I prefer Javascript. Only users of this application can do upload (the application has an authentication with database), so, anonymous user can not do it.

My questions are:

Upvotes: 0

Views: 146

Answers (1)

Brandon Yarbrough
Brandon Yarbrough

Reputation: 38389

Sending either a refresh or an access token to an untrusted end user is very dangerous. The bearer of an access token has complete authority to act as the associated account (within the scope used to generate it) until the access token expires a few minutes later. You don't want to do that.

There are a few good alternatives. The easiest way is to create exactly the upload request you want, then sign the URL for that request using the private key of a service account. That signed URL, which will be valid for a few minutes, could then be used to upload a single object. You'll need to sign the URL on the server side before giving it to the customer. Here's the documentation on signed URLs: https://cloud.google.com/storage/docs/access-control/signed-urls

Upvotes: 1

Related Questions