syl*_*ian 4 ruby google-drive-api
我知道这里有一个类似的问题,但是由于我的情况有点不同,我仍然无法完成这项工作.我想通过使用google-drive-ruby gem在google驱动器中创建一个文件夹.
据谷歌(https://developers.google.com/drive/folder)使用"驱动器" API,您可以通过插入用的MIME类型"application/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"
}
Run Code Online (Sandbox Code Playgroud)
在我的情况下,我希望能够做同样的事情,但在使用google_drive API时.它有一个接受的mime-type选项的upload_from_file选项,但是这仍然不为我工作,我走到这一步,执行以下代码时是最好的结果是从谷歌此错误消息.
session.upload_from_file("test.zip","test",:content_type =>"application/vnd.google-apps.folder")
"Mime-type application/vnd.google-apps.folder无效.无法使用Google mime-types创建文件.
如果你能给我任何建议,我将不胜感激.
它实际上非常简单.Google云端硬盘中的文件夹是google-drive-ruby gem中的GoogleDrive::Collection(http://gimite.net/doc/google-drive-ruby/GoogleDrive/Collection.html).因此,您可以使用google-drive-ruby首先创建一个文件,然后通过该方法将其添加到集合中.GoogleDrive::Collection#add(file)
这也模仿了Google云端硬盘的实际工作方式:将文件上传到根集合/文件夹,然后将其添加到其他集合/文件夹.
这是我编写的一些示例代码.根据您提供的上下文,它应该可以工作 - 可能会针对您的特定用例进行一些小调整:
# 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)
Run Code Online (Sandbox Code Playgroud)
此外,如果您只想创建一个新文件夹,而不在其中放入任何文件,那么只需将其添加到根集合中:
session.root_collection.create_subcollection("my-folder-name")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2102 次 |
| 最近记录: |