Use Python to upload files on Google Drive

In the contemporary technology of the internet, a massive number of mundane or work-associated obligations are carried out over the internet. Compromise of information saved on a device could cause large damage to an individual/company. Considering the upward thrust in the wide variety of malware, viruses, worms, bug, etc the safety of information at rest has emerged as a subject for lots of people.

One of the most common methods of shielding the data is by creating a backup of it. While backups do provide some safety to critical statistics, they need to be situated a long way faraway from the original records. Such that although the device containing the original data gets compromised somehow, the backup facts would nonetheless be safe. Cloud garage is an era constructed to fulfill that purpose.

Any individual having a google account can use 15 Gigabytes of free cloud garage for storing their statistics. This solves the hassle of an offsite backup. But uploading file facts each time could be a little cumbersome. So to ease that process, we will create a python program that looks internal a directory and uploads any documents interior it to our Google Drive account.

For this purpose, we’ll be exploitation pydrive library. This module isn’t preloaded with python. therefore to put in it execute the subsequent command within the command-line:

pip install pydrive

Creating OAuth written document
For authenticating with success to our google account whenever we wish to transfer some knowledge thereto, we want to form associate OAuth written document.
After finishing the strategy higher than we’d find yourself with a file of a reputation almost like client_secret_(really long ID).json. Rename the file to “client_secrets.json” and place it within the same directory wherever the most python program are 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 on top of 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. once all the specified permissions are granted, the browser shows a message

The authentication flow has completed.

After that the method of uploading files to Google Drive initiates.

Certain things to stay in mind whereas victimization the on top of program:-

Make sure “client_secrets.json” is within a similar directory because the Python program
The directory(path) shouldn’t contain any subdirectories within it
Your Google Drive ought to have decent empty area so as to include for brand spanking new files
The new files are going to be created within the root of Google Drive storage

9 Likes