Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add/create/insert files to Google Drive through the API?

Need help regarding inserting a file to google drive through api. The api documentation for this purpose does not clearly explains how to send the actual body of the file through http post request.

like image 816
Niranja Avatar asked Apr 25 '12 14:04

Niranja


People also ask

How do I automatically add files to Google Drive?

Google offers Backup and Sync, an application you can install on your computer in order to back up any folder on your computer over to Google Drive automatically. Simply install Backup and Sync and you can add any folder on your computer to automatically upload all files to Google Drive.

How do I create a folder in Google Drive API?

To create a file in a folder, use the files. create method and specify the folder ID in the parents property of the file. The following code snippet shows how to create a file in a specific folder using a client library: Note: If you're using the older Drive API v2, use the files.

Is there an API for Google Drive?

The Google Drive API allows you to create apps that leverage Google Drive cloud storage. You can develop applications that integrate with Drive, and create robust functionality in your application using the Drive API.


2 Answers

The documentation on insert operations already contains examples in a bunch of programming languages, here is how to do it using the HTTP based protocol of the Google Drive API.

First, POST the new file metadata to the Drive endpoint. It has to be in the form of a File resource JSON object:

POST /drive/v2/files HTTP/1.1 Host: www.googleapis.com Authorization: Bearer <OAuth 2.0 access token here> ...  {   "title": "file_name.extension",   "mimeType": "mime/type",   "description": "Stuff about the file" } 

The response body will be a JSON representation of the newly created File resource. It will look like:

{   "kind": "drive#file",   "id": string,   "etag": etag,   "selfLink": string,   "title": "file_name",   "mimeType": "mime/type",   "description": "Stuff about the file"   ...   "downloadUrl": string,   ... } 

This is a confirmation that the file entry has been created. Now you need to upload the content. To do that you need to take the ID of the file given by the id JSON attribute in the response above and PUT the content of the actual file to the upload endpoint with an OAuth 2.0 authorized request. It should look like:

PUT /upload/drive/v2/files/{id}?uploadType=media HTTP/1.1 Host: www.googleapis.com Authorization: Bearer <OAuth 2.0 access token here> Content-Type: mime/type  <file content here> 

You are done :)

There is also a way to do this in 1 single POST request using a multipart request where you post the metadata of the file at the same time as the content. Here is an example:

POST /upload/drive/v2/files HTTP/1.1 Host: www.googleapis.com Authorization: Bearer <OAuth 2.0 access token here> Content-Type: multipart/form-data; boundary=287032381131322 ...  --287032381131322 Content-Type: application/json  {   "title": "file_name.extension",   "mimeType": "mime/type",   "description": "Stuff about the file" } --287032381131322 Content-Type: mime/type  <file content here> --287032381131322-- 

The response will contain the metadata of the newly created file. You may also use the Content-Transfer-Encoding: base64 header in the sub-part of the request to be able to pass the data of the file as Base 64 encoded.

Lastly there is also a resumable upload protocol which is convenient to upload large files, offer a pause/resume feature and/or upload files with flaky internet connection.

PS: most of this is now described in Drive's file upload documentation.

like image 53
Nicolas Garnier Avatar answered Sep 29 '22 06:09

Nicolas Garnier


Thanks for the explanation! This has taken my hours of going around in circles with the crappy google SDK documentation (sorry I had to get my rant out).

Here's a function I made that will update a text file (as you can see I'm updating html):

  function gd_updateFile(fileId, folderId, text, callback) {      const boundary = '-------314159265358979323846';     const delimiter = "\r\n--" + boundary + "\r\n";     const close_delim = "\r\n--" + boundary + "--";      var contentType = "text/html";     var metadata = {'mimeType': contentType,};      var multipartRequestBody =         delimiter +  'Content-Type: application/json\r\n\r\n' +         JSON.stringify(metadata) +         delimiter + 'Content-Type: ' + contentType + '\r\n' + '\r\n' +         text +         close_delim;      if (!callback) { callback = function(file) { console.log("Update Complete ",file) }; }      gapi.client.request({         'path': '/upload/drive/v2/files/'+folderId+"?fileId="+fileId+"&uploadType=multipart",         'method': 'PUT',         'params': {'fileId': fileId, 'uploadType': 'multipart'},         'headers': {'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'},         'body': multipartRequestBody,         callback:callback,     });   } 

It's a mashup of the google example (which uses a binary file from upload), and the nice explanation from @Nivco above.

like image 23
user1158023 Avatar answered Sep 29 '22 08:09

user1158023