谷歌驱动器API创建子文件夹
问题描述:
我需要在谷歌驱动器中创建子文件夹使用谷歌驱动器API添加使用nuget包在控制台应用程序。谷歌驱动器API创建子文件夹
我可以得到根文件夹的文件夹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();
}
答
这里是我做的,创造了谷歌驱动器中的子文件夹时的方式它必须需要一个父母。因此,在执行字符串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";
答
您必须在创建文件夹时添加属性父项。
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); });
}
我使用的驱动器V2在JavaScript
希望这有助于