Upload file to Google Team Drive

I've been scouring the internet for several minutes trying to figure out what is needed to download the file that will be contained in Team Drive.

I have read most of the documentation, here are only interesting snippets / mentions of command drives I found, but unfortunately there are no special features:
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

I am using the .Net gapi nuget package (v3). Create a service like this:

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 = "...",
});

      

I have a Team Drive ID that I am targeting and I can successfully get TeamDrive by the following code, but there are no interesting methods to download here:

var teamDrive = service.Teamdrives.Get(driveFolderId).Execute();

      

I am currently trying to use the normal way CreateMediaUpload

to create a file.

File body = new File();
body.Name = name;
body.MimeType = "application/octet-stream";
FilesResource.CreateMediaUpload request = service.Files.Create(body, stream, "text/plain");
request.Upload();

      

There File

are several interesting properties, namely Parents

, as well TeamDriveId

. When installed TeamDriveId

in the Team Drive ID, the file goes to my personal drive in the root directory. When you set parent to Team Drive id, I can't find the file anywhere.

No errors are thrown and the result request.Upload()

indicates success / completion every time (even if the file is not displayed). Where else should I look for the parent command setup? Another interesting feature on File

, DriveService

or TeamDrive

not, so I'm pretty lost.

+3


source to share


1 answer


In addition to setting the parent ID of the command ID, you must also set the property SupportsTeamDrives

to true in the request.

Then the code will look something like this, I noticed the important lines:

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();

      

The key point here is that the Team Drives permission scheme is completely different from the personal drive permission scheme, so you need to explicitly specify it to prove that you understand the differences.



An additional bit of information, if you want to list or find files on the command dial, you must also specify IncludeTeamDriveItems

and Corpora

in the request (in addition to SupportsTeamDrives

).

Then the search might look like this:

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();

      

+3


source







All Articles