Uploading files on Google Drive using Python
Last Updated :
16 Jul, 2020
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.
Code
Python3 1==
from pydrive.drive import GoogleDrive
from pydrive.auth import GoogleAuth
# For using listdir()
import os
# Below code does the authentication
# part of the code
gauth = GoogleAuth()
# Creates local webserver and auto
# handles authentication.
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
# replace the value of this variable
# with the absolute path of the directory
path = r"C:\Games\Battlefield"
# iterating thought all the files/folder
# of the desired directory
for x in os.listdir(path):
f = drive.CreateFile({'title': x})
f.SetContentFile(os.path.join(path, x))
f.Upload()
# Due to a known bug in pydrive if we
# don't empty the variable used to
# upload the files to Google Drive the
# file stays open in memory and causes a
# memory leak, therefore preventing its
# deletion
f = None
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
Similar Reads
Upload and Download files from Google Drive storage using Python 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 Pytho
6 min read
Get list of files and folders in Google Drive storage using Python In this article, we are going to have a look at how can we get a list of files (or folders) stored in our Google Drive cloud storage using Google Drive 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 create a simple Python s
4 min read
Upload files in Python In this article, we will be looking into the process of file uploading in Python using cgi environment. One often comes across various web applications in which the client or the users is required to upload data in the form of a file(eg. image file, an audio file, text file, etc). There are two aspe
3 min read
How to Upload File in Python-Flask File uploading is a typical task in web apps. Taking care of file upload in Flask is simple all we need is to have an HTML form with the encryption set to multipart/form information to publish the file into the URL. The server-side flask script brings the file from the request object utilizing the r
2 min read
How to do Cloud File Sharing using Python? In this article, we will see how to share files online using a free, secure platform called GoFile. We can make it available for anyone through the link generated with the help of Python. Gofile is a platform for sharing and storing files. You have no restrictions on how much content you can save or
4 min read