Uploading files on Google Drive using Python
In the modern era of the internet, a large number of mundane or work-related tasks are carried out over the internet. Compromise of data stored on a device could cause massive harm to an individual/company. Considering the rise in the number of malware, viruses, worms, trojan horse etc the security of data at rest has become a concern for many people.
One of the most common ways of protecting the data is by creating a backup of it. While backups do provide some protection to critical data, it should be situated far away from the original data. Such that even if the system containing the original data gets compromised somehow, the backup data would still be safe. Cloud storage is a technology built to fulfil that purpose.
Any person having a google account can use 15 Gigabytes of free cloud storage for storing their data. This solves the problem of an offsite backup. But uploading file data every time could be a little cumbersome. So to ease that process, we will create a python program that looks inside a directory and uploads any files inside it to our Google Drive account.
For this purpose, we will be using
pydrive library. This module is not preloaded with python. So to install it execute the following command in the command-line:
pip install pydrive
Creating OAuth credential
For authenticating successfully to our google account every time we want to upload some data to it, we need to create an OAuth credential.
After completing the method above we would end up with a file of a name similar to client_secret_(really long ID).json. Rename the file to “client_secrets.json” and place it in the same directory where the main python program will be created.
The above code upon execution executes an instance of the default browser of your OS. It then asks for providing permissions to pydrive for accessing your Google Drive account. When all the desired permissions are granted, the browser shows a message
The authentication flow has completed.
After which the process of uploading files to Google Drive initiates.
Certain things to keep in mind while using the above program:-
- Make sure “client_secrets.json” is inside the same directory as the Python program
- The directory(path) should not contain any subdirectories inside it
- Your Google Drive should have sufficient empty space in order to incorporate for new files
- The new files will be created in the root of Google Drive storage