2013-04-03 113 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

相关问题