googleDriveLib module
Overview
This module provides utilities for interacting with Google Drive API using a service account. It is based on the guide: https://medium.com/@matheodaly.md/using-google-drive-api-with-python-and-a-service-account-d6ae1f6456c2
Functions
Logging
Connection & Authentication
- googleDriveLib.connect_google_driver(service_account_id: str = 'artscroisesServiceAccount') Any[source]
Connect to Google Drive API using service account credentials.
Retrieves service account credentials from secrets vault and builds Drive API service object.
- Parameters:
service_account_id – Service account key name in secrets vault
- Returns:
Google Drive API service object, or None if connection fails
File Operations
- googleDriveLib.get_files(service: Any = None, folder_id: str | None = None) dict[str, Any] | None[source]
List files from Google Drive folder (excluding published files).
- Parameters:
service – Google Drive API service object
folder_id – Google Drive folder ID to list files from
- Returns:
Dict with ‘files’ key containing list of file metadata dicts, or None if operation fails or parameters missing
- googleDriveLib.download_file(service: Any = None, files: list[dict[str, Any]] | None = None, folder: str = 'input') None[source]
Download files from Google Drive to local folder.
Downloads each file in the list with progress reporting.
- Parameters:
service – Google Drive API service object
files – List of file metadata dicts with ‘id’ and ‘name’ keys
folder – Local folder path to save files to (default: “input”)
- googleDriveLib.upload_file(service: Any, file: str, mimetype: str = 'text/csv') None[source]
Upload file to Google Drive.
- Parameters:
service – Google Drive API service object
file – Path to local file to upload
mimetype – MIME type of file (default: “text/csv”)
- googleDriveLib.rename_file(service: Any = None, file_id: str | None = None, new_title: str | None = None) dict[str, Any] | None[source]
Rename file in Google Drive.
- Parameters:
service – Google Drive API service object
file_id – ID of file to rename
new_title – New name for the file
- Returns:
Updated file metadata dict, or None if any parameter is missing
Module Details
Google Drive API integration using service account credentials.
Provides functions to: - Connect to Google Drive via service account - List files from Drive folders - Download files to local folder - Upload files to Drive - Rename Drive files
Uses service account credentials stored in secrets vault. Logs output to console and sendMail.log file.
- googleDriveLib.connect_google_driver(service_account_id: str = 'artscroisesServiceAccount') Any[source]
Connect to Google Drive API using service account credentials.
Retrieves service account credentials from secrets vault and builds Drive API service object.
- Parameters:
service_account_id – Service account key name in secrets vault
- Returns:
Google Drive API service object, or None if connection fails
- googleDriveLib.get_files(service: Any = None, folder_id: str | None = None) dict[str, Any] | None[source]
List files from Google Drive folder (excluding published files).
- Parameters:
service – Google Drive API service object
folder_id – Google Drive folder ID to list files from
- Returns:
Dict with ‘files’ key containing list of file metadata dicts, or None if operation fails or parameters missing
- googleDriveLib.rename_file(service: Any = None, file_id: str | None = None, new_title: str | None = None) dict[str, Any] | None[source]
Rename file in Google Drive.
- Parameters:
service – Google Drive API service object
file_id – ID of file to rename
new_title – New name for the file
- Returns:
Updated file metadata dict, or None if any parameter is missing
- googleDriveLib.download_file(service: Any = None, files: list[dict[str, Any]] | None = None, folder: str = 'input') None[source]
Download files from Google Drive to local folder.
Downloads each file in the list with progress reporting.
- Parameters:
service – Google Drive API service object
files – List of file metadata dicts with ‘id’ and ‘name’ keys
folder – Local folder path to save files to (default: “input”)
- googleDriveLib.upload_file(service: Any, file: str, mimetype: str = 'text/csv') None[source]
Upload file to Google Drive.
- Parameters:
service – Google Drive API service object
file – Path to local file to upload
mimetype – MIME type of file (default: “text/csv”)
The module provides the following capabilities:
Authentication: Connect to Google Drive using service account credentials
File Listing: Retrieve files from specific folders with metadata
File Download: Download files from Google Drive to local storage
File Upload: Upload files to Google Drive
File Rename: Rename files in Google Drive
Usage Example
import googleDriveLib as gd
# Connect to Google Drive
service = gd.connect_google_driver("myServiceAccount")
# Get files from a folder
files = gd.get_files(service, folder_id="your_folder_id")
# Download files
gd.download_file(service, files["files"], folder="downloads")
# Rename a file
gd.rename_file(service, fileId="file_id", newTitle="new_name.pdf")