Skip to content

Easy access of Google Drive files via Python functions

License

Notifications You must be signed in to change notification settings

theunissenlab/gdrive_python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Google Drive Access via Python (gdrive-access)

gdrive-access is a simplified set of Python functions for navigating Google Drive folders and uploading/downloading files, built on PyDrive. To get set up, you will need to install gdrive-access and run the "setup credentials" script (python -m gdrive_access.setup_credentials --dir CREDENTIALS_DIR) to give it permission to access your Google Drive through the Google Drive web API.

1 Install

gdrive-access should work on any version of Python 3 but has only been tested on Python 3.9. It is recommended to install it in a virtual environment.

To install the latest

pip install git+https://github.com/theunissenlab/[email protected]

First time credentials setup

Run the following script and follow the instructions carefully. Optionally, specify a CREDENTIALS_DIR where gdrive-access will put credential files (defaults to current working directory). It will create files settings.yaml and credentials.json in that directory. You will also create a file in it called client_secrets.json during the setup process.

setup_gdrive_credentials --dir CREDENTIALS_DIR

2 Usage

Import the GDriveCommands class

from gdrive_access import GDriveCommands

Initialize the object and authenticate

Alternatively, specify the custom path to CREDENTIALS_DIR/settings.yaml if you did not use the default during setup.

g = GDriveCommands("settings.yaml")

Access Files

For these methods, if the first argument is not an instance of pydrive2.files.GoogleDriveFile, will assume the path is relative to the root directory.

Get a pointer to a specific folder or path.

g.find(GDRIVE_DIRECTORY, *path_elements)  # -> GDRIVE_FILE/GDRIVE_DIRECTORY
g.find(*path_elements)                    # -> GDRIVE_FILE/GDRIVE_DIRECTORY

List the contents of a folder.

g.ls(GDRIVE_DIRECTORY, *path_elements)      # -> list of GDRIVE_FILEs
g.ls(*path_elements)                        # -> list of GDRIVE_FILEs

Check if a given path exists

g.exists(GDRIVE_DIRECTORY, *path_elements)  # -> bool
g.exists(*path_elements)                    # -> bool

Download Files

g.download_file(GDRIVE_FILE, local_path, overwrite=g.Overwrite.NEVER)
g.download_files([GDRIVE_FILE1, GDRIVE_FILE2, ...], local_folder_path, overwrite=g.Overwrite.NEVER) 
g.download_folder(GDRIVE_DIRECTORY, local_folder_path, overwrite=g.Overwrite.NEVER) 

Upload Files/Create Folders

g.create_folder(GDRIVE_DIRECTORY, folder_name)  # -> GDRIVE_DIRECTORY
g.upload_file(local_file_path, GDRIVE_DIRECTORY, overwrite=g.Overwrite.ON_MD5_CHECKSUM_CHANGE)

3 Uninstall

pip uninstall gdrive-access

4 TODO

  • Have a way to resolve when multiple files have the same name?

About

Easy access of Google Drive files via Python functions

Resources

License

Stars

Watchers

Forks

Packages

No packages published