mik-laj commented on a change in pull request #17201:
URL: https://github.com/apache/airflow/pull/17201#discussion_r677120851



##########
File path: airflow/providers/alibabacloud/sensors/oss_key.py
##########
@@ -0,0 +1,106 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+import oss2
+from typing import Optional
+from urllib.parse import urlparse
+
+from airflow.exceptions import AirflowException
+from airflow.providers.alibabacloud.hooks.oss import OSSHook
+from airflow.sensors.base import BaseSensorOperator
+
+
+class OSSKeySensor(BaseSensorOperator):
+    """
+    Waits for a key (a file-like instance on OSS) to be present in a OSS 
bucket.
+    OSS being a key/value it does not support folders. The path is just a key
+    a resource.
+
+    :param bucket_key: The key being waited on. Supports full oss:// style url
+        or relative path from root level. When it's specified as a full oss://
+        url, please leave bucket_name as `None`.
+    :type bucket_key: str
+    :param region: OSS region
+    :type region: str
+    :param bucket_name: OSS bucket name
+    :type bucket_name: str
+    :param oss_conn_id: The Airflow connection used for OSS credentials.
+    :type oss_conn_id: Optional[str]
+    :param auth: authentication for Alibaba Cloud user. If not specified 
fetched from connection.
+    :type auth: Optional[oss2.auth.Auth]
+    :param access_id: access key id for Alibaba Cloud user. If not specified 
fetched from connection.
+    :type access_id: Optional[str]
+    :param access_secret: access key secret for Alibaba Cloud user. If not 
specified fetched from connection.
+    :type access_secret: Optional[str]
+    """
+
+    template_fields = ('bucket_key', 'bucket_name')
+
+    def __init__(
+        self,
+        bucket_key: str,
+        region: str,
+        bucket_name: Optional[str] = None,
+        oss_conn_id: Optional[str] = 'oss_default',
+        auth: Optional[oss2.auth.Auth] = None,
+        access_id: Optional[str] = None,
+        access_secret: Optional[str] = None,
+        **kwargs,
+    ):
+        super().__init__(**kwargs)
+
+        self.bucket_name = bucket_name
+        self.bucket_key = bucket_key
+        self.region = region
+        self.oss_conn_id = oss_conn_id
+        self.auth = auth
+        self.access_id = access_id
+        self.access_secret = access_secret
+        self.hook: Optional[OSSHook] = None
+
+    def poke(self, context):
+
+        if self.bucket_name is None:
+            parsed_url = urlparse(self.bucket_key)
+            if parsed_url.netloc == '':
+                raise AirflowException('If key is a relative path from root, 
please provide a bucket_name')
+            self.bucket_name = parsed_url.netloc
+            self.bucket_key = parsed_url.path.lstrip('/')
+        else:
+            parsed_url = urlparse(self.bucket_key)
+            if parsed_url.scheme != '' or parsed_url.netloc != '':
+                raise AirflowException(
+                    'If bucket_name is provided, bucket_key'
+                    + ' should be relative path from root'
+                    + ' level, rather than a full oss:// url'
+                )
+
+        self.log.info('Poking for key : oss://%s/%s', self.bucket_name, 
self.bucket_key)
+        return self.get_hook().object_exists(
+            key=self.bucket_key,
+            bucket_name=self.bucket_name,
+            auth=self.auth,
+            access_id=self.access_id,
+            access_secret=self.access_secret)
+
+    def get_hook(self) -> OSSHook:
+        """Create and return an OSSHook"""
+        if self.hook:

Review comment:
       What do you think about cached_property?




-- 
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: [email protected]

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


Reply via email to