Google Drive synchronization with Python

Sam Stands
Nov 15, 2019 · 4 min read

In this guide, I will demonstrate how you can upload a folder, saved locally, to a Google Drive (through the Google Drive API) using only a simple Python program. The code can be improved upon, and everyone is free to do so. This guide is intended to be short and to the point.

Project motivation

The cost of having the Google Drive App installed on my computer greatly outweighs my synchronization needs, so I wanted to rid myself of the Google Drive App. Despite my limited need for syncing, I still needed to sync one particular folder now and again. I found myself wanting a solution where I could sync a specific folder with my Google Drive, without installing the Google Drive App or, even worse, having to manually sync the folder myself by uploading it to my Google Drive.

Prerequisite

Python and a package-management system, preferably PIP. There are a lot of guides on how to get both Python and PIP installed on your computer, so I will not cover that here. Nonetheless, I might suggest installing Anaconda; if you’re looking for pointers on how to get both Python and PIP installed efficiently. Anaconda also comes loaded with a lot of packages which reduces the need to separately install packages with PIP.

By the way, read this guide if you need information on how to install packages with PIP.

Step-by-step guide for implementation

Create a folder on your Desktop (or somewhere else on your computer if you wish) and give it an appropriate name. If you are unfamiliar with programming, I suggest you create the folder on your Desktop for simplicity’s sake.

Clone my repo into your newly created folder (see step 1), henceforth “your Project folder”, or paste my code into files you have created yourself, or write my code yourself, or do a combination of the aforementioned procedures. The main point is this: Make sure you get the contents of my files into your files (with the same file names) in your Project folder.

To interact with your Google Drive, through Google’s API, you need to get a Credential file from Google Developers Console. This step of the guide is another step-by-step guide on how to proceed to get your Google Drive Client ID file. If you already have the file you can ignore the instructions that follow below, move the Client ID file into your Project folder and continue to step 4. Otherwise, please follow along:

  1. Go to https://console.developers.google.com/ and log into the relevant Google Account, i.e., the Google Account connected to the Google Drive you want to interact with.
  2. Click on Credentials (in the left-hand menu).
  3. Click on “Create credentials”.
  4. Choose “OAuth Client ID”.
  5. Name your OAuth client ID. You can name it whatever you want.
  6. Then for both “Authorized JavaScript origins” and “Authorized redirect URIs” input fields write: http://localhost:8080 then save inside each field.
  7. Save (press the big blue button at the bottom of the page).
  8. Download the JSON file of the newly created credential.
  9. Rename the JSON file (now located in your Downloads folder) to:
    client_secrets.json
  10. Move the client_secrets.json file to your Project folder from your Downloads folder.

Go to https://www.google.com/drive/ and log into the relevant Google Account. By the way, the relevant Google Account has to be the same account as the one you created for the Google Drive Client ID file (see step 3).

Inside your Google Drive, create a new folder and name it something appropriate, this is the folder you will use for syncing. After you have created the folder, open the folder.

While inside the newly created folder from step 5. Copy the ID of the folder. The ID is the key behind the last “/” in the URL.

Open the folder_sync_registrer.txt file in your Project folder. Highlight the text Pathname for your Project folder and past the folder ID, you copied in step 6. Save the file, but do not close it yet.

Go and copy the pathname of your Project folder. There are several ways to get the pathname for a particular folder, here’s a guide.

Navigate back to the folder_sync_registrer.txt file in your Project folder. Highlight the text Google Drive ID and past the pathname, you copied in step 8. Save the file and close it.

You are all done. You can now run the upload file. Open the Terminal and, type and enter: cd ~ /Desktop/your Project folder name (if you created your Project folder on your Desktop), before you type and enter: python upload.py.

Tip

If you wish to upload more folders. Just create more folders in your Google Drive, then add the pathnames of the local folders and the new Google Drive IDs to the folder_sync_registrer.txt file. See example below:

“/Users/sam/Desktop/folder1/”,”15vYFyHRd83FZgyfjAJ0XQtl8gm7–3WFy”
“/Users/sam/Desktop/folder2/”,”15vYFyHRd83FZgyfjAJ0XQtl8gm7–3WFx”
“/Users/sam/Desktop/folder3/”,”15vYFyHRd83FZgyfjAJ0XQtl8gm7–3WFz”

Let me know what you think of the guide in the comments below.

The Startup

Get smarter at building your thing. Join The Startup’s +792K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Sam Stands

Written by

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +792K followers.

Sam Stands

Written by

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +792K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store