谷歌驱动器api创建子文件夹

时间:2016-07-07 19:35:47

标签: google-drive-api

我需要使用在控制台应用程序中使用nuget包添加的google drive api在google驱动器中创建子文件夹。

我可以获取根文件夹的文件夹ID。可以获取rot文件夹的子项,也可以在根文件夹中上传文件。唯一的问题是在文件夹中创建子文件夹。

for (int i = 1; i < array.Count(); i++)
                        {
                            var subfoldername = new Google.Apis.Drive.v2.Data.File { Title = array[i], MimeType = "application/vnd.google-apps.folder" };
                            ChildrenResource.ListRequest request = service.Children.List(rootfolderid);
                            ChildList children = request.Execute();
                            if (children.Items.Count > 0)
                            {
                                foreach (ChildReference c in children.Items)
                                {
                                    Google.Apis.Drive.v2.Data.File file = service.Files.Get(c.Id).Execute();
                                    if (file.MimeType == "application/vnd.google-apps.folder")
                                    {
                                        List<GoogleDriveFile> googledrive = new List<GoogleDriveFile>();
                                        googledrive.Add(new GoogleDriveFile
                                        {
                                            OriginalFilename = file.OriginalFilename
                                        });
                                    }
                                }
                            }
                            else
                            {
// here need to add sub folder in folder, but this line adds folder at root
                                var result = service.Files.Insert(foldername).Execute();
                            }

2 个答案:

答案 0 :(得分:1)

您必须在创建文件夹时添加属性parent。

parents[]

  

包含此文件的父文件夹的集合。   设置此字段会将文件放在所有提供的文件夹中。在插入时,如果没有提供文件夹,则该文件将放在默认的根文件夹中。

示例代码:

function createSubFolder() {
var body = new Object();
body.title = 'SubFolder';
body.parents = [{'id':'0B5xvxYkWPFpCUjJtZVZiMWNBQlE'}];
body.mimeType = "application/vnd.google-apps.folder";

console.log(body)
var request = gapi.client.request({
'path': '/drive/v2/files',
'method': 'POST',
'body': JSON.stringify(body)
});

request.execute(function(resp) { console.log(resp); });
}

我在JavaScript中使用Drive v2

希望这有帮助

答案 1 :(得分:1)

这是我的方式,在谷歌驱动器中创建子文件夹时,它必须需要父母。因此,在执行字符串q之前,我们需要搜索父根id

string findRootId = "mimeType = 'application/vnd.google-apps.folder' and title ='" + RootFolder + "' and trashed = false";

IList<File> _RootId = GoogleDriveHelper.GetFiles(service, findRootId);

if (_RootId.Count == 0) {                                  
  _RootId.Add(GoogleDriveHelper.createDirectory(service, RootFolder, "", "root"));
  Console.WriteLine("Root folder {0} was created.", RootFolder);
}

var id = _RootId[0].Id;

string Q = "mimeType = 'application/vnd.google-apps.folder' and '" + id + "' in parents and title ='" + GoogleDriveFolderName + "' and trashed = false";