Skip to content

File management tools for Google Drive using pydrive

License

Notifications You must be signed in to change notification settings

preethampaul/gdrive2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gdrive2

What is gdrive2?

Documentation Status GitHub license GitHub release

gdrive2 helps users to easily access files from Google Drive using paths, instead of File ids. Google API requires the users to know the File ID to access it, but this package which is built using the pyDrive package allows users to use a file path to access the file or folder.

In addition to this, the package also provides commands that can be called from a terminal, like cd, ls, pull or push, to quickly view, modify, download or upload files from or to Google drive using a python code or just the command line.

Setup:

pip install gdrive2

Documentation:

For more details about setup and commands, check gdrive2.readthedocs.io.

Quickdemo 1 : Basics

Lets see how gdrive2 can be used from command line.

Open terminal and set some folder where you intend to download or from where you intend to upload files.

$ gd init

When asked for a username, this is not same as the google username. More about this explained in the username documentation. Enter some nickname (for example, mygdrive2) you would like to give to your google account, so that you can use this for quick authentication into your account in future. This should take you to a Oauth Consent screen, where you'll be asked to enter your Google username and password. You are good, if you see this a html page showing this.

The authentication is successful.

Once authentication is done, from the current working folder, you can try several gdrive2 commands. Just like git, gdrive2 also creates a hidden folder .gd which contains information about the fileIDs, driveIDs etc. More about this explained in the parents documentation.

⚠️ After authentication of a new account, the credentials are stored in ROOT_PATH/api_data folder. The contents of this folder must be handled with discretion.

After, initialization and authentication, you can use all the gdrive2 commands from this directory.

$ gd status
---------------
Parent dicts :
---------------
// origin // 
username : mygdrive2
path     : ''
id       : 'root'
drive    : 'My Drive'
driveId  : 'root'
client_sec : 'client_secrets'

No files/folders staged.

This command shows the contents of .gd/.gdinfo.json file created during initialization. The structure of this and the meaning of each term are explained in the parents documentation. For example, the folders in my drive are in this hierarchy.

My Drive
|
|___fruits
|      |___hard
|      |     |__apple.jpg
|      |     |__guava.png
|      |
|      |___soft
|            |__grapes.jpg
|
|___flowers
      |___yellow
      |      |__sunflower.tiff
      |
      |___red
	   |__roses.jpg
	   |__poppy.png

To list my files in the path stored in the path variable of the dictionary showed above:

$ gd ls
: D[2351.5kB] 1 : fruits
: D[6571.2kB] 2 : flowers
 

My Drive is the name of the google drive's root folder. '' (empty string) denotes the path to it. This shows that there are two folders in my root folder - fruits and flowers.

$ gd ls fruits/hard
: f[1000.5kB] 1 : apple.jpg
: F[321.0kB] 2 : guava.jpg

This is how the paths can be simply used to get the file contents. Now, I'll change my path from '' to 'fruits/hard':


$ gd cd fruits/hard
origin cwd changed to 'fruits/hard'

$ gd ls
: f[1000.5kB] 1 : apple.jpg
: F[321.0kB] 2 : guava.jpg

$ gd status
---------------
Parent dicts :
---------------
// origin // 
username : mygdrive2
path     : 'fruits/hard'
id       : '34eWf..iT23'
drive    : 'My Drive'
driveId  : 'root'
client_sec : 'client_secrets'

No files/folders staged.

If I want to download the folder:

$ gd pull

Or, to just download the apple.jpg:

$ gd pull origin apple.jpg

If I want to upload berry.png to fruits/hard:

$ gd add 
$ gd cd fruits/hard
$ gd push

Quickdemo 2 : Multiple Parent functionality

.gd/.gdinfo.json is a dictionary with each key defined as a parent (just like a remote in git). The first key of this dictionary is 'default_parent' whose value is the name of a default parent. Multiple parents can be set with multiple usernames(i.e. google accounts), paths, shared drives or even different client secrets files and one of these can be given the status of 'default_parent'. This makes frequent uploading and downloading as easy as git pull and push functions.

To add a new parent:

$ git init -add

Once a parent is added (say, origin2), we can assign different parameters (like username, path etc.) and push files simultaneously

$ git push origin origin2

Quickdemo 3 : Use of these commands in python script:

All these commands can be used in a python script as shown below. The only difference from the terminal commands is that apart from the main function (init, status, etc), the optional arguements must be passed as strings in a list. If there is no arguements, an empty list must be passed in the function.

>> import gdrive2 as gd
>> 
>> gd.init([])
>> gd.status([])
>> gd.add(['berry.jpg', 'mango.jpg'])

For more details, check gdrive2.readthedocs.io

Please post issues here or email me at [email protected]