caesay
caesay

Reputation: 17271

Upload file to Google Team Drive

I've been scouring the internet for a few hours trying to figure out what is necessary to upload a file that will be contained within a Team Drive.

I've read most of the documentation, the only interesting bits / mention of team drives I found are here, but unfortunately there's no specifics:
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'm using the .Net gapi nuget package (v3). Create a service like the following:

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 the Id of the Team Drive I'm targetting, and I can successfully retrieve the TeamDrive by the following code, but there are no interesting methods here for uploading:

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

I've currently been trying to use the normal CreateMediaUpload way of creating 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's a few interesting properties on File, namely Parents and also TeamDriveId. When setting the TeamDriveId to the Team Drive Id, the file ends up in my personal drive in the root directory. When setting the parent to the Team Drive Id, I can't seem to find the file anywhere.

There are no errors thrown, and the result of request.Upload() indicates Success/Complete every time (even if the file doesn't show up). Where else should I be looking to set the parent team drive? There's no other interesting properties on File, DriveService, or TeamDrive so I'm pretty lost.

Upvotes: 6

Views: 2836

Answers (1)

caesay
caesay

Reputation: 17271

In addition to setting to the parent to the team drive id, you must also set the SupportsTeamDrives property to true in the request.

The code would then look similar to the following (I've noted 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 here is that the Team Drives permission scheme is completely different to the personal drive permission scheme, so you need to explicitly opt-in to it to prove you understand the differences.

An extra bit of info, if you want to list or search for files in a team drive, you must also specify IncludeTeamDriveItems and Corpora on the request (in addition to SupportsTeamDrives).

A Search might then 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();

Upvotes: 6

Related Questions