Skip to content

Commit

Permalink
Update Azure fileshare hook to use azure-storage-file-share instead o…
Browse files Browse the repository at this point in the history
…f azure-storage-file (#33904)

* Update Azure fileshare hook to use azure-storage-file-share instead of azure-storage-file
  • Loading branch information
pankajastro committed Sep 2, 2023
1 parent caf135f commit b7f84e9
Show file tree
Hide file tree
Showing 9 changed files with 271 additions and 370 deletions.
34 changes: 24 additions & 10 deletions airflow/providers/google/cloud/transfers/azure_fileshare_to_gcs.py
Expand Up @@ -17,10 +17,12 @@
# under the License.
from __future__ import annotations

import warnings
from tempfile import NamedTemporaryFile
from typing import TYPE_CHECKING, Sequence

from airflow import AirflowException
from airflow.exceptions import AirflowProviderDeprecationWarning
from airflow.models import BaseOperator
from airflow.providers.google.cloud.hooks.gcs import GCSHook, _parse_gcs_url, gcs_object_is_directory
from airflow.providers.microsoft.azure.hooks.fileshare import AzureFileShareHook
Expand Down Expand Up @@ -73,6 +75,7 @@ def __init__(
share_name: str,
dest_gcs: str,
directory_name: str | None = None,
directory_path: str | None = None,
prefix: str = "",
azure_fileshare_conn_id: str = "azure_fileshare_default",
gcp_conn_id: str = "google_cloud_default",
Expand All @@ -84,7 +87,15 @@ def __init__(
super().__init__(**kwargs)

self.share_name = share_name
self.directory_path = directory_path
self.directory_name = directory_name
if self.directory_path is None:
self.directory_path = directory_name
warnings.warn(
"Use 'directory_path' instead of 'directory_name'.",
AirflowProviderDeprecationWarning,
stacklevel=2,
)
self.prefix = prefix
self.azure_fileshare_conn_id = azure_fileshare_conn_id
self.gcp_conn_id = gcp_conn_id
Expand All @@ -106,10 +117,12 @@ def _check_inputs(self) -> None:

def execute(self, context: Context):
self._check_inputs()
azure_fileshare_hook = AzureFileShareHook(self.azure_fileshare_conn_id)
files = azure_fileshare_hook.list_files(
share_name=self.share_name, directory_name=self.directory_name
azure_fileshare_hook = AzureFileShareHook(
share_name=self.share_name,
azure_fileshare_conn_id=self.azure_fileshare_conn_id,
directory_path=self.directory_path,
)
files = azure_fileshare_hook.list_files()

gcs_hook = GCSHook(
gcp_conn_id=self.gcp_conn_id,
Expand Down Expand Up @@ -141,16 +154,17 @@ def execute(self, context: Context):

if files:
self.log.info("%s files are going to be synced.", len(files))
if self.directory_name is None:
if self.directory_path is None:
raise RuntimeError("The directory_name must be set!.")
for file in files:
azure_fileshare_hook = AzureFileShareHook(
share_name=self.share_name,
azure_fileshare_conn_id=self.azure_fileshare_conn_id,
directory_path=self.directory_path,
file_path=file,
)
with NamedTemporaryFile() as temp_file:
azure_fileshare_hook.get_file_to_stream(
stream=temp_file,
share_name=self.share_name,
directory_name=self.directory_name,
file_name=file,
)
azure_fileshare_hook.get_file_to_stream(stream=temp_file)
temp_file.flush()

# There will always be a '/' before file because it is
Expand Down
23 changes: 23 additions & 0 deletions airflow/providers/microsoft/azure/CHANGELOG.rst
Expand Up @@ -27,6 +27,29 @@
Changelog
---------

7.0.0
.....

Breaking changes
~~~~~~~~~~~~~~~~

.. warning::
In this version of the provider, we have changed AzureFileShareHook to use azure-storage-file-share library instead
of azure-storage-file this change has impact on existing hook method see below for details, removed deprecated
extra__azure_fileshare__ prefix from connection extras param and removed protocol param from connection extras

* get_conn from AzureFileShareHook return None instead FileService
* Remove protocol param from Azure fileshare connection extras
* Remove deprecated extra__azure_fileshare__ prefix from Azure fileshare connection extras, list_files
* Remove share_name, directory_name param from AzureFileShareHook method check_for_directory,
list_directories_and_files, create_directory in favor of AzureFileShareHook share_name and directory_path param
* AzureFileShareHook method create_share and delete_share accept kwargs from ShareServiceClient.create_share
and ShareServiceClient.delete_share
* Remove share_name, directory_name, file_name param from AzureFileShareHook method get_file, get_file_to_stream
and load_file in favor of AzureFileShareHook share_name and file_path
* Remove AzureFileShareHook.check_for_file method
* Remove AzureFileShareHook.load_string, AzureFileShareHook.load_stream in favor of AzureFileShareHook.load_data

6.3.0
.....

Expand Down

0 comments on commit b7f84e9

Please sign in to comment.