Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 393
caesay Avatar asked Mar 27 '17 07:03

caesay


People also ask

What is the difference between Google Drive and team drive?

Google shared drives (formerly known as Team Drives) are a feature in Google's Workspace for Education. Unlike files in My Drive, files in shared drives are owned by the team/group rather than an individual. Refer below for a table that outlines the key differences between U-M Google shared drives and My Drive.

How do I enable Team Drive in Google Drive?

Enable Team Drive Login with a G Suite administrator account to http://admin.google.com, go to Apps > G Suite > Drive and Docs, open Sharing Settings, then uncheck the box next to “Team Drive creation” to enable Team Drives. Select “Save” to keep your setting.

What happened to Google team drives?

The “Team Drives” feature of Google Drive will be renamed to “shared drives.” This will impact: Drive on web and mobile: Timing may vary depending on the specific platform. See below for more info.


1 Answers

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();
like image 131
caesay Avatar answered Sep 19 '22 13:09

caesay