2013-04-03 94 views
4

我知道這裏有一個類似的問題,但我仍然無法得到這項工作,因爲我的情況有點不同。 我希望能夠通過使用google-drive-ruby gem在谷歌驅動器中創建文件夾。在谷歌驅動器中創建文件夾與谷歌驅動器紅寶石寶石

據谷歌(https://developers.google.com/drive/folder)使用「驅動器」 API,您可以通過插入與MIME類型「應用/ vnd.google-apps.folder」

例如一個文件中創建的文件夾時

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" 
} 

在我來說,我希望能夠做同樣的事情,但使用google_drive API時。它具有接受mime-type選項的upload_from_file選項,但這仍然不適用於我,迄今爲止我得到的最好結果是在執行以下代碼時出現了來自Google的此錯誤消息。

session.upload_from_file( 「test.zip」, 「測試」,:CONTENT_TYPE => 「應用/ vnd.google-apps.folder」)

「Mime類型的應用程序/ vnd.google- apps.folder是無效的。文件不能 與谷歌MIME類型來創建。

,我會很感激,如果你能給我什麼建議。

+0

切換到Google官方ruby api lib是不合理的嗎? https://github.com/google/google-api-ruby-client – 2013-09-26 20:50:18

+0

@SteveMidgley我更新了OP的問題標題,以更準確地表示引用'google-drive-ruby'的問題。話雖如此,我應該自己玩官方的API,因爲在這之前我一直使用gimite的gem。 =) – zealoushacker 2013-12-05 20:54:02

回答

8

這其實很簡單。在谷歌驅動器A文件夾一個GoogleDrive::Collectionhttp://gimite.net/doc/google-drive-ruby/GoogleDrive/Collection.html)在google-drive-ruby gem中。因此,你可以用google-drive-ruby做什麼,首先創建一個文件,然後通過GoogleDrive::Collection#add(file)方法將它添加到集合中。

這也模仿了Google Drive實際工作的方式:將文件上傳到根集合/文件夾然後將其添加到其他集合/文件夾。

下面是我寫的一些示例代碼。它應該工作 - 爲您的具體使用情況也許一些小的調整 - 根據您已提供的上下文:

# 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) 

此外,如果你只是想創建一個新的文件夾,沒有把任何文件在裏面,那麼只需將其添加到根集合:

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

嗨@zealoushacker 只想問一下,我怎麼能得到搜索文件夾內的所有文件(搜索文件夾後使用session.collection_by_title(「文件夾名稱」)) – 2016-05-13 07:41:53

+0

我試過這個http:// pastebin。 COM/5R4hUqEB – 2016-05-13 08:01:32