Google Drive

Overview

The GoogleDrive class allows you to interact with Google Drive. You can update permissions with this connector.

In order to instantiate the class, you must pass Google service account credentials as a dictionary, or store the credentials as a JSON file locally and pass the path to the file as a string in the GOOGLE_DRIVE_CREDENTIALS environment variable. You can follow these steps:

  • Go to the Google Developer Console and make sure the “Google Drive API” is enabled.

  • Go to the credentials page via the lefthand sidebar. On the credentials page, click “create credentials”.

  • Choose the “Service Account” option and fill out the form provided. This should generate your credentials.

  • Select your newly created Service Account on the credentials main page.

  • select “keys”, then “add key”, then “create new key”. Pick the key type JSON. The credentials should start to automatically download.

You can now copy and paste the data from the key into your script or (recommended) save it locally as a JSON file.

Quickstart

To instantiate the GoogleDrive class, you can either pass the constructor a dict containing your Google service account credentials or define the environment variable GOOGLE_DRIVE_CREDENTIALS to contain a path to the JSON file containing the dict.

Use API credentials via environmental variables
from parsons import GoogleDrive
drive = GoogleDrive()
Pass API credentials as argument
from parsons import GoogleDrive
credential_filename = 'google_drive_service_credentials.json'
drive = GoogleDrive(app_creds=credential_filename)
Access Drive API methods
new_folder = drive.create_folder(name='My Folder')

API

class parsons.google.google_drive.GoogleDrive(app_creds: str | dict | Credentials | None = None)[source]

A connector for Google Drive

Parameters:

app_creds (str | dict | Credentials | None) – dict | str | Credentials Can be a dictionary of Google Drive API credentials, parsed from JSON provided by the Google Developer Console, or a path string pointing to credentials saved on disk, or a google.oauth2.credentials.Credentials object. Required if env variable GOOGLE_DRIVE_CREDENTIALS is not populated.

download_file(file_id: str) str[source]

Download file from Drive to disk. Returns local filepath.

Parameters:

file_id (str)

Return type:

str

replace_file(file_path: str, file_id: str) str[source]

Replace file in drive.

Parameters:
  • file_path (str)

  • file_id (str)

Return type:

str

upsert_file(file_path: str, parent_folder_id: str) str[source]

Create or replace file in drive folder, based on file name.

Parameters:
  • file_path (str)

  • parent_folder_id (str)

Return type:

str

copy_file(file_id: str, destination_folder_id: str | None = None, new_name: str | None = None) str[source]

Copy a file within Google Drive.

Parameters:
  • file_id (str) – str The ID of the file to copy

  • destination_folder_id (str | None) – str The ID of the destination folder. If not provided, copies to the same parent folder.

  • new_name (str | None) – str The name for the copied file. If not provided, Drive will use “Copy of [original name]”.

Returns:

The ID of the newly created copy

Return type:

str

get_permissions(file_id: str) dict[source]
Parameters:

file_id (str) – str this is the ID of the object you are hoping to share

Returns:

permission dict

Return type:

dict

share_object(file_id: str, email_addresses: list[str] | None = None, role: Literal['owner', 'organizer', 'fileOrganizer', 'writer', 'commenter', 'reader'] = 'reader', type: Literal['user', 'group', 'domain', 'anyone'] = 'user') list[dict][source]
Parameters:
  • file_id (str) – str this is the ID of the object you are hoping to share

  • email_addresses (list[str] | None) – list this is the list of the email addresses you want to share; set to a list of domains like [‘domain’] if you choose type=’domain’; set to None if you choose type=’anyone’

  • role (Literal['owner', 'organizer', 'fileOrganizer', 'writer', 'commenter', 'reader']) – str Options are – owner, organizer, fileOrganizer, writer, commenter, reader https://developers.google.com/drive/api/guides/ref-roles

  • type (Literal['user', 'group', 'domain', 'anyone']) – str Options are – user, group, domain, anyone

Returns:

List of permission objects

Return type:

list[dict]

transfer_ownership(file_id: str, new_owner_email: str) None[source]
Parameters:
  • file_id (str) – str this is the ID of the object you are hoping to share

  • new_owner_email (str) – str the email address of the intended new owner

Return type:

None