Skip to content

upload/download google drive content in one line of code

License

Notifications You must be signed in to change notification settings

codegallivant/gdrive-python-wrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

gdrive-interact

Python script to enable users to easily upload/download files/folders to/from their Google Drive via Google Drive API.

Requirements

  • Python
  • pip modules
    • pydrive
  • Google Drive API credentials file

Usage

import gdrive_pyinteract as gdi

# Specify path to credentials file of Google Drive API
gdi.set_client_config_path("client_secrets.json")

# Authenticate 
client = gdi.authenticate_client("creds.txt") # Saves access token credentials to creds.txt. If file does not exist, one-time manual sign-in is done via browser and the file is auto-generated.

# Get id of file/folder
folder_id = gdi.get_id(client, "<drive_folder_name>/<drive_folder_name>/.../<folder_or_file_name>")

# Get list of files in a folder
file_list = gdi.list_files(client, folder_id)

# Upload a file
gdi.upload_file(client, '<drive_folder_name>/<drive_folder_name>/.../<drive_file_name>', "C:/.../<system_directory_name>", "<system_file_name>")

# Upload a folder
gdi.upload_folder(client, '<drive_folder_name>/<drive_folder_name>/.../<drive_folder_name>',rf"C:/.../<system_folder_name>")

# Download a file
gdi.download_file(client, "<drive_folder_name>/<drive_folder_name>/.../<drive_file_name>", "C:/.../<system_directory_name>")

# Download a folder
gdi.download_folder(client, "<drive_folder_name>/<drive_folder_name>/.../<drive_folder_name>", "C:/.../<system_directory_name>", files_only=False) # Set files_only = True if you only want the files within, and not the folder itself

How to create a credentials file

In order to contact and successfully authenticate with your Drive account, you must have a credentials file. In the above example, it is client_secrets.json. To create one, see Google's API Console. Create a project and enable Google Drive API. Download the credentials file.

About

upload/download google drive content in one line of code

Topics

Resources

Stars

Watchers

Forks