Google Drive synchronization with Python
In this guide, we will demonstrate how to use a simple Python program to upload a folder saved locally to Google Drive (through the Google Drive API). This guide is intended to be concise and straightforward, and the code can be improved upon by readers as needed.
Project motivation
Many users find the cost of installing the Google Drive app on their computers outweighs their synchronization needs. In such cases, it can be useful to have a solution for syncing specific folders with Google Drive, without installing the app or manually uploading the folder.
Prerequisite
To follow along with this guide, you will need Python and a package management system, preferably PIP. There are many guides available on how to install both Python and PIP, so I will not cover that here. If you need information on how to install packages with PIP, you can read this guide.
Step-by-step guide for implementation
1 To begin, create a new folder on your desktop (or elsewhere on your computer) and give it a descriptive name. If you are new to programming, it may be simpler to create the folder on your desktop.
2Next, clone the repository into the newly created folder (referred to as the “project folder” from here on) or copy the code into your own files within the project folder. Alternatively, you can write the code yourself or a combination of the above methods. The important thing is to make sure the contents of the repository files are in your own files with the same file names within the project folder.
3To use Google’s API to access your Google Drive, you will need to obtain a credential file from the Google Developers Console. If you already have the file, you can skip the following instructions and move the client ID file into your project folder. Otherwise, follow these steps to obtain the file:
- Go to https://console.developers.google.com/ and log in with the Google account connected to the Google Drive you want to access.
- Click on “Credentials” in the left-hand menu.
- Click on “Create credentials” and select “OAuth client ID.”
- Give your OAuth client ID a descriptive name.
- In the “Authorized JavaScript origins” and “Authorized redirect URIs” fields, enter “http://localhost:8080" and click “Save.”
- Click the big blue “Save” button at the bottom of the page.
- Download the JSON file for the newly created credential.
- Rename the JSON file (now located in your Downloads folder) to “client_secrets.json.”
- Move the “client_secrets.json” file to your project folder from your Downloads folder.
4 Go to https://www.google.com/drive/ and log in with the same account you used to create the Google Drive client ID file (see step 3).
5Inside your Google Drive, create a new folder and give it a descriptive name. This is the folder you will use for syncing.
6Open the newly created folder. Copy the ID of the folder by copying the text after the last “/” in the URL.
7Open the “folder_sync_registrer.txt” file in your project folder. Highlight the text “Google Drive ID” and paste the Google folder ID you copied in step 6. Save the file, but do not close it yet.
8Copy the pathname of your project folder. There are several ways to obtain the pathname of a particular folder, and you can find a guide here.
9Navigate back to the “folder_sync_registrer.txt” file in your project folder. Highlight the text “Pathname for your Project folder” and paste the pathname you copied in step 8. Save the file and close it.
10 You are now ready to run the upload file. Open the terminal and type cd
followed by the path to your project folder (e.g. cd /Desktop/your_project_folder_name
if you created the folder on your desktop). Then, type python upload.py
and press enter.
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.