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.
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.
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
1Create 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.
2Clone 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.
3To 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:
- 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.
- Click on Credentials (in the left-hand menu).
- Click on “Create credentials”.
- Choose “OAuth Client ID”.
- Name your OAuth client ID. You can name it whatever you want.
- Save (press the big blue button at the bottom of the page).
- Download the JSON file of the newly created credential.
- Rename the JSON file (now located in your Downloads folder) to:
- Move the client_secrets.json file to your Project folder from your Downloads folder.
4 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).
5Inside 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.
6While 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.
7Open 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.
8Go and copy the pathname of your Project folder. There are several ways to get the pathname for a particular folder, here’s a guide.
9Navigate 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.
10 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.
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:
Let me know what you think of the guide in the comments below.