Create a folder on google drive using gug-drive-ruby gem

I know that a similar question was asked here, however, I still cannot get this work, as my case is a little different. I want to be able to create a folder on a google drive using gug-drive-ruby gem .

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

eg.

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, but when using the google_drive API. It has a upload_from_file parameter that accepts a mime type parameter, however this still doesn’t work for me, the best result I have received so far was that when executing the following code this error message from Google.

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

"Invalid mime type application / vnd.google-apps.folder. Files cannot be created using Google mime types.

I would be grateful if you can give me any suggestions.

+4
source share
1 answer

This is actually quite simple. The folder in Google Drive is the GoogleDrive::Collection ( http://gimite.net/doc/google-drive-ruby/GoogleDrive/Collection.html ) in the gug-drive-ruby gem . Therefore, what can you do with google-drive-ruby, first create a file and then add it to the collection using the GoogleDrive::Collection#add(file) method.

It also mimics how Google Drive actually works: upload the file to the root collection / folder, and then add it to other collections / folders.

Here is an example of the code I wrote. It should work - perhaps with a slight change for your specific use case - based on the context you 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) 

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

 session.root_collection.create_subcollection("my-folder-name") 
+8
source

All Articles