Skip to main content

Google Drive API for service accounts

Project description

pygdrive

pygdrive is a wrapper library of google-api-python-client that simplifies uploading and downloading files.

Project Info

Features of pygdrive

  • Simplifies OAuth2.0 with service_account into just a few lines with flexible settings.
  • Wraps Google Drive API into a class which can search, upload and download files and folders.

How to install

You can install PyDrive with regular pip command.

pip install pygdrive

You can install pygdrive from the current development version from GitHub, use:

pip install git+https://github.com/vectorspace-ai/pygdrive.git

Creating credentials.json file

Download client_secrets.json from Google API Console. Share your Drive account's folder to your service account. Your service account's addresss looks like XXX@XXX.iam.gserviceaccount.com. Then your service account can see the shared folder from your Drive account.

Сreating Gdrive object

  from pygdrive import Gdrive
  SCOPES = ['https://www.googleapis.com/auth/drive']
  CREDENTIALS_FILEPATH = 'credentials.json'
  DOWNLOAD_DIR = 'download_dir/'
  UPLOAD_DIR = 'upload_dir/'
  gdrive_object = Gdrive(CREDENTIALS_FILEPATH, SCOPES, DOWNLOAD_DIR, UPLOAD_DIR)

File management made easy

Upload file to certain folder:

Make sure that folder exists and filename appears in UPLOAD_DIR

folder_id, _ = gdrive_object.find_folder('coingecko')
gdrive_object.upload_file(folder_id, 'filename.extension')

You could specify your local upload path if it differs from UPLOAD_DIR:

folder_id, _ = gdrive_object.find_folder('coingecko')
gdrive_object.upload_file(folder_id, 'filename.extension', 'upload_storage/')

Create folder:

parent_id = 'PARENT ID'
folder_id = gdrive_object.create_folder(parent_id, 'folder_name')

Make sure that DOWNLOAD_DIR exists and filename.extension in google drive folder.

file_id, file_name = gdrive_object.find_file('filename.extension')
gdrive_object.download_file(file_id, file_name)

You could specify your local download path if it differs from DOWNLOAD_DIR:

file_id, file_name = gdrive_object.find_file('filename.extension')
gdrive_object.download_file(file_id, file_name, 'some_updated_local_path/')

After uploading/downloading files you should expect uploaded/downloaded file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pygdrive-1.0.5.tar.gz (5.7 kB view details)

Uploaded Source

File details

Details for the file pygdrive-1.0.5.tar.gz.

File metadata

  • Download URL: pygdrive-1.0.5.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.5

File hashes

Hashes for pygdrive-1.0.5.tar.gz
Algorithm Hash digest
SHA256 ec6d884e414126f592ef838832b26b51ba57b6ac538df04a47bdec1aff2c8572
MD5 268a3b9272e2dd6a5d547f6e7ea24b7a
BLAKE2b-256 520e8db15c0cb62858abb72cc2014e093db46502ddc5385a0ce587036535b8d2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page