cedar.stores.gdrive module¶
Helper utilities for using Google Drive
-
class
cedar.stores.gdrive.
GDriveStore
(service)[source]¶ Bases:
object
Store GEE “pre-ARD” images and metadata on Google Drive
- Parameters
service (googleapiclient.discovery.Resource) – Google Drive API service
-
classmethod
from_credentials
(client_secrets_file=None, credentials_file=None)[source]¶ Create and/or load credentials and create the store
-
retrieve_image
(self, dest, name, path=None, overwrite=True)[source]¶ Retrieve (pieces of) an image from the Google Drive
-
retrieve_metadata
(self, dest, name, path=None, overwrite=True)[source]¶ Retrieve image metadata from the GCS
-
cedar.stores.gdrive.
build_gdrive_service
(credentials)[source]¶ Return Google Drive API service, either by credentials or file
- Parameters
credentials (google.oauth2.credentials.Credentials) – User credentials, including access token, for using the application.
- Returns
GDrive v3 API resource
- Return type
googleapiclient.discovery.Resource
-
cedar.stores.gdrive.
delete
(service, name, parent_id=None, appProperties=False)[source]¶ Delete a file/folder on Google Drive
- Parameters
- Returns
ID of deleted file/folder
- Return type
-
cedar.stores.gdrive.
download_file
(service, name, dest, parent_id=None)[source]¶ Download a file to a destination directory
- Parameters
- Returns
Filename written to
- Return type
- Raises
FileExistsError – Raised if file exists in destination but not allowed to overwrite,
ValueError – Raised if the file given does not exist in Google Drive
-
cedar.stores.gdrive.
download_file_id
(service, file_id, dest)[source]¶ Download a file to a destination directory using its ID
- Parameters
service (googleapiclient.discovery.Resource) – Google API resource for GDrive v3
file_id (str) – ID of file on Google Drive
dest (str or pathlib.Path) – Destination filename
- Returns
Filename written to
- Return type
- Raises
FileExistsError – Raised if file exists in destination but not allowed to overwrite,
ValueError – Raised if the file given does not exist in Google Drive
-
cedar.stores.gdrive.
exists
(service, name, parent_id=None, directory=False, trashed=False, appProperties=False)[source]¶ Check if file/folder exists
- Parameters
service (googleapiclient.discovery.Resource) – Google API resource for GDrive v3
name (str) – Name of file/folder
parent_id (str, optional) – Parent ID of folder containing file (to narrow search)
directory (bool, optional) – True if file needs to also be a directory
trashed (bool, optional) – Search in the trash?
appProperties (bool) – Search for application-specific files using
appProperties
- Returns
Returns object ID if exists, otherwise empty string
- Return type
-
cedar.stores.gdrive.
find_credentials
(client_secrets_file=None, credentials_file=None)[source]¶ Locate GDrive client secrets & credentials files
-
cedar.stores.gdrive.
get_appProperties
()[source]¶ Returns private “appProperties” to include when creating files
-
cedar.stores.gdrive.
get_credentials
(client_secrets_file=None, credentials_file=None, no_browser=True)[source]¶ Get OAuth2 Credentials for Google Drive
- Parameters
client_secrets_file (str or Path) – Filename of “client_secrets.[…].json” file
credentials_file (str or Path) – Filename of user credentials to load, or to save to for future use. If not provided, will use default location.
no_browser (bool, optional) – Disables opening a web browser in favor of prompting user to authenticate using a terminal prompt.
- Returns
credentials (google.oauth2.credentials.Credentials) – User credentials, including access token, for using the application.
credentials_file (str) – File storing user credentials
-
cedar.stores.gdrive.
list_dirs
(service, parent_id=None, name=None)[source]¶ List folders on Google Drive
- Parameters
- Yields
list[dict] – Info about objects stored on Google Drive (keys=(‘id’, ‘name’, ))
-
cedar.stores.gdrive.
list_objects
(service, parent_id=None, name=None, q=None, appProperties=False)[source]¶ List files/folders on Google Drive
- Parameters
service (googleapiclient.discovery.Resource) – Google API resource for GDrive v3
parent_id (str, optional) – Parent ID of folder to list (to narrow search)
name (str, optional) – Name to search for (don’t include asterisks)
q (str or Sequence[str], optional) – Additional search query parameters
appProperties (bool) – Search for application-specific files using
appProperties
- Yields
list[dict] – Info about objects stored on Google Drive (keys=(‘id’, ‘name’, ))
-
cedar.stores.gdrive.
load_credentials
(filename)[source]¶ Load Google OAuth2 credentials from file
- Returns
- Return type
google.oauth2.credentials.Credentials
-
cedar.stores.gdrive.
mkdir
(service, name, parent_id=None, check=False)[source]¶ Make a directory on GDrive
- Parameters
service (googleapiclient.discovery.Resource) – Google API resource for GDrive v3
name (str) – Directory to create
parent_id (str, optional) – ID of parent directory
check (bool, optional) – Check if directory exists before creating. If exists, will not create a new directory and instead return the ID of this directory
- Returns
Google Drive ID for directory created (or already existing)
- Return type
-
cedar.stores.gdrive.
read_json
(service, name, parent_id=None)[source]¶ Reads and returns a JSON file from Google Drive
-
cedar.stores.gdrive.
save_credentials
(credentials, filename)[source]¶ Save Google OAuth2 credentials
- Parameters
credentials (google.oauth2.credentials.Credentials) – Google OAuth2 credentials
filename (str or Path) – Path to save credentials
-
cedar.stores.gdrive.
upload_json
(service, data, name, path=None, check=False, encoding='utf-8')[source]¶ Upload JSON data to Google Drive
- Parameters
service (googleapiclient.discovery.Resource) – Google API resource for GDrive v3
data (str or dict) – JSON, either already dumped to str or as a dict
name (str) – Filename for data
path (str, optional) – Parent directory to put file. If
None
, stores in root of Google Drivecheck (bool, optional) – Check to see if the file already exists first. If so, will overwrite (or “update” instead of “create”)
encoding (str, optional) – Metadata encoding
- Returns
ID of file uploaded
- Return type