how to use gapi.client.storage.objects.insert for media upload? (large files)

697 views
Skip to first unread message

joopring

unread,
Sep 26, 2012, 3:01:34 PM9/26/12
to google-api-jav...@googlegroups.com
Hello,
I try to upload files larger than 64K but fail.

In https://developers.google.com/storage/docs/json_api/v1/how-tos/upload the media upload feature of the Google Cloud Storage JSON API is documented. To upload files larger than the 64K that is allowed for inline data, one should append a query parameter uploadType=media (for simple uploads).
Also, the standard resource URI should be prefixed with "upload".

The documentation of the objects.insert method (https://developers.google.com/storage/docs/json_api/v1/objects/insert#examples) says: "This method supports media upload".

Putting both together seems to imply that in principle, the gapi.client.storage.objects.insert method *should* be able to do media upload of large files.
But is media upload implemented in the javascript client?
If so, how can I make it work?
And if not, can anyone help me make an ordinary ajax call using POST? I've tried but cannot figure out the correct endpoint. The examples on the page documenting the media upload are not helpful (referring to a fictional Farm API). I've tried:

"https://www.googleapis.com/storage/v1beta1/b/" + bucket + "/" + fileName + "?uploadType=media"
"https://www.googleapis.com/storage/v1beta1/b/upload/" + bucket + "/" + fileName + "?uploadType=media"
"https://www.googleapis.com/storage/v1beta1/upload/" + bucket + "/" + fileName + "?uploadType=media"
"https://www.googleapis.com/storage/upload/" + bucket + "/" + fileName + "?uploadType=media"

but none of these work.

Thanks in advance!

Joop Ringelberg


Robert King

unread,
Oct 30, 2014, 10:48:45 PM10/30/14
to google-api-jav...@googlegroups.com
This isn't very satisfactory but:
from https://cloud.google.com/storage/docs/json_api/v1/json-api-javascript-samples :

function insertObject(event) {
     
try{
       
var fileData = event.target.files[0];
     
}
     
catch(e) {
       
//'Insert Object' selected from the API Commands select list
       
//Display insert object button and then exit function
        filePicker
.style.display = 'block';
       
return;
     
}
     
const boundary = '-------314159265358979323846';
     
const delimiter = "\r\n--" + boundary + "\r\n";
     
const close_delim = "\r\n--" + boundary + "--";

     
var reader = new FileReader();
      reader
.readAsBinaryString(fileData);
      reader
.onload = function(e) {
       
var contentType = fileData.type || 'application/octet-stream';
       
var metadata = {
         
'name': fileData.name,
         
'mimeType': contentType
       
};

       
var base64Data = btoa(reader.result);
       
var multipartRequestBody =
          delimiter
+
         
'Content-Type: application/json\r\n\r\n' +
          JSON
.stringify(metadata) +
          delimiter
+
         
'Content-Type: ' + contentType + '\r\n' +
         
'Content-Transfer-Encoding: base64\r\n' +
         
'\r\n' +
          base64Data
+
          close_delim
;

       
//Note: gapi.client.storage.objects.insert() can only insert
       
//small objects (under 64k) so to support larger file sizes
       
//we're using the generic HTTP request method gapi.client.request()
       
var request = gapi.client.request({
         
'path': '/upload/storage/v1beta2/b/' + BUCKET + '/o',
         
'method': 'POST',
         
'params': {'uploadType': 'multipart'},
         
'headers': {
           
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
         
},
         
'body': multipartRequestBody});
         
//Remove the current API result entry in the main-content div
          listChildren
= document.getElementById('main-content').childNodes;
         
if (listChildren.length > 1) {
            listChildren
[1].parentNode.removeChild(listChildren[1]);
         
}
       
try{
         
//Execute the insert object request
          executeRequest
(request, 'insertObject');
         
//Store the name of the inserted object
          object
= fileData.name;        
       
}
       
catch(e) {
          alert
('An error has occurred: ' + e.message);
       
}
     
}
   
}
Reply all
Reply to author
Forward
0 new messages