11import logging
22import urllib .parse
3- from azure .storage .blob import BlobServiceClient
3+ from azure .storage .blob import BlobServiceClient , generate_blob_sas , BlobSasPermissions
44from . import azure_file_entity , azure_storage_container
55from ...abstract import storage_client
66from ....resource_errors import ExceptionHandler
7-
7+ import datetime
88
99class AzureStorageClient (storage_client .StorageClient ):
10+ """
11+ Represents a client for interacting with Azure Storage.
12+ """
13+
1014 _blob_service_client : BlobServiceClient
1115
1216 def __init__ (self , config ):
@@ -16,6 +20,15 @@ def __init__(self, config):
1620
1721 @ExceptionHandler .decorated
1822 def get_container (self , container_name : str ):
23+ """
24+ Retrieves the Azure Storage container with the specified name.
25+
26+ Args:
27+ container_name (str): The name of the container.
28+
29+ Returns:
30+ azure_storage_container.AzureStorageContainer: The Azure Storage container object.
31+ """
1932 if container_name :
2033 client_container = self ._blob_service_client .get_container_client (container_name )
2134 return azure_storage_container .AzureStorageContainer (container_name , client_container )
@@ -25,12 +38,32 @@ def get_container(self, container_name: str):
2538
2639 @ExceptionHandler .decorated
2740 def get_file (self , container_name : str , file_name : str ):
41+ """
42+ Retrieves the Azure Storage file with the specified name from the specified container.
43+
44+ Args:
45+ container_name (str): The name of the container.
46+ file_name (str): The name of the file.
47+
48+ Returns:
49+ azure_file_entity.AzureFileEntity: The Azure Storage file object.
50+ """
2851 client_container = self ._blob_service_client .get_container_client (container_name )
2952 blob_client = client_container .get_blob_client (file_name )
3053 return azure_file_entity .AzureFileEntity (file_name , blob_client )
3154
3255 @ExceptionHandler .decorated
3356 def get_file_from_url (self , container_name : str , full_url : str ):
57+ """
58+ Retrieves the Azure Storage file from the specified URL.
59+
60+ Args:
61+ container_name (str): The name of the container.
62+ full_url (str): The full URL of the file.
63+
64+ Returns:
65+ azure_file_entity.AzureFileEntity: The Azure Storage file object.
66+ """
3467 path = '/' .join (urllib .parse .unquote (full_url ).split ('/' )[4 :])
3568 file = azure_file_entity .AzureFileEntity
3669 client_container = self ._blob_service_client .get_container_client (container_name )
@@ -40,3 +73,27 @@ def get_file_from_url(self, container_name: str, full_url: str):
4073 if file_info .file_path == path :
4174 file = file_info
4275 return file
76+
77+
78+ def get_sas_url (self , container_name : str , file_path : str , expiry_hours :int = 12 ) -> str :
79+ """
80+ Generates a Shared Access Signature (SAS) URL for the specified Azure Storage file.
81+
82+ Args:
83+ container_name (str): The name of the container.
84+ file_path (str): The path of the file within the container.
85+ expiry_hours (int, optional): The number of hours until the SAS URL expires. Defaults to 12.
86+
87+ Returns:
88+ str: The SAS URL for the file.
89+ """
90+ sas_token = generate_blob_sas (
91+ self ._blob_service_client .account_name ,
92+ container_name ,
93+ file_path ,
94+ account_key = self ._blob_service_client .credential .account_key ,
95+ permission = BlobSasPermissions (read = True ),
96+ expiry = datetime .datetime .utcnow () + datetime .timedelta (hours = expiry_hours )
97+ )
98+ sas_url = f"https://{ self ._blob_service_client .account_name } .blob.core.windows.net/{ container_name } /{ file_path } ?{ sas_token } "
99+ return sas_url
0 commit comments