Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create folder in google drive with google-drive-ruby gem

I know that a similar question was asked here, however I still can't get this work since my case is a bit different. I want to be able to create a folder in google drive by using the google-drive-ruby gem.

According to Google (https://developers.google.com/drive/folder) when using the "Drive" Api you can create a folder by inserting a file with mime-type "application/vnd.google-apps.folder"

e.g.

POST https://www.googleapis.com/drive/v2/files
Authorization: Bearer {ACCESS_TOKEN}
Content-Type: application/json
...
{
  "title": "pets",
  "parents": [{"id":"0ADK06pfg"}]
  "mimeType": "application/vnd.google-apps.folder"
}

In my case I want to be able to do the same thing but when using the google_drive API. It has the upload_from_file option which accepts the mime-type option, however this still doesn't work for me, the best result that I got so far was when executing the following code was this error message from Google.

session.upload_from_file("test.zip", "test", :content_type => "application/vnd.google-apps.folder")

"Mime-type application/vnd.google-apps.folder is invalid. Files cannot be created with Google mime-types.

I'll appreciate if you can give me any suggestions.

like image 605
sylvian Avatar asked Apr 03 '13 21:04

sylvian


People also ask

How do I create a Google Drive 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.

Can you create folder in Google Drive?

To organize your files in Drive, you can create folders to make files easier to find and share with others. Note: If you organize a lot of files or folders at once, it might take time for you to see the changes. This is available on multiple devices.


1 Answers

It's actually pretty straightforward. A folder in Google Drive is a GoogleDrive::Collection (http://gimite.net/doc/google-drive-ruby/GoogleDrive/Collection.html) in google-drive-ruby gem. Therefore, what you may do with google-drive-ruby is first create a file, and then add it to a collection via the GoogleDrive::Collection#add(file) method.

This also mimics the way that Google Drive actually works: upload a file to the root collection/folder, then add it to other collections/folders.

Here's some sample code, which I had written. It should work - with perhaps some minor tweaking for your specific use case - based on the context that you had provided:

# this example assumes the presence of an authenticated
# `GoogleDrive::Session` referenced as `session`
# and a file named `test.zip` in the same directory
# where this example is being executed

# upload the file and get a reference to the returned
# GoogleSpreadsheet::File instance
file = session.upload_from_file("test.zip", "test")

# get a reference to the collection/folder to which
# you want to add the file, via its folder name
folder = session.collection_by_title("my-folder-name")

# add the file to the collection/folder. 
# note, that you may add a file to multiple folders
folder.add(file)

Further, if you only want to create a new folder, without putting any files in it, then just add it to the root collection:

session.root_collection.create_subcollection("my-folder-name")
like image 128
zealoushacker Avatar answered Sep 28 '22 09:09

zealoushacker