Patrick Jackson
Patrick Jackson

Reputation: 19446

Google Storage Media download with Json api

I am attempting to download a file from Google Storage using the Javascript json api. I am able to retreive the object info by using the code below, however I'm not sure how to get the actual media. I'm familiar with the Java library method getMediaHttpDownloader, but I do not see an equivalent in JS. Any help would be appreciated!

gapi.client.storage.objects.get({"bucket":"bucketName","object":"objectName"});

Upvotes: 4

Views: 1757

Answers (3)

Yuriy N.
Yuriy N.

Reputation: 6107

I did it using gapi and jQuery.

In my case object is public. (pulbic link in storage browser must be checked). In case you don't want your object to be public, use $.post instead of $.get and provide assess_token as header exactly as it is done in other answers.
Storage.getObjectInfo retrieves object metadata. Storage.getObjectMedia retrieves object content.

var Storage = function() {};
Storage.bucket = 'mybucket';
Storage.object = 'myfolder/myobject'; //object name, got by gapi.objects.list 



Storage.getObjectMedia = function(object, callback) {
    function loadObject(objectInfo) {
      var mediaLink = objectInfo.mediaLink;
      $.get(mediaLink, function(data) {   //data is actually object content
        console.log(data);
        callback(data);
      });
    }
    Storage.getObjectInfo(object, loadObject);
};
Storage.getObjectInfo = function(object, callback) {
  var request = gapi.client.storage.objects.get({
    'bucket' : Storage.bucket,
    'object' : Storage.object
  });
  request.execute(function(resp) {
    console.log(resp);
    callback(resp);
  });
};

It is also relatively rare case when we need to download the content of object. In most cases objects stored in Storage are media files like images and sounds and then all what we need is actually mediaLink, which must be inserted to src attribute value of appropriate dom element (img or audio).

Upvotes: 0

Brandon Yarbrough
Brandon Yarbrough

Reputation: 38389

The Javascript library does not currently support directly downloading media. You can still get to the data, but you'll have to access it another way.

Depending on the domain your website is hosted on and the bucket you're reading from, you'll need to set up CORS: https://developers.google.com/storage/docs/cross-origin

Then, you'll need to request the object directly via the XML API. For example, you could do something like this:

var accessToken = gapi.auth.getToken().access_token;
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://'+bucket+'.storage.googleapis.com/'+object);
xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);
xhr.send();

Upvotes: 3

Patrick Jackson
Patrick Jackson

Reputation: 19446

I've ended up not using the api(not sure that you can download using api, interested if you do know how) and using XmlHttpRequest instead. To do this I had to setup CORS for my google storage bucket to allow my site cross domain access. Below is my code:

var myToken = gapi.auth.getToken();
    var req = new XMLHttpRequest;

    req.open('GET','https://storage.googleapis.com/bucket/object',
                    true);
    req.setRequestHeader('Authorization', 'Bearer ' + myToken.access_token);

    req.send(null);

Upvotes: 2

Related Questions