Skip to content

Google Drive

Google Drive Actions allow interaction with Google Drive files and folders using the Google Drive API v3.

Actions

googledrive.create_folder

Creates a folder inside a parent folder. Fails if a folder with the same name already exists in the parent folder.

Parameters:

client - Google Drive service instance from googledrive.get_client.

parent_id - id of the parent folder (use root for "My Drive").

folder_name - name of the folder to be created.

Return:

id - id of the created folder.

item_name - name of the created folder.

parent_reference_id - id of the parent folder.

Exceptions:

InvalidParameters: if client, parent_id or folder_name is missing.

WrongVarType: if parameter types are invalid or client is not a Google Drive service instance.

GoogleDriveItemAlreadyExists: if a folder with the same name already exists in the parent folder.

GoogleClientAuthenticationError: authentication failure (including HTTP 401).

GoogleDriveAPIError: Drive API errors (403/other), including a creation that returns no id.

GoogleDriveItemNotFound: when the parent folder is not found (404).

ConnectionException: unexpected errors.


googledrive.delete_item

Deletes a drive item by id. By default the item is moved to the trash; with permanent=True it is deleted permanently.

Parameters:

client - Google Drive service instance from googledrive.get_client.

item_id - id of the item to delete.

permanent (optional) - if True, deletes permanently. Otherwise, moves the item to the trash. Default: False.

Return:

deleted_item_id - id of the deleted item.

Exceptions:

InvalidParameters: if client or item_id is missing.

WrongVarType: if parameter types are invalid or client is not a Google Drive service instance.

GoogleClientAuthenticationError: authentication failure (including HTTP 401).

GoogleDriveAPIError: Drive API errors (403/other).

GoogleDriveItemNotFound: when the item is not found (404).

ConnectionException: unexpected errors.


googledrive.download_file_by_id

Downloads a file from Google Drive by its id. Google native files are exported: Docs to .docx, Sheets to .xlsx, Slides to .pptx and Drawings to .pdf. If download_path has no extension, the export extension is appended to it.

Parameters:

client - Google Drive service instance from googledrive.get_client.

item_id - id of the file.

download_path - path to save the downloaded file. Missing folders are created.

Return:

download_path - path where the file was saved (may include the added extension for exported files).

Exceptions:

InvalidParameters: if required parameters are missing, if item_id is a folder, or if it is a Google file type that can't be downloaded or exported (e.g. Forms).

WrongVarType: if parameter types are invalid or client is not a Google Drive service instance.

GoogleClientAuthenticationError: authentication failure (including HTTP 401).

GoogleDriveAPIError: Drive API errors (403/other).

GoogleDriveItemNotFound: when the item is not found (404).

ConnectionException: unexpected errors.


googledrive.get_client

Creates a Google Drive service instance (Drive API v3) to interact with Google Drive. Three kinds of JSON credentials are accepted:

  • Service account: optionally impersonates a user through delegated_user (domain-wide delegation only).
  • OAuth client (installed app / web): on the first run a browser window opens for consent; the user token is saved in token_path and reused (and refreshed) afterwards.
  • OAuth user token (the file saved in token_path): refreshed without opening a browser, so it runs headless (e.g. AWS Lambda).
Parameters:

credentials_path - path to the service account, OAuth client or OAuth user token JSON file.

delegated_user (optional) - email of the user to impersonate (service account with domain-wide delegation only).

token_path (optional) - path to store/reuse the OAuth user token (OAuth client only).

Return:

client - Google Drive service instance.

Exceptions:

InvalidParameters: if credentials_path is missing, is not a valid file, or is not a service account, OAuth client or OAuth user token JSON.

WrongVarType: if parameter types are invalid.

GoogleClientAuthenticationError: authentication failure.

ConnectionException: unexpected errors.


googledrive.get_drive_id

Gets a drive id. Without drive_name, returns the id of the user's "My Drive" root folder; with drive_name, returns the id of the shared drive with that exact name.

Parameters:

client - Google Drive service instance from googledrive.get_client.

drive_name (optional) - name of a shared drive.

Return:

drive_id - id of the drive.

Exceptions:

InvalidParameters: if client is missing.

WrongVarType: if parameter types are invalid or client is not a Google Drive service instance.

GoogleDriveItemNotFound: when the shared drive is not found.

GoogleClientAuthenticationError: authentication failure (including HTTP 401).

GoogleDriveAPIError: Drive API errors (403/other).

ConnectionException: unexpected errors.


googledrive.get_items_by_path

Gets the items of a Google Drive folder, filtered by an optional regex. Trashed items are ignored.

Parameters:

client - Google Drive service instance from googledrive.get_client.

path - folder id (or root) or a path like Folder/Subfolder, relative to "My Drive". The value is first tried as an id; if no item has that id, it is resolved as a path.

regex (optional) - regex to filter items by name.

full_metadata (optional) - if True, fetches all pages of results. Otherwise, only the first page is returned. Default: False.

Return:

total - number of items found (0 for an empty folder).

items - list of items with name, id, type (Folder for folders, otherwise the MIME type) and lastModifiedDateTime.

Exceptions:

InvalidParameters: if client or path is missing.

WrongVarType: if parameter types are invalid or client is not a Google Drive service instance.

GoogleDriveItemNotFound: when the folder or any part of the path is not found.

GoogleClientAuthenticationError: authentication failure (including HTTP 401).

GoogleDriveAPIError: Drive API errors (403/other).

ConnectionException: unexpected errors.


googledrive.get_user_info

Gets the information of the authenticated user.

Parameters:

client - Google Drive service instance from googledrive.get_client.

Return:

user_id - permission id of the user.

email - email address of the user.

display_name - display name of the user.

Exceptions:

InvalidParameters: if client is missing.

WrongVarType: if client is not a Google Drive service instance.

GoogleClientAuthenticationError: authentication failure (including HTTP 401).

GoogleDriveAPIError: Drive API errors (403/other).

ConnectionException: unexpected errors.


googledrive.move_item

Moves an item to another folder, optionally renaming it. The item is removed from all its previous parent folders.

Parameters:

client - Google Drive service instance from googledrive.get_client.

item_id - id of the item to move.

destination_id - id of the destination folder.

new_name (optional) - new name for the item.

Return:

id - id of the moved item.

item_name - name of the moved item.

parent_reference_id - id of the destination folder.

Exceptions:

InvalidParameters: if client, item_id or destination_id is missing.

WrongVarType: if parameter types are invalid or client is not a Google Drive service instance.

GoogleClientAuthenticationError: authentication failure (including HTTP 401).

GoogleDriveAPIError: Drive API errors (403/other), including a move that returns no id.

GoogleDriveItemNotFound: when the item or the destination is not found (404).

ConnectionException: unexpected errors.


googledrive.upload_item

Uploads a local file into a Google Drive folder (resumable upload). The file keeps its local name; an existing file with the same name is not replaced, a new item is created.

Parameters:

client - Google Drive service instance from googledrive.get_client.

folder_id - id of the destination folder (a path is not accepted).

file_path - local path to the file to upload (must exist and be a file).

Return:

id - id of the uploaded item.

item_name - uploaded file name.

size - file size in bytes as returned by the API (None if not returned).

Exceptions:

InvalidParameters: if required parameters are missing or file_path is not a valid file.

WrongVarType: if parameter types are invalid or client is not a Google Drive service instance.

GoogleClientAuthenticationError: authentication failure (including HTTP 401).

GoogleDriveAPIError: Drive API errors (403/other), including an upload that returns no item id.

GoogleDriveItemNotFound: when the destination folder is not found (404).

ConnectionException: unexpected errors.