[MUSIC PLAYING]
WESLEY CHUN: Greetings, developers.
And welcome to another episode of the G Suite Dev Show.
I'm your host, Wesley Chun.
You may have heard about Google Drive's Team Drives feature
recently.
There's plenty of excitement around this product.
But beyond its hype and the solutions
it brings, let's introduce you to a writing Team Drive
applications using the Google Drive API.
If you're really new to Team Drives
and want to know how to use them,
check out this guided tutorial.
Down below in the video description,
you'll also find Team Drive announcements as well as
an intro video.
So why Team Drives?
If you use G Suite in your organization,
Google Drive is likely at the center of your company
documents and collaboration.
However, Drive was designed around users.
Users are owners of files, users share
documents with other users, users collaborate on documents,
and so on.
But this model doesn't work as well
in larger organizations that are more team-oriented rather
than user-centric.
Employees changing teams or leaving the company
altogether may result in files going missing.
Common solutions, like assigning new owners of files,
using shared folders, and even Google Groups,
isn't always successful as files still appear to go missing.
Well, this is why Team Drives were
created, to solve these core problems for G Suite customers.
No more confusion and mysteriously missing files.
Think of Team Drives as a completely new type of drive
re-invented specifically for teams and organizations.
While Team Drives work in conjunction with regular Drive,
it's best to consider both as separate parallel universes,
living side-by-side from a user's perspective,
just like in the user interface you see here.
That'll help avoid confusion.
While Team Drives are relatively new,
we don't think you should have to write completely new apps
just to leverage Team Drives.
The good news for those of you already using the Drive API
is that you only need to augment your code
to support Team Drives and only when
you're ready to add Team Drive functionality.
Yes, there's a new collection of methods
plus additional arguments to existing methods
for Team Drives.
But the net result is that all of your existing applications
will continue to function normally with regular Drive
So what are some of the basic operations
that any Drive developer should know
regardless of the type of application you have?
Well, one, you should be able to create a Team Drive.
Two, you should also know how to add members to a Team Drive.
Three, you should also be able to create
a folder in a Team Drive.
And four, you should be able to import or upload files
to a Team Drive folder.
We'll look at more advanced skills in upcoming videos,
but you've got to start somewhere, right?
Before we walk through our code sample that does all four
of these, make sure that you have an applicable G Suite
domain, administrative access to be able to manipulate
Team Drives in that domain.
Next, you should have the Google APIs Client Library installed
in your development language.
We'll demo in Python because, one, I know
a little something about it.
And two, it makes for great pseudocode for everyone else.
Finally, you should have a project
in the Developer's Console with the Drive API enabled.
Once you're ready to go, let's walk through this demo script
on the computer.
OK, on lines 1 through 14, we have our boilerplate imports
and authorization code.
But there are a couple of lines to look more carefully at.
In our application, we need a universally unique identifier,
and we'll show you where that comes in.
But we import that on line 2.
On line 8, we need the full read/write scope
for Google Drive.
And on line 14, we create the service endpoint
to the Drive API.
We're using Version 3 here.
Now let's look at our first Team Drives function.
It's called Create Team Drive, or create_td.
And it does exactly what you think.
It creates a Team Drive using the Create method
in the new Team Drives collection.
Line 17 is where that unique identifier comes in.
Think of a request_id as a serial number
to ensure that apps performing retries
don't create duplicate Team Drives.
If you have heard the term idempotent before,
this is exactly what we're talking about.
UUIDs are unique, so that's why we're using them here.
And most programming languages, including Python,
have some sort of UUID library.
Note, this is the only way to create a Team Drive.
So yes, creating a Team Drive is different from creating
a folder in regular Google Drive.
On lines 22 to 25, add_user adds a member to a Team Drive
by calling permissions.create, in a way
quite similar to sharing a document with another user.
The supportsTeamDrives flag on line 25
is obviously new and used when calling existing Drive API
methods, but requesting Team Drive functionality
On 27 to 30, create_td_folder creates a folder
in a Team Drive given the Team Drive
ID, which we got from the return value of create_td.
So create_td_folder takes that Team Drive ID and a folder
name.
All files and folders can only live
in one place on Team Drives, meaning
they can only have one parent.
This is nearly identical to the code
to create a folder in regular Drive
except for the supportsTeamDrives flag
and a single parent.
I'll let you guess what import_csv_to_td_folder
does on lines 32 to 35.
Like create_folder, this code imports a file
with files.create, just like for regular Drive,
but has a supportsTeamDrives flag again.
And those are the four main functions.
The rest of the code is just the supporting cast.
Lines 37 to 39 are just various definitions.
On 37, we have the MIME type of a Google Drive folder.
And on 39, we have the MIME type for Google Sheets files.
Sandwiched in between is the CSV file
that we're going to import.
And by import, we mean upload and convert
to Google Sheets format.
On disk, we have the file stored with the CSV file extension.
So we have to add that here in the media body on line 34.
But as a Google Sheet, we no longer need that CSV extension.
So we just take the name as is.
And you can see that as a second parameter
to the function call, which gets assigned
into the files metadata on 33.
By the way, if you really want to run this,
you're going to have to import your own CSV file, so
change the name if necessary.
Now, you could change this code to upload the raw CSV
and not convert to Sheets.
And the way you would do that is, instead of using the Google
Sheets MIME type, you would just use text/csv, which
is the MIME type for CSV files.
And 41 to 48, just run all of these functions one at a time,
then displaying to the user that the function completed.
So on line 41, we call create_td to create a Team Drive called
Corporate shared TD.
And we save the return value, which is the Team Drive ID.
On line 43, we call add_user, pass in the Team Drive ID,
and the name of the user we want to add.
As before, if you want to really add a user,
change this email address to a real one
for a user in your domain.
On 45, we take the Team Drive ID and call create_td_folder,
creating a folder called Manufacturing data in my Team
Drive.
And then on 47, we call import_csv_to_td_folder, pass
in the folder_id of the file that we just created--
Manufacturing data-- the source file-- inventory,
or inventory.csv--
and the Sheets MIME type for conversion.
Again, if you don't want to convert,
then just use text/csv here.
And that's it.
So that's how you can test drive Team Drives
in less than 50 lines of code.
You can read more in the code deep dive blog post
and run it with Python 2 or 3.
It works with both.
But no matter which version you use,
you should expect the output from the print
calls in the script plus a new Team Drive folder called
Corporate shared TD, inside of which you'll find
a Manufacturing data folder.
And inside of that should be your Inventory Sheets file,
imported successfully from CSV.
To get started, check out this page in the Docs
that introduces Team Drives, concepts, some of the rules,
and basically how Team Drives work.
Once you're ready to take your first steps,
check out this Docs page, enabling Team Drive support,
and specific differences in using the API when writing apps
for regular Google Drive versus Team Drives.
Finally, see the complete video from Google Cloud Next
2017, where you'll learn about Team Drives for developers
and hear directly from partners who have already
implemented Team Drive apps.
So that's our tour of Team Drive basics for developers.
Keep an eye out for more Team Drives episodes coming soon.
Thanks for joining us today, and please
subscribe to our channel.
This is Wesley Chun, and we'll see you upstairs
in the G Suite.
[MUSIC PLAYING]
Hey, everyone.
Thanks for watching.
Check out these related videos.
And we'll see you next time.
[MUSIC PLAYING]