|
| 1 | +"""Storage GCS""" |
| 2 | + |
| 3 | +from collections.abc import Callable |
| 4 | +from json import dumps, loads |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +from dotflow.abc.storage import Storage |
| 8 | +from dotflow.core.context import Context |
| 9 | +from dotflow.core.exception import ModuleNotFound |
| 10 | + |
| 11 | + |
| 12 | +class StorageGCS(Storage): |
| 13 | + """ |
| 14 | + Import: |
| 15 | + You can import the **StorageGCS** class directly from dotflow providers: |
| 16 | +
|
| 17 | + from dotflow.providers import StorageGCS |
| 18 | +
|
| 19 | + Example: |
| 20 | + `class` dotflow.providers.storage_gcs.StorageGCS |
| 21 | +
|
| 22 | + from dotflow import Config |
| 23 | + from dotflow.providers import StorageGCS |
| 24 | +
|
| 25 | + config = Config( |
| 26 | + storage=StorageGCS( |
| 27 | + bucket="my-dotflow-bucket", |
| 28 | + prefix="workflows/", |
| 29 | + project="my-gcp-project" |
| 30 | + ) |
| 31 | + ) |
| 32 | +
|
| 33 | + Args: |
| 34 | + bucket (str): GCS bucket name. |
| 35 | +
|
| 36 | + prefix (str): Key prefix for all stored objects. |
| 37 | +
|
| 38 | + project (str): GCP project ID. Defaults to ADC project. |
| 39 | + """ |
| 40 | + |
| 41 | + def __init__( |
| 42 | + self, |
| 43 | + *args, |
| 44 | + bucket: str, |
| 45 | + prefix: str = "dotflow/", |
| 46 | + project: str = None, |
| 47 | + **kwargs, |
| 48 | + ): |
| 49 | + try: |
| 50 | + from google.api_core.exceptions import NotFound |
| 51 | + from google.cloud import storage as gcs |
| 52 | + except ImportError: |
| 53 | + raise ModuleNotFound( |
| 54 | + module="google-cloud-storage", |
| 55 | + library="dotflow[gcp]", |
| 56 | + ) from None |
| 57 | + |
| 58 | + self._not_found = NotFound |
| 59 | + self.client = gcs.Client(project=project) |
| 60 | + self.bucket_obj = self.client.bucket(bucket) |
| 61 | + self.bucket_obj.reload() |
| 62 | + self.prefix = prefix |
| 63 | + |
| 64 | + def post(self, key: str, context: Context) -> None: |
| 65 | + task_context = [] |
| 66 | + |
| 67 | + if isinstance(context.storage, list): |
| 68 | + for item in context.storage: |
| 69 | + if isinstance(item, Context): |
| 70 | + task_context.append(self._dumps(storage=item.storage)) |
| 71 | + else: |
| 72 | + task_context.append(self._dumps(storage=context.storage)) |
| 73 | + |
| 74 | + self._write(key=key, data=task_context) |
| 75 | + |
| 76 | + def get(self, key: str) -> Context: |
| 77 | + task_context = self._read(key) |
| 78 | + |
| 79 | + if len(task_context) == 0: |
| 80 | + return Context() |
| 81 | + |
| 82 | + if len(task_context) == 1: |
| 83 | + return self._loads(storage=task_context[0]) |
| 84 | + |
| 85 | + contexts = Context(storage=[]) |
| 86 | + for context in task_context: |
| 87 | + contexts.storage.append(self._loads(storage=context)) |
| 88 | + |
| 89 | + return contexts |
| 90 | + |
| 91 | + def key(self, task: Callable): |
| 92 | + return f"{task.workflow_id}-{task.task_id}" |
| 93 | + |
| 94 | + def _read(self, key: str) -> list: |
| 95 | + blob = self.bucket_obj.blob(f"{self.prefix}{key}") |
| 96 | + try: |
| 97 | + data = blob.download_as_text() |
| 98 | + return loads(data) |
| 99 | + except self._not_found: |
| 100 | + return [] |
| 101 | + |
| 102 | + def _write(self, key: str, data: list) -> None: |
| 103 | + blob = self.bucket_obj.blob(f"{self.prefix}{key}") |
| 104 | + blob.upload_from_string( |
| 105 | + dumps(data), |
| 106 | + content_type="application/json", |
| 107 | + ) |
| 108 | + |
| 109 | + def _loads(self, storage: Any) -> Context: |
| 110 | + try: |
| 111 | + return Context(storage=loads(storage)) |
| 112 | + except Exception: |
| 113 | + return Context(storage=storage) |
| 114 | + |
| 115 | + def _dumps(self, storage: Any) -> str: |
| 116 | + try: |
| 117 | + return dumps(storage) |
| 118 | + except TypeError: |
| 119 | + return str(storage) |
0 commit comments