Google Drive API: Uploading & Downloading Files

hi this is Wesley Chun you know how uploading and downloading files is pretty easy for Google Drive users well today we're going to show you that it's just as easy for developers using the drive API as a bonus we'll also introduce the latest and greatest version of the API so let's start there the drive API was launched along with Google Drive back in 2012 since then the team has been innovating and improving the API along with listening to user feedback in the newest version v3 we changed more sensible method and field names remove duplicate functionality and reduce the amount of data returned from API calls as a result v3 is leaner and faster I caught up with Steve basil who's the developer platform engineer for Google Drive to help me introduce developers to v3 so I gave a quick intro to the new version of the API at the top of the show but maybe you could tell us what was a team specifically trying to accomplish with this release sure so when we launched the API a couple years ago we did what we thought was right but after a few years of seeing how the API was used in production and talking to developers about what they were trying to accomplish we found that there's a really good opportunity to simplify the API and focus on speed and performance so in addition to the space and time savings where some things made easier for developers definitely yes I think one of the biggest improvements was around downloading files in the older version of the API downloading and exporting Google Docs and sheathe was a little bit complicated having to deal with export links so we created a new API method explicitly for exporting documentation and downloads in general are a lot simpler in the API so one of the things that developers are concerned with when a new API comes out is that the old one or the current one is immediately obsoleted is that the case with this one not this time normally that would be the case when we release a new API but for now we're going to keep both versions of the API running v3 doesn't really add any new features is primarily focused on simplifying the API making it faster out of the box and it's really targeted for new developers started getting started with the API so for those who have existing projects no need to panic v2 is going to continue to work for really the next couple of years okay well cool so let's I do have an existing app using the current version which is v2 it is it possible for me to start like a new component to that app using v3 can they kind of live side by side of course and that's a really great way to get started with v3 just pick something that is new maybe some code that you want to refactor that's a great opportunity to dip your toes into v3 understand what change how it would benefit you and if that goes well those people might find out that the rest of their application would benefit from moving to v3 but there's no rush to do it people feel that you know they have the time and they you know think they would benefit go for it you know there's a lot of gains I'm gonna get but again no need to panic really just you know take it easy okay cool so there's a lot of developers out there using Google Drive but they may all be using different development environments what languages are supported by the drive API so we have client libraries for most of the common languages out there so if you're you do objective-c or Swift for iOS PHP Python pining on yeah that's my favorite Python – there's client libraries for most languages nodejs even a client-side JavaScript library and they'll work with the API some of them are easier to use and others but really all of them are going to get you a lot closer to having your code ready than if you just wrote your own client and of course these are REST API so if you are more comfortable dealing with just your whatever HTTP client you have or some other rest library that you happen to be using go ahead and use those it's going to work just the same you might have to do a little bit more work particularly for uploads and downloads and some of the more complicated parts but the vast majority of is pretty easy to use pretty much on any platform okay great so that's good news that a lot of developers out there could use the drive API exactly yeah all right well Steve that was really useful information I think developers have all the need to know all we need to do is get them started with it thanks for stopping by today yeah thanks for having me now let's help you start using v3 we're going to walk through a simple code snippet that uploads and downloads files with the API including converting to a Google Apps format and exporting as PDF we'll do this first using the current version vitu then show you how we improve things for v3 so put on your coding hat and let's go to the computer now as in previous episodes we're going to do it in Python for brevity and it makes for great pseudocode if you're using something else as your dev tool to see all the Google API is client library supported languages click the link on screen or down in the video description below if you're on mobile now the first 20 or so lines are nearly identical to the auth boilerplate we covered in depth at the beginning of the video series check out those videos especially the one on the common code walkthrough if you miss these to understand what it does and why it's needed there are some differences in this modernized version the one you should note are there's an import of the print function on line one that will allow your script around in python 2 or 3 then there's this import block for the arc parse module and lines 20 and 21 so that your script uses the new run flow function from off to client tools which succeeds the run function but falls back to the latter if arc parse is not available meaning you're using an older version of python visit the link to the blog post to find out more info about this change since we're using the drive API we need the correct scope and that's on line 14 use the most restrictive scope you can hear it's Drive dot file because we're creating files on Google Drive the more restrictive your scope the fewer permissions you need to ask of your users and the safer your code will be now the first step create a service endpoint to Google Drive as you can see on line 22 as mentioned earlier we're going to do this in v2 first then show you how the code changes for v3 lines 24 to 27 are the files to upload you can see that they're two tuples with file names and a flag for converting to Google Apps format a yes or a no and you can also see that we're uploading the same file twice first as is in plain text then convert to Google Apps format for the plain text conversion its Google Docs but you can imagine that for CSV files it'll be Google sheets right now to the main part of the script we're going to start with the section to do uploads the for loop calls the files insert command for each upload with parameters the first is a convert flag the body is the file metadata which is just the file name the media body is also the file name but it's slightly different this is where the data is going to come from meaning that this file is going to be OH and the content sent to the API finally you should pass in the fields you want returned why is this important if not specified you get back a Paley with more than 30 fields why waste all that network traffic when you only need a couple of them I just want the mimetype to confirm how the file was stored in drive and I also want the export links more on that soon on lines 33 and 34 if the call is successful we're going to display the file was uploaded to the user including the mime type the final section of the script is where we wrap up our poor man's plain text PDF conversion that is to export and download the Google Docs version of the file as PDF note that we still have the result from the second upload which is the Google Doc converted file back to the export links we requested that field because it has the download links that we need specifically the PDF one how do we use it well we need to issue an authorized HTTP GET request because there's no API call for this at least not in v2 after the call completes check if we got the data if so save to disk and display success ok let's save it and run it if you didn't already know the first time you run a command line script like this a browser window is going to pop open with the OAuth 2 prompt where you have to authorize this script to be able to get access to the files in your Google Drive or your users Google Drive when you opt-in the credentials are going to be downloaded and stored in this storage that JSON file so you only need to do it once unless you delete this file anyway let's just pretend that all that has happened already and so we're just going to run the script as if we didn't have to worry about authorization we should see output for the plain text version there it is and in the Google Docs converter version and finally the download into PDF now let's check the drive UI web or mobile since I'm on a computer obviously it's the web version to confirm that we've got two versions of the hello file uploaded so here's the plain text one you can get a preview of it there it is and then you can also get a preview of the Google Doc converted one and there that is the last thing we need to do is check that we have a PDF file and we can do that pretty easily from the command line confirm that it actually is a PDF file and then open it and there we are hello world PDF great now let's do the same thing but use v3 of the API instead so first change the API version 2 v3 now one key change there's not going to be a convert flag anymore the problem is that a boolean variable only gives you two choices google apps conversion or not at all there's no option to specify what format you want if mime types was used instead of bullying's there's more flexibility for conversion either uploads or downloads so in the first case let's change the false to none which is pythons null object meaning no conversion and in the second case we need the mime type for Google Docs and that's easily found now we need to change this convert variable to reflect this change of data now we mentioned that v3 has better and more sensible naming here the title field changes to name more accurately reflecting that a files name it's not its title but hello its name another change with using the mime type instead of the convert flag is that instead of being a parameter it's actually going to be part of the file metadata so we need to add that if doing conversion another improved name change in v3 is that instead of insert create is actually a better name or verb so that's what we're going to change this to we have to get rid of the convert flag the body stays the same the media body stays the same lastly fields is no longer required as v3 is more nimble returning much less data than v2 fortunately the things that are returned are actually the ones we need finally if successful display the uploaded file message to the user now let's see how export and downloads are simplified first another v3 change no more download links in other words we don't need to look for export links and make an HTTP call instead both steps have been simplified to a single API request to the files export method if successful the call will yield the data we need to save so write it to disk and output to the user just as before and that's it now let's save and run the v3 version the output should be exactly the same as v2 there's the plain text there's a Google Doc conversion and a downloaded PDF now double check in your drive UI that you get another two files and there they are I'll let you confirm that the PDF was downloaded again successfully and that's it now you know how to upload files to Google five using v2 and v3 with or without conversion to Google Apps format awesome so as you can see it's not too bad if you're using v2 but better yet if you use the newest version you can take advantage of all the improvements that we made either way we hope this sample expires you to write your own app using the drive API to get started click the link that takes you right to our Docs one a deeper look at the code covered in this video we'll jump to the blog post at the second link and to see other devbytes in our developer video series check out the links at the bottom now that you know how to upload and download files with and without conversion we hope this jump starts your next project using the Google Drive API if you liked this video subscribe and check out our other dev bytes I'm Wesley Chun from Google and we'll see you the next time on the launchpad online


Add a Comment

Your email address will not be published. Required fields are marked *