Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moves
AzureBlobStorageToGCSOperator
from Azure to Google provider (#…
…32306) * moved AzureBlobStorageToGCSOperator to google provider
- Loading branch information
Showing
10 changed files
with
211 additions
and
168 deletions.
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
airflow/providers/google/cloud/transfers/azure_blob_to_gcs.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
# | ||
# 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 __future__ import annotations | ||
|
||
import tempfile | ||
from typing import TYPE_CHECKING, Sequence | ||
|
||
from airflow.models import BaseOperator | ||
from airflow.providers.google.cloud.hooks.gcs import GCSHook | ||
from airflow.providers.microsoft.azure.hooks.wasb import WasbHook | ||
|
||
if TYPE_CHECKING: | ||
from airflow.utils.context import Context | ||
|
||
|
||
class AzureBlobStorageToGCSOperator(BaseOperator): | ||
""" | ||
Operator transfers data from Azure Blob Storage to specified bucket in Google Cloud Storage. | ||
.. seealso:: | ||
For more information on how to use this operator, take a look at the guide: | ||
:ref:`howto/operator:AzureBlobStorageToGCSOperator` | ||
:param wasb_conn_id: Reference to the wasb connection. | ||
:param gcp_conn_id: The connection ID to use when fetching connection info. | ||
:param blob_name: Name of the blob | ||
:param container_name: Name of the container | ||
:param bucket_name: The bucket to upload to | ||
:param object_name: The object name to set when uploading the file | ||
:param filename: The local file path to the file to be uploaded | ||
:param gzip: Option to compress local file or file data for upload | ||
:param impersonation_chain: Optional service account to impersonate using short-term | ||
credentials, or chained list of accounts required to get the access_token | ||
of the last account in the list, which will be impersonated in the request. | ||
If set as a string, the account must grant the originating account | ||
the Service Account Token Creator IAM role. | ||
If set as a sequence, the identities from the list must grant | ||
Service Account Token Creator IAM role to the directly preceding identity, with first | ||
account from the list granting this role to the originating account. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
*, | ||
wasb_conn_id="wasb_default", | ||
gcp_conn_id: str = "google_cloud_default", | ||
blob_name: str, | ||
container_name: str, | ||
bucket_name: str, | ||
object_name: str, | ||
filename: str, | ||
gzip: bool, | ||
impersonation_chain: str | Sequence[str] | None = None, | ||
**kwargs, | ||
) -> None: | ||
super().__init__(**kwargs) | ||
self.wasb_conn_id = wasb_conn_id | ||
self.gcp_conn_id = gcp_conn_id | ||
self.blob_name = blob_name | ||
self.container_name = container_name | ||
self.bucket_name = bucket_name | ||
self.object_name = object_name | ||
self.filename = filename | ||
self.gzip = gzip | ||
self.impersonation_chain = impersonation_chain | ||
|
||
template_fields: Sequence[str] = ( | ||
"blob_name", | ||
"container_name", | ||
"bucket_name", | ||
"object_name", | ||
"filename", | ||
) | ||
|
||
def execute(self, context: Context) -> str: | ||
azure_hook = WasbHook(wasb_conn_id=self.wasb_conn_id) | ||
gcs_hook = GCSHook( | ||
gcp_conn_id=self.gcp_conn_id, | ||
impersonation_chain=self.impersonation_chain, | ||
) | ||
|
||
with tempfile.NamedTemporaryFile() as temp_file: | ||
self.log.info("Downloading data from blob: %s", self.blob_name) | ||
azure_hook.get_file( | ||
file_path=temp_file.name, | ||
container_name=self.container_name, | ||
blob_name=self.blob_name, | ||
) | ||
self.log.info( | ||
"Uploading data from blob's: %s into GCP bucket: %s", self.object_name, self.bucket_name | ||
) | ||
gcs_hook.upload( | ||
bucket_name=self.bucket_name, | ||
object_name=self.object_name, | ||
filename=temp_file.name, | ||
gzip=self.gzip, | ||
) | ||
self.log.info( | ||
"Resources have been uploaded from blob: %s to GCS bucket:%s", | ||
self.blob_name, | ||
self.bucket_name, | ||
) | ||
return f"gs://{self.bucket_name}/{self.object_name}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
docs/apache-airflow-providers-google/operators/transfer/azure_blob_to_gcs.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
|
||
.. 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. | ||
Azure Blob Storage to Google Cloud Storage (GCS) Transfer Operator | ||
================================================================== | ||
The `Google Cloud Storage <https://cloud.google.com/storage/>`__ (GCS) is used to store large data from various applications. | ||
This is also the same with `Azure Blob Storage <https://docs.microsoft.com/en-us/rest/api/storageservices/blob-service-rest-api>`__. | ||
This page shows how to transfer data from Azure Blob Storage to GCS. | ||
|
||
Prerequisite Tasks | ||
^^^^^^^^^^^^^^^^^^ | ||
|
||
.. include:: ../_partials/prerequisite_tasks.rst | ||
|
||
.. _howto/operator:AzureBlobStorageToGCSOperator: | ||
|
||
Transfer Data from Azure Blob Storage to Google Cloud Storage | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
Operator transfers data from Azure Blob Storage to specified bucket in Google Cloud Storage | ||
|
||
Use the :class:`~airflow.providers.google.cloud.transfers.azure_blob_to_gcs.AzureBlobStorageToGCSOperator` | ||
to transfer data from Azure Blob Storage to Google Cloud Storage. | ||
|
||
Example usage: | ||
|
||
.. exampleinclude:: /../../tests/system/providers/google/cloud/azure/example_azure_blob_to_gcs.py | ||
:language: python | ||
:start-after: [START how_to_azure_blob_to_gcs] | ||
:end-before: [END how_to_azure_blob_to_gcs] | ||
|
||
Reference | ||
^^^^^^^^^ | ||
|
||
For further information, look at: | ||
|
||
* `GCS Client Library Documentation <https://googleapis.dev/python/storage/latest/index.html>`__ | ||
* `GCS Product Documentation <https://cloud.google.com/storage/docs/>`__ | ||
* `Azure Blob Storage Client Library Documentation <https://learn.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-python>`__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
connections/index.rst connections/azure.rst | ||
secrets-backends/index.rst secrets-backends/azure-key-vault-secrets-backend.rst | ||
logging.rst logging/index.rst | ||
transfer/azure_blob_to_gcs.rst ../../apache-airflow-providers-google/latest/operators/transfer/azure_blob_to_gcs.rst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.