From commits-return-20712-archive-asf-public=cust-asf.ponee.io@airflow.incubator.apache.org Mon Sep 3 16:13:44 2018 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id 0FD1B180663 for ; Mon, 3 Sep 2018 16:13:43 +0200 (CEST) Received: (qmail 71514 invoked by uid 500); 3 Sep 2018 14:13:43 -0000 Mailing-List: contact commits-help@airflow.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@airflow.incubator.apache.org Delivered-To: mailing list commits@airflow.incubator.apache.org Received: (qmail 71505 invoked by uid 99); 3 Sep 2018 14:13:43 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 03 Sep 2018 14:13:43 +0000 From: GitBox To: commits@airflow.apache.org Subject: [GitHub] ashb commented on a change in pull request #3823: [AIRFLOW-2985] An operator for S3 object copying/deleting Message-ID: <153598402266.21997.9853156762562984284.gitbox@gitbox.apache.org> Date: Mon, 03 Sep 2018 14:13:42 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit ashb commented on a change in pull request #3823: [AIRFLOW-2985] An operator for S3 object copying/deleting URL: https://github.com/apache/incubator-airflow/pull/3823#discussion_r214702197 ########## File path: airflow/contrib/operators/s3_copy_object_operator.py ########## @@ -0,0 +1,129 @@ +# -*- coding: utf-8 -*- +# +# 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. + +from pprint import pformat +from urllib.parse import urlparse + +from airflow.exceptions import AirflowException +from airflow.hooks.S3_hook import S3Hook +from airflow.models import BaseOperator +from airflow.utils.decorators import apply_defaults + + +class S3CopyObjectOperator(BaseOperator): + """ + Creates a copy of an object that is already stored in S3. + + Note: the S3 connection used here needs to have access to both + source and destination bucket/key. + + :param dest_bucket_key: The key of the object to copy to. + It can be either full s3:// style url or ralative path from root level. + When it's specified as a full s3:// url, please omit dest_bucket_name. + :type dest_bucket_key: str + :param source_bucket_key: The key of the source object. + The convention to specify source_bucket_key is the same as dest_bucket_key. + :type source_bucket_key: str + :param dest_bucket_name: Name of the S3 bucket to where the object is copied. + It should be omitted when dest_bucket_key is provided as a full s3:// url. + :type dest_bucket_name: str + :param source_bucket_name: Name of the S3 bucket where the source object is in. + It should be omitted when source_bucket_key is provided as a full s3:// url. + :type source_bucket_name: str + :param source_version_id: Version ID of the source object (OPTIONAL) + :type source_version_id: str + :param s3_conn_id: Connection id of the S3 connection to use + :type s3_conn_id: str + :parame verify: Whether or not to verify SSL certificates for S3 connetion. + By default SSL certificates are verified. + You can provide the following values: + - False: do not validate SSL certificates. SSL will still be used, + but SSL certificates will not be + verified. + - path/to/cert/bundle.pem: A filename of the CA cert bundle to uses. + You can specify this argument if you want to use a different + CA cert bundle than the one used by botocore. + This is also applicable to ``dest_verify``. + :type verify: bool or str + """ + + @apply_defaults + def __init__( + self, + dest_bucket_key, + source_bucket_key, + dest_bucket_name=None, + source_bucket_name=None, + source_version_id=None, + s3_conn_id='aws_default', + verify=None, + *args, **kwargs): + super(S3CopyObjectOperator, self).__init__(*args, **kwargs) + + if dest_bucket_name is None: + parsed_url = urlparse(dest_bucket_key) + if parsed_url.netloc == '': + raise AirflowException('Please provide dest_bucket_name') + else: + dest_bucket_name = parsed_url.netloc + if parsed_url.path[0] == '/': + dest_bucket_key = parsed_url.path[1:] + else: + dest_bucket_key = parsed_url.path + else: + parsed_url = urlparse(dest_bucket_key) + if parsed_url.scheme != '' or parsed_url.netloc != '': + raise AirflowException('If dest_bucket_name is provided, ' + + 'dest_bucket_key should be relative path ' + + 'from root level, rather than a full s3:// url') + + if source_bucket_name is None: + parsed_url = urlparse(source_bucket_key) + if parsed_url.netloc == '': + raise AirflowException('Please provide source_bucket_name') + else: + source_bucket_name = parsed_url.netloc + if parsed_url.path[0] == '/': + source_bucket_key = parsed_url.path[1:] + else: + source_bucket_key = parsed_url.path + else: + parsed_url = urlparse(source_bucket_key) + if parsed_url.scheme != '' or parsed_url.netloc != '': + raise AirflowException('If source_bucket_name is provided, ' + + 'source_bucket_key should be relative path ' + + 'from root level, rather than a full s3:// url') + + self.dest_bucket_key = dest_bucket_key + self.source_bucket_key = source_bucket_key + self.dest_bucket_name = dest_bucket_name + self.source_bucket_name = source_bucket_name + self.source_version_id = source_version_id + self.s3_conn_id = s3_conn_id + self.verify = verify + + def execute(self, context): + s3_hook = S3Hook(aws_conn_id=self.s3_conn_id, verify=self.verify) + CopySource = {'Bucket': self.source_bucket_name, + 'Key': self.source_bucket_key, + 'VersionId': self.source_version_id} + response = s3_hook.get_conn().copy_object(Bucket=self.dest_bucket_name, + Key=self.dest_bucket_key, + CopySource=CopySource) Review comment: Yes, logic in S3 hook, two operators. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: users@infra.apache.org With regards, Apache Git Services