我一直在网上搜索几个小时,试图找出上传一个将包含在Team Drive中的文件的必要条件。
我已经阅读了大部分文档,我发现的团队驱动器中唯一有趣的部分/提及是在这里,但遗憾的是没有具体内容:
https://developers.google.com/drive/v3/web/manage-uploads
https://developers.google.com/drive/v3/web/manage-teamdrives
https://developers.google.com/drive/v3/web/about-files
我正在使用.Net gapi nuget包(v3)。创建如下服务:
string[] scopes = new string[] { DriveService.Scope.Drive, DriveService.Scope.DriveFile };
var secrets = new ClientSecrets
{
ClientId = "...",
ClientSecret = "...",
};
var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(secrets, scopes, Environment.UserName, CancellationToken.None).Result;
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "...",
});
我有我目标的Team Drive的ID,我可以通过以下代码成功检索TeamDrive,但这里没有有趣的方法可供上传:
var teamDrive = service.Teamdrives.Get(driveFolderId).Execute();
我目前一直在尝试使用正常的CreateMediaUpload
方式创建文件。
File body = new File();
body.Name = name;
body.MimeType = "application/octet-stream";
FilesResource.CreateMediaUpload request = service.Files.Create(body, stream, "text/plain");
request.Upload();
File
上有一些有趣的属性,即Parents
和TeamDriveId
。将TeamDriveId
设置为Team Drive Id时,文件最终会出现在根目录中的个人驱动器中。将父级设置为Team Drive Id时,我似乎无法在任何地方找到该文件。
没有抛出错误,request.Upload()
的结果每次都表示成功/完成(即使文件没有显示)。我还应该在哪里设置父团队驱动器? File
,DriveService
或TeamDrive
上没有其他有趣的属性,所以我很遗憾。
答案 0 :(得分:6)
除了将父级设置为团队驱动器ID之外,还必须在请求中将SupportsTeamDrives
属性设置为true。
然后代码看起来类似于以下内容(我注意到了重要的一行):
File body = new File();
body.Name = name;
body.MimeType = "application/octet-stream";
body.Parents = new List<string> { driveFolderId }; // <--------
FilesResource.CreateMediaUpload request = service.Files.Create(body, stream, "application/octet-stream");
request.SupportsTeamDrives = true; // <--------
request.Upload();
这里的关键是Team Drives权限方案与个人驱动器权限方案完全不同,因此您需要明确选择它以证明您了解差异。
如果您要在团队驱动器中列出或搜索文件,还需要提供额外的信息,还必须在请求中指定IncludeTeamDriveItems
和Corpora
(除了SupportsTeamDrives
)。
搜索可能看起来像这样
var existingSearch = service.Files.List();
existingSearch.Fields = "nextPageToken, files(id, name)";
existingSearch.Q = $"'{driveFolderId}' in parents and name = '{name}'";
if (isFolderTeamDrive)
{
existingSearch.SupportsTeamDrives = true;
existingSearch.Corpora = "teamDrive";
existingSearch.IncludeTeamDriveItems = true;
existingSearch.TeamDriveId = driveFolderId;
}
var existingResponse = existingSearch.Execute();