问题描述
我知道这里也有类似的问题,但是由于我的情况有些不同,我仍然无法完成这项工作.我希望能够使用 google-drive-ruby gem 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.
根据Google( https://developers.google.com/drive/folder )使用"Drive" Api时,您可以通过插入MIME类型为"application/vnd.google-apps.folder"的文件来创建文件夹
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"
例如
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的错误消息.
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")
session.upload_from_file("test.zip", "test", :content_type => "application/vnd.google-apps.folder")
如果您能给我任何建议,我将不胜感激.
I'll appreciate if you can give me any suggestions.
推荐答案
实际上非常简单. Google云端硬盘中的文件夹是GoogleDrive::Collection
( http://gimite. net/doc/google-drive-ruby/GoogleDrive/Collection.html )中的 google -drive-ruby宝石.因此,您可以使用google-drive-ruby首先创建一个文件,然后通过GoogleDrive::Collection#add(file)
方法将其添加到集合中.
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.
这也模仿了Google云端硬盘的实际工作方式:将文件上传到根集合/文件夹,然后将其添加到其他集合/文件夹.
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")
这篇关于使用google-drive-ruby gem在google drive中创建文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!