kaxil commented on issue #14598:
URL: https://github.com/apache/airflow/issues/14598#issuecomment-790644229


   You can already do this via an XComBackend:
   
   
https://airflow.apache.org/blog/airflow-1.10.12/#allow-defining-custom-xcom-class
   
   There is also an example in 
https://medium.com/apache-airflow/airflow-2-0-dag-authoring-redesigned-651edc397178
   
   ```python
   import pandas as pd
   from typing import Any
   from airflow.models.xcom import BaseXCom
   from airflow.providers.google.cloud.hooks.gcs import GCSHook
   class GCSXComBackend(BaseXCom):
      PREFIX = "xcom_gs://"
      BUCKET_NAME = "airflow-xcom-backend"
      @staticmethod
      def serialize_value(value: Any):
          if isinstance(value, pd.DataFrame):
              hook = GCSHook()
              object_name = "data_" + str(uuid.uuid4())
              with hook.provide_file_and_upload(
                  bucket_name=GCSXComBackend.BUCKET_NAME,
                  object_name=object_name,
              ) as f:
                  value.to_csv(f.name)
              # Append prefix to persist information that the file
              # has to be downloaded from GCS
              value = GCSXComBackend.PREFIX + object_name
          return BaseXCom.serialize_value(value)
      @staticmethod
      def deserialize_value(result) -> Any:
          result = BaseXCom.deserialize_value(result)
          if isinstance(result, str) and 
result.startswith(GCSXComBackend.PREFIX):
              object_name = result.replace(GCSXComBackend.PREFIX, "")
              with GCSHook().provide_file(
                  bucket_name=GCSXComBackend.BUCKET_NAME,
                  object_name=object_name,
              ) as f:
                  f.flush()
                  result = pd.read_csv(f.name)
          return result
   ```


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to