Sunday, November 17, 2024
Google search engine
HomeLanguagesUploading files on Google Drive using Python

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.

Code




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
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments