You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "simi (via GitHub)" <gi...@apache.org> on 2025/06/21 18:26:58 UTC

[PR] Add ObjectStoragePath.from_conn method. [airflow]

simi opened a new pull request, #52002:
URL: https://github.com/apache/airflow/pull/52002

   ### Problem
   
   Currently, when working with `ObjectStoragePath`, you need to redundantly specify both the `conn_id` and the full URI including the protocol (scheme), even though the protocol can be inferred from the connection itself.
   
   For example, in two different environments:
   
   ```bash
   # Local development
   AIRFLOW_CONN_STORAGE='{
     "conn_type": "objectstore",
     "extra": {
       "provider": "file",
       "base_path": "/opt/airflow/storage"
     }
   }'
   
   # Production
   AIRFLOW_CONN_STORAGE='{
     "conn_type": "objectstore",
     "extra": {
       "provider": "gcs",
       "base_path": "rasa-as-test",
       "project": "rasa-test-1",
       "key_path": "/opt/airflow/config/keys/gcp.json"
     }
   }'
   ```
   
   To construct an object path, you currently need to write:
   
   ```python
   # Local
   ObjectStoragePath("file://folder/file.txt", conn_id="storage")
   
   # Production
   ObjectStoragePath("gcs://bucket/file.txt", conn_id="storage")
   ```
   
   Even though the `conn_id` is the same, the protocol part (`file://`, `gcs://`) must be manually duplicated in the path string. This defeats the purpose of having environment-specific connections and creates unnecessary friction.
   
   ---
   
   ### Proposal
   
   This PR introduces:
   
   ```python
   ObjectStoragePath.from_conn(conn: Connection, path: str) -> ObjectStoragePath
   ```
   
   This method allows you to construct a consistent object path across environments by deferring to the connection metadata for `provider` and `base_path`, which are used to build the full URI internally.
   
   ---
   
   ### Example
   
   With this change, you can now write:
   
   ```python
   ObjectStoragePath.from_conn(conn, "folder/file.txt")
   ```
   
   ...and the correct protocol and bucket/path will be resolved from the `conn.extra` fields, like `provider` and `base_path`.
   
   ---
   
   ### Considerations
   
   * Yes, the connection is resolved twice: once manually before passing to `from_conn`, and again later during internal resolution via `attach` and the I/O system. This isn't ideal, but it was the simplest way to ensure `ObjectStoragePath` has fully defined properties (`protocol`, etc.) at construction time. And it should be cached per my understanding. Maybe that's not actually problem.
   
   ### Alternatives
   
   I initially tried to implement it like `ObjectStoragePath("file.txt", conn_id="storage")`, hoping the protocol would be inferred from the connection at some early time. But this doesn't work — attributes like protocol remain empty until .fs (not 100% sure which method does that) is accessed later, which fills them in lazily.
   
   This delayed resolution makes the object unpredictable. The `from_conn` approach ensures all parts of the URI are set upfront using the connection details, so the path is complete and deterministic from the start — no hidden side effects or missing values.
   
   ---
   
   ### Feedback Welcome
   
   I'm Airflow code beginner and I'm open to better designs if there's a cleaner way to avoid the double resolution and still ensure proper initialization. I intentionally kept the change minimal to fit cleanly into the current behavior of `ObjectStoragePath` and the `airflow.io.*`.


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ObjectStoragePath.from_conn method. [airflow]

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on PR #52002:
URL: https://github.com/apache/airflow/pull/52002#issuecomment-2994805053

   Is the `objectstore` conn type a thing? It’s somewhat awkward for me that we must have a separate conn type to use with OSP, while many existing type (e.g. ftp) can do the same.


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ObjectStoragePath.from_conn method. [airflow]

Posted by "simi (via GitHub)" <gi...@apache.org>.
simi commented on PR #52002:
URL: https://github.com/apache/airflow/pull/52002#issuecomment-3104373668

   let's continue at https://lists.apache.org/thread/qvqozr30d5hkjs3vvyk06n2ko5f3fg7o for now


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ObjectStoragePath.from_conn method. [airflow]

Posted by "Nataneljpwd (via GitHub)" <gi...@apache.org>.
Nataneljpwd commented on PR #52002:
URL: https://github.com/apache/airflow/pull/52002#issuecomment-3095218898

   Looks good now, was probably some kind of issue with  the test, probably the sqlite  DB crashed 


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ObjectStoragePath.from_conn method. [airflow]

Posted by "Nataneljpwd (via GitHub)" <gi...@apache.org>.
Nataneljpwd commented on code in PR #52002:
URL: https://github.com/apache/airflow/pull/52002#discussion_r2184918871


##########
task-sdk/src/airflow/sdk/io/path.py:
##########
@@ -84,6 +87,41 @@ class ObjectStoragePath(CloudPath):
 
     __slots__ = ("_hash_cached",)
 
+    @classmethod
+    def from_conn(cls, conn: Connection) -> ObjectStoragePath:
+        """
+        Construct an ObjectStoragePath from an Airflow Connection.
+
+        This method expects with following keys to be present in its 'extra' JSON configuration:
+
+        - "provider": the scheme or protocol (e.g., "s3", "gs", "file") as known by fsspec
+        - "base_path": the base bucket, container, or directory (e.g., "my-bucket", "tmp/export")
+
+        The final URI is assembled as: <provider>://<base_path>, and passed to ObjectStoragePath.
+
+        :param conn: Airflow Connection object
+        :return: ObjectStoragePath instance
+        :raises ValueError: if the connection type is not `objectstore`

Review Comment:
   Update this comment to reflect the behavior 



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ObjectStoragePath.from_conn method. [airflow]

Posted by "simi (via GitHub)" <gi...@apache.org>.
simi commented on PR #52002:
URL: https://github.com/apache/airflow/pull/52002#issuecomment-3095684217

   @potiuk looking at 3 "random" providers, 
   
   https://github.com/apache/airflow/blob/f896807026c7f75f686309293e72a1140a7a9cf9/providers/microsoft/azure/src/airflow/providers/microsoft/azure/fs/adls.py
   https://github.com/apache/airflow/blob/f896807026c7f75f686309293e72a1140a7a9cf9/providers/google/src/airflow/providers/google/cloud/fs/gcs.py
   https://github.com/apache/airflow/blob/f896807026c7f75f686309293e72a1140a7a9cf9/providers/amazon/src/airflow/providers/amazon/aws/fs/s3.py
   
   They do provide in `filesystem` (acquirable from `get_provider_info`) exposing `get_fs` method and also `schemes` list. Is that something recommended to rely on?


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ObjectStoragePath.from_conn method. [airflow]

Posted by "simi (via GitHub)" <gi...@apache.org>.
simi commented on PR #52002:
URL: https://github.com/apache/airflow/pull/52002#issuecomment-3029396890

   @Nataneljpwd @uranusjr thanks for the review. I did another take on this. Now it is about to create like "root" `ObjectStoragePath` you use later in your dags with Path API. It is similar what's described in OSP docs today, just no need to specify fsspec URI and thus backing connection could be different per environment if passing needed extra. It can be GCP with GCS config, fs with file or AWS with S3 config and DAG will use it with no code change. WDYT?
   
   ```python
   storage = ObjectStoragePath.from_conn(BaseHook.get_connection("storage"))
   output = storage / "my_file.txt"
   
   with output.open("w") as destination:
       destination.write("Hello!")
   ```


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ObjectStoragePath.from_conn method. [airflow]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #52002:
URL: https://github.com/apache/airflow/pull/52002#issuecomment-3260108692

   This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 5 days if no further activity occurs. Thank you for your contributions.


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ObjectStoragePath.from_conn method. [airflow]

Posted by "simi (via GitHub)" <gi...@apache.org>.
simi commented on PR #52002:
URL: https://github.com/apache/airflow/pull/52002#issuecomment-3095559241

   Is it worth to explore what we can get out of https://github.com/apache/airflow/blob/f896807026c7f75f686309293e72a1140a7a9cf9/providers/google/src/airflow/providers/google/get_provider_info.py#L783 and similar entries?


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ObjectStoragePath.from_conn method. [airflow]

Posted by "simi (via GitHub)" <gi...@apache.org>.
simi commented on PR #52002:
URL: https://github.com/apache/airflow/pull/52002#issuecomment-3095917546

   @potiuk I was looking at this again. It is somehow possible to get provider out of connection, lookup for filesystem, import and initialize. But it will initialize `FileSystem` not `ObjectStorage` anyway and I understand this is not intended to be used this way.
   
   ```python
   from airflow.providers_manager import ProvidersManager
   
   pm = ProvidersManager()
   
   def get_filesystems_for_conn_type(conn_type: str) -> list[str] | None:
       for provider_name, provider_info in pm.providers.items():
           for conn_type_info in provider_info.data.get("connection-types", []):
               if conn_type_info.get("connection-type") == conn_type:
                   return provider_info.data.get("filesystems")
   
       return None  # Or raise an error if you want strict behavior
   
   fs = get_filesystems_for_conn_type(BaseHook.get_connection('storage').conn_type)[0]
   get_fs = getattr(__import__(fs[0], fromlist=['get_fs']), 'get_fs')
   get_fs('storage')
   <gcsfs.core.GCSFileSystem object at 0x7efcbe1cec60>
   ```
   
   Looking at connection code, it seems to be originally intended mostly for DB connections (looking at all the comments in the class) and today the meaning changed, but not the class internals. Would it make sense to make it more generic and able to store more structured data for example "filesystem info" for ObjectStorage out of it?


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ObjectStoragePath.from_conn method. [airflow]

Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on PR #52002:
URL: https://github.com/apache/airflow/pull/52002#issuecomment-3095638532

   > I understand current approach is super brittle and provides almost no guarantees. Indeed Connection is too wide abstraction and doesn't currently couple with ObjectStoragePath. I have seen similar brittle coupling in other Airflow parts and considered that would be maybe ok in here also.
   
   I would say "loose" coupling rather than "brittle". "Brittle" suggest instability. "Loose" means that the coupling is deliberately loose. As everything in IT choices are often trade-offs. And "strong coupling" is a choice you can make, "loose coupling" is another. Each of them comes with pros and cons, and "strong coupling" is not always the best - because - for example - it causes unnecessary ripple effects when you make changes that are not applicable to "all entities". This is exactly the same case with DRY -> DRY means strong coupling and while wildely considered as good practice, it's not always the best and you can sometimes (quite often in fact) choose to copy things if close coupling and DRY makes your code unnecessary complex.
   
   In this case - connection providing authentication information mostly is deliberate choice - it's used in many places and many ways so strong coupling it with particular "users", where such coupling is not needed and "noise" for other "users" is something we do not want.
   
    > Is there any best practice on configuring Object Storage initialization? Maybe we can just improve https://airflow.apache.org/docs/apache-airflow/stable/core-concepts/objectstorage.html with best patterns. I'm trying to avoid unnecessary branching as in this pseudo-code.
   
   I would say there are likely several practices you can use and we do not want to limit our users to choose only one approach. I can for example imagine (easily and I know people are doing it) that you are using different connection_id for local and remote (you chose a different pattern). But yes decribing dos on how you could potentially do it, might be a good idea.
   
   So if you are willing to describe few of those patterns and possible usages - feel absolutely free. We do it already in a number of places - for example https://airflow.apache.org/docs/apache-airflow/stable/best-practices.html#handling-conflicting-complex-python-dependencies where we describe various ways you can handle conflicting requirements or https://airflow.apache.org/docs/apache-airflow/stable/best-practices.html#dynamic-dag-generation where we are describing various patterns of dynamic dag generation without falling into "doing too much while parsing" trap.
   
   Feel absolutely free to make PR with such documentation, find the right place where to add it, describe a few variants with examples, that would be cool contribution.
   


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ObjectStoragePath.from_conn method. [airflow]

Posted by "Nataneljpwd (via GitHub)" <gi...@apache.org>.
Nataneljpwd commented on PR #52002:
URL: https://github.com/apache/airflow/pull/52002#issuecomment-3093679758

   @simi , are you still working on the PR? You have 1 failed test that needs to be fixed


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ObjectStoragePath.from_conn method. [airflow]

Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on PR #52002:
URL: https://github.com/apache/airflow/pull/52002#issuecomment-3096296262

   > @potiuk I'm not 100% happy with any current solution and documenting my helper method is not good idea IMHO. It should be decided good pattern and merged or left on side, since it will be questionable why this helper is not part of the codebase if documented. And as mentioned it introduces some requirements on extra columns, which is not standard.
   
   Quite often we do not WANT to decide. When you build product, implementing single opinionated way means that you limit your users from doing things differently.  So we often revert to options, flags, variations, best practices and patterns (often several of them) that our users can choose from - and we do not want to force them. And there is a fine balance and decision making process that usually you get the hang of after several years of delivering product, rather than using the product in single way that is best for "me". This is where my reservations are coming from.
   
   And I understand you are not 100% happy. When you make product it is impossible to make eveyrone 100% happy. You strive to make 80% people 80% happy, and if you achieve that. Your product is likely to succeed (that's how I see it at least). There are many decisions we made that made some people not 100% happy, there are even some that made some people 100% unhappy, and there are even some that made some of us (committers and PMC members) very unhappy where others were somewhat happy but this was the best choice overall.
   
   BTW. Every decision you made on such low-level, and changing some basic models in Airflow will need to be very thoroughly thought through and justified. So while now you are discussing with me - whatever your "100% happiness" solution that you might propose will be -> first thing to do will be to write a '[DISCUSS]` thread on the mailing list (see "community" tab in our docs") and either achieving consenus and sending a "[LAZY CONSENSUS]" thread or  - if consensus cannot be reached - starting `[VOTE]` thread and get it pass with committers and PMC members having binding vote.
   
   So - just so you know - changing the basic behaviour of connection and object storage - requires such community decision, it will not be solved in a discussion in PR. Whatever you see here as my comments are "my comments only" - and whether the rest of the community will agree with it and with your proposal, it's not given.


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ObjectStoragePath.from_conn method. [airflow]

Posted by "Nataneljpwd (via GitHub)" <gi...@apache.org>.
Nataneljpwd commented on code in PR #52002:
URL: https://github.com/apache/airflow/pull/52002#discussion_r2160427041


##########
task-sdk/tests/task_sdk/io/test_path.py:
##########
@@ -182,6 +184,27 @@ def test_bucket_key_protocol(self):
         assert o.protocol == protocol
 
 
+class TestConnPath:
+    def test_connection_inferred_protocol(self):

Review Comment:
   I would add a test on what happens when each or one or both of the required elements are missing 



##########
task-sdk/src/airflow/sdk/io/path.py:
##########
@@ -84,6 +86,33 @@ class ObjectStoragePath(CloudPath):
 
     __slots__ = ("_hash_cached",)
 
+    @classmethod
+    def from_conn(cls, conn: Connection, path: str) -> ObjectStoragePath:
+        """
+        Construct an ObjectStoragePath from an Airflow Connection and a relative path.
+
+        This method assumes the Connection is of type `objectstore` and requires the following
+        keys to be present in its 'extra' JSON configuration:
+
+        - "provider": the scheme or protocol (e.g., "s3", "gs", "file")
+        - "base_path": the base bucket, container, or directory (e.g., "my-bucket", "tmp/export")
+
+        The final URI is assembled as: <provider>://<base_path>/<path>, and passed to ObjectStoragePath.
+
+        :param conn: Airflow Connection object of type `objectstore`
+        :param path: Relative path to append to the base path
+        :return: ObjectStoragePath instance
+        :raises ValueError: if the connection type is not `objectstore`
+        """
+        if conn.conn_type == "objectstore":
+            provider = conn.extra_dejson.get("provider")
+            base_path = conn.extra_dejson.get("base_path")

Review Comment:
   Won't this be troublesome if the values are not set? 
   Is it thrown in url unsplit? I would check it as well if the fields exist and throw a more indicative error than what urlunsplit would throw, to make it easier to debug
   
   I don't think the check exists in the constructor and you may get an object storage type provider without the required fields (including the extra dejson)



##########
task-sdk/src/airflow/sdk/io/path.py:
##########
@@ -84,6 +86,33 @@ class ObjectStoragePath(CloudPath):
 
     __slots__ = ("_hash_cached",)
 
+    @classmethod
+    def from_conn(cls, conn: Connection, path: str) -> ObjectStoragePath:
+        """
+        Construct an ObjectStoragePath from an Airflow Connection and a relative path.
+
+        This method assumes the Connection is of type `objectstore` and requires the following
+        keys to be present in its 'extra' JSON configuration:
+
+        - "provider": the scheme or protocol (e.g., "s3", "gs", "file")
+        - "base_path": the base bucket, container, or directory (e.g., "my-bucket", "tmp/export")
+
+        The final URI is assembled as: <provider>://<base_path>/<path>, and passed to ObjectStoragePath.
+
+        :param conn: Airflow Connection object of type `objectstore`
+        :param path: Relative path to append to the base path
+        :return: ObjectStoragePath instance
+        :raises ValueError: if the connection type is not `objectstore`
+        """
+        if conn.conn_type == "objectstore":

Review Comment:
   Isn't it better to check if it is not an object store and throw when that happens?
   It will reduce nesting in the code and will make it easier to work with



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ObjectStoragePath.from_conn method. [airflow]

Posted by "simi (via GitHub)" <gi...@apache.org>.
simi commented on PR #52002:
URL: https://github.com/apache/airflow/pull/52002#issuecomment-3094435095

   @Nataneljpwd I'm not sure it is related. But I can rebase to restart tests.
   
   ```
   E   sqlalchemy.exc.OperationalError: (MySQLdb.OperationalError) (2013, 'Lost connection to server during query')
   E   [SQL: UPDATE dag_bundle SET last_refreshed=%s WHERE dag_bundle.name = %s]
   E   [parameters: (datetime.datetime(2025, 7, 4, 13, 0, 38, 455528), 'testing')]
   E   (Background on this error at: https://sqlalche.me/e/14/e3q8)
   ```


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ObjectStoragePath.from_conn method. [airflow]

Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on PR #52002:
URL: https://github.com/apache/airflow/pull/52002#issuecomment-3096199766

   > Looking at connection code, it seems to be originally intended mostly for DB connections (looking at all the comments in the class) and today the meaning changed, but not the class internals. Would it make sense to make it more generic and able to store more structured data for example "filesystem info" for ObjectStorage out of it?
   
   No. not really. It was supposed to be generic connection, and actually mostly it was started wiht "http" type of connections. I  do not think we need to update it really there is a need to do more than just documenting possible patterns.
   
   Again - Connection is loosly coupled by design and should be as generic as possible. I do not see particular need to i introducing coupling instead of current "very little" duplication to be honest. It's much better if you can see the whole URL rather than joining it from several separate components, even if it means a little duplication. You could - for example - add addiontal validation, for example when you use different scheme in URL and different in connection, it could error out (but this validation should be possible to disable) if you want to have some "stronger" ties between scheme in connection and the one used in URL.
   
   And again - documenting the patterns that you described where you want to change schemes in prod/dev (either by changing connection id, or definition of connection) is a good idea. There is a good reason why Dags are written in Python because rather than declaratively describe and strongly couple such relations you can "loosely" couple them and add either automated deriving or validation in the Python code of yours - up to you.
   
   Same as Type Hinting in Python - you might want to use it for hinting, autocompletion etc, but you loose flexibility when you want to have it obligatory and erroring out like it is in other languages.


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ObjectStoragePath.from_conn method. [airflow]

Posted by "simi (via GitHub)" <gi...@apache.org>.
simi commented on code in PR #52002:
URL: https://github.com/apache/airflow/pull/52002#discussion_r2185218855


##########
task-sdk/src/airflow/sdk/io/path.py:
##########
@@ -84,6 +87,41 @@ class ObjectStoragePath(CloudPath):
 
     __slots__ = ("_hash_cached",)
 
+    @classmethod
+    def from_conn(cls, conn: Connection) -> ObjectStoragePath:
+        """
+        Construct an ObjectStoragePath from an Airflow Connection.
+
+        This method expects with following keys to be present in its 'extra' JSON configuration:
+
+        - "provider": the scheme or protocol (e.g., "s3", "gs", "file") as known by fsspec
+        - "base_path": the base bucket, container, or directory (e.g., "my-bucket", "tmp/export")
+
+        The final URI is assembled as: <provider>://<base_path>, and passed to ObjectStoragePath.
+
+        :param conn: Airflow Connection object
+        :return: ObjectStoragePath instance
+        :raises ValueError: if the connection type is not `objectstore`

Review Comment:
   updated



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ObjectStoragePath.from_conn method. [airflow]

Posted by "Nataneljpwd (via GitHub)" <gi...@apache.org>.
Nataneljpwd commented on code in PR #52002:
URL: https://github.com/apache/airflow/pull/52002#discussion_r2160759841


##########
task-sdk/src/airflow/sdk/io/path.py:
##########
@@ -84,6 +86,33 @@ class ObjectStoragePath(CloudPath):
 
     __slots__ = ("_hash_cached",)
 
+    @classmethod
+    def from_conn(cls, conn: Connection, path: str) -> ObjectStoragePath:
+        """
+        Construct an ObjectStoragePath from an Airflow Connection and a relative path.
+
+        This method assumes the Connection is of type `objectstore` and requires the following
+        keys to be present in its 'extra' JSON configuration:
+
+        - "provider": the scheme or protocol (e.g., "s3", "gs", "file")
+        - "base_path": the base bucket, container, or directory (e.g., "my-bucket", "tmp/export")
+
+        The final URI is assembled as: <provider>://<base_path>/<path>, and passed to ObjectStoragePath.
+
+        :param conn: Airflow Connection object of type `objectstore`
+        :param path: Relative path to append to the base path
+        :return: ObjectStoragePath instance
+        :raises ValueError: if the connection type is not `objectstore`
+        """
+        if conn.conn_type == "objectstore":

Review Comment:
   Change the check to test whether the conn type  exists in an array of all object storage compatible connection types, i.e, s3 and file Conn types.



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ObjectStoragePath.from_conn method. [airflow]

Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on PR #52002:
URL: https://github.com/apache/airflow/pull/52002#issuecomment-3096307274

   More information on the "Apache" voting process here https://www.apache.org/foundation/voting.html 


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ObjectStoragePath.from_conn method. [airflow]

Posted by "simi (via GitHub)" <gi...@apache.org>.
simi commented on PR #52002:
URL: https://github.com/apache/airflow/pull/52002#issuecomment-3096315699

   Just to make it clear, I'm not pushing for this pattern to be considered best practice. I'm looking for best practice to implement on my side. But I wasn't able to find any so I tried to push for this one.
   
   I agree @potiuk it will be the best to try to collect community experience (if there's any since Object Storage was experimental until 3.0 release) and find the best practice all together. I'll send a message linking to this discussion into mail list.
   
   As usual, thanks for your patience and understanding.


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ObjectStoragePath.from_conn method. [airflow]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] closed pull request #52002: Add ObjectStoragePath.from_conn method.
URL: https://github.com/apache/airflow/pull/52002


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ObjectStoragePath.from_conn method. [airflow]

Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on PR #52002:
URL: https://github.com/apache/airflow/pull/52002#issuecomment-3095734675

   > They do provide `filesystems` (acquirable from `get_provider_info`) exposing `get_fs` method and also `schemes` list. Is that something recommended to rely on?
   
   This is a built in feature of ObjectStorage implementation - it chooses the right implementation of Object storage depending on the URL /schema used. https://airflow.apache.org/docs/apache-airflow/stable/core-concepts/objectstorage.html 
   
   Not sure what do you mean by "rely on", but it's part of the implementation and the way how pluggable implementations of object storage are "provided" by providers.


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ObjectStoragePath.from_conn method. [airflow]

Posted by "simi (via GitHub)" <gi...@apache.org>.
simi commented on PR #52002:
URL: https://github.com/apache/airflow/pull/52002#issuecomment-3095546058

   @potiuk thanks for the review. I do agree on almost everything written. Currently I do host this as "utility" shared method (shared at the end of comment).
   
   > I don't think the pattern you explain is "generic" that "everyone" is using . It's very specific for your case, where you want to use different provider locally and for production. This is somethign your company chose to do, but this is not something that is very generic pattern.
   
   I'm trying to disconnect Airflow environment from DAGs. When `ObjectStoragePath` is acquired (and properly configured), this abstraction is already there and there is (almost) no difference if configured for S3 or AWS.
   
   I understand current approach is super brittle and provides almost no guarantees. Indeed **Connection** is too wide abstraction and doesn't currently couple with `ObjectStoragePath`. I have seen similar brittle coupling in other Airflow parts and considered that would be maybe ok in here also.
   
   Would it make sense to couple **Connection** and **Object Storage** somehow making more simple to check if given connection can initiate **Object Storage** path?
   
   Is there any best practice on configuring Object Storage initialization? Maybe we can just improve https://airflow.apache.org/docs/apache-airflow/stable/core-concepts/objectstorage.html with best patterns. I'm trying to avoid unnecessary branching as in this pseudo-code.
   
   ```
   path = 'my/path'
   ...
   if provider is aws
     base = ObjectStoragePath(f"s3://{path}", conn_id="connection")
   else if provider is gcs
       base = ObjectStoragePath(f"gcs://{path}", conn_id="connection")
   ```
   
   Would it make more sense for example try to extract "protocol" part for `fsspec` out of connection instead like `BaseHook.get_connection("storage").fsspec_protocol` and let user compose `ObjectStoragePath` that way?
   
   :thinking: Per my experience environments differs mostly by "bucket" on ObjectStoragePath (and in some of my cases also by protocol). IMHO it is worth to extract those two out of specified connection.
   
   I was thinking also about to store base path (including protocol) in Variable and the rest in Connection. But this will couple Variable and Connection together I will need to ensure is kept in sync on all environments (aka no guarantees). 
   
   ---
   
   ```python
   from airflow.sdk import ObjectStoragePath
   
   
   # TODO: check https://github.com/apache/airflow/pull/52002
   def path_from_conn(conn) -> "ObjectStoragePath":
       extra = conn.extra_dejson or {}
   
       provider = extra.get("provider")
       base_path = extra.get("base_path")
   
       if not provider or not base_path:
           raise ValueError(
               f"Missing 'provider' or 'base_path' in connection extras for conn_id={conn.conn_id}"
           )
   
       if provider not in fsspec.available_protocols():
           raise ValueError(
               f"Unknown provider '{provider}' for conn_id={conn.conn_id}. "
               f"Known providers: {fsspec.available_protocols()}"
           )
   
       uri = urlunsplit((provider, base_path, "", "", ""))
       return ObjectStoragePath(uri, conn_id=conn.conn_id)
   ```


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ObjectStoragePath.from_conn method. [airflow]

Posted by "simi (via GitHub)" <gi...@apache.org>.
simi commented on PR #52002:
URL: https://github.com/apache/airflow/pull/52002#issuecomment-3096247635

   @potiuk I was looking mainly at those comments.
   
   https://github.com/apache/airflow/blob/0f0ea3b5c444465777a7c5eb5188b301faed1aee/airflow-core/src/airflow/models/connection.py#L99-L103
   
   https://github.com/apache/airflow/blob/0f0ea3b5c444465777a7c5eb5188b301faed1aee/airflow-core/src/airflow/models/connection.py#L254
   
   https://github.com/apache/airflow/blob/0f0ea3b5c444465777a7c5eb5188b301faed1aee/airflow-core/src/airflow/models/connection.py#L469-L471
   
   Looking at connection unification, for some providers crucial fields are "standard".
   
   <img width="1552" height="1196" alt="image" src="https://github.com/user-attachments/assets/51b45d0c-9372-4c95-a5b0-5db7a89b71c1" />
   
   For some providers fields are "extra".
   
   <img width="1552" height="1196" alt="image" src="https://github.com/user-attachments/assets/9fa7c4a1-565f-4203-bd4d-5e131b611744" />
   
   It is quite hard to navigate for me in this.
   
   > And again - documenting the patterns that you described where you want to change schemes in prod/dev (either by changing connection id, or definition of connection) is a good idea. There is a good reason why Dags are written in Python because rather than declaratively describe and strongly couple such relations you can "loosely" couple them and add either automated deriving or validation in the Python code of yours - up to you.
   
   @potiuk I'm not 100% happy with any current solution and documenting my helper method is not good idea IMHO. It should be decided good pattern and merged or left on side, since it will be questionable why this helper is not part of the codebase if documented. And as mentioned it introduces some requirements on extra columns, which is not standard.
   
   IMHO the ship on unifying IO access (ObjectStorage) has sailed already, sooner or later this will need unification also. If Connection is not the best place, maybe new entity like Filesystem connected to Connection and getting more info (like bucket/base_path) could be the way. But that's out of scope of my capabilities to introduce.
   
   Btw. https://airflow.apache.org/docs/apache-airflow/stable/core-concepts/objectstorage.html# mentions.
   
   > Airflow provides a generic abstraction on top of object stores, like s3, gcs, and azure blob storage. This abstraction allows you to use a variety of object storage systems in your dags without having to change your code to deal with every different object storage system.
   
   IMHO it is not 100% true and that's exactly what am I looking for. The last piece is still missing (how to initialize ObjectStorage simply from already configured access in generic way) to be able to use variety of object storage systems without having to change your code.
   


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ObjectStoragePath.from_conn method. [airflow]

Posted by "Nataneljpwd (via GitHub)" <gi...@apache.org>.
Nataneljpwd commented on PR #52002:
URL: https://github.com/apache/airflow/pull/52002#issuecomment-2994998982

   @uranusjr looks like you are right, there are connection types for s3 and local file system storage, perhaps, a good suggestion here should be to check if the connection type is in an array that includes all object storage connection types or all file system compatible storage types


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ObjectStoragePath.from_conn method. [airflow]

Posted by "simi (via GitHub)" <gi...@apache.org>.
simi commented on PR #52002:
URL: https://github.com/apache/airflow/pull/52002#issuecomment-3035992918

   I'll squash if OK now.


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org