var request = gapi.client.storage.objects.insert(
{
'bucket': bucket,
'name': fileName,
'resource': {
"media": {
"contentType": mimeType,
"contentLength": data.length,
"data": data
},
'cacheControl': 'no-cache'
}
});
And here is the response:
However the same concepts apply to the Google Cloud Storage JSON API.
But how?
Joop
The upload URI. The format of the upload endpoint is the standard resource URI with an “upload” prefix. Use this URI when transferring the media data itself. Example: POST /upload/farm/v1/animalsreader.onload = function(e) {
var fileData = evt.target.files[0];
const boundary = '-------314159265358979323846';
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";
var contentType = fileData.type || 'application/octet-stream';
var metadata = {
'title': 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;
var request = gapi.client.request({
'path': '/upload/storage/v1beta1/b/marc-us/o',
'method': 'POST',
'params': {
'name': fileData.name,
'uploadType': 'multipart'
},
'headers': {
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
},
'body': multipartRequestBody});
callback = function() { alert('file uploaded'); };
request.execute(callback);
}
reader.readAsBinaryString(evt.target.files[0]);
}
--