In this article, we are going to see how can we download files from our Google Drive to our PC and upload files from our PC to Google Drive using its API in Python. It is a REST API that allows you to leverage Google Drive storage from within your app or program. So, let’s go ahead and write a Python script to do that.
- Python (2.6 or higher)
- A Google account with Google Drive enabled
- Google API client and Google OAuth libraries
Install the required libraries by running this command:
pip install –upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
- Now, to work with Google Drive API, we have to set up our account and enable Google Drive API.
- To set up your account, you can follow the steps given in the article.
- So, now we are ready to write the Python script.
Please make sure the file credentials.json is in the same directory.
First of all, we will import the required libraries. Then we will define a class DriveAPI with a constructor and two functions for uploading and downloading files. Inside the constructor, we will check if the file ‘token.pickle’ exists or not. If it exists, that means we have the access to the Google Drive storage and we don’t need to ask for it again. We may have to refresh the token if it’s been a long time since the token was used. if it doesn’t exist or is invalid, the script will open up a new tab in the browser and ask for access to Google Drive.
Once the access is granted, it will connect to the drive and fetch a list of files in the Google Drive storage for that account and print that list. Each item of the list contains an id and name for that file in Google Drive.
Now, Inside the FileDownload function, we will write the code to download a file. We need two things to do this. First is the id of that file in Drive and second is the name you want it to be saved as. Now, we will make a request to the Drive service to get us the file with the given id. Then, we will use a BytesIO object which will write the file to the memory. We will use the MediaIoBaseDownload class to receive the file from the server and write it in memory with the BytesIO object. Since the file size may vary from a few bytes to very large, we will prefer downloading the file in Chunks. We can also pass the chunk size if we don’t want to use the default one. Now, we will run a while loop and in each iteration of this loop, we will download a chunk of the file. Once it’s done, we will write the file from memory to our Hard Drive Storage. We will wrap this whole process in a try-except block so that if something goes wrong, our script doesn’t throw an error.
To Upload a File, we will use the FileUpload function. We only need the file path to upload a file. From the file path, we can easily extract the file name and find its mime-type using the mimetypes module. We will create a dictionary with the key “name” which contains the file name. Now, we will use the MediaFileUpload class to generate the media file, and then we will create a new file in the drive with the create function and it will save our file data to that newly created file.
This will attempt to open a new window in your default browser. If this fails, copy the URL from the console and manually open it in your browser. Now, Log in to your Google account if you aren’t already logged in. If there are multiple accounts, you will be asked to choose one of them. Then, click on the Allow button to proceed. After the authentication has been completed, your browser will display a message saying “The authentication flow has been completed. You may close this window.” Now, the program will print a list of files in your Google drive and ask you if you want to Upload or Download a file.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.