AzureContainerClient
A client for interacting with single Azure Blob Storage Container.
Inherits from:
BaseContainerClient: to provide a generic interface for object storage clientsAzureClient: to provide an interface for Azure Python SDK
Parameters:
container_sas_url(str): SAS URL to an Azure Blob Storage Container. This url will define the specific container that the client has access. Respecting the permission set up in the SAS Token.
__init__
Initialize an instance of AzureContainerClient.
Parameters
container_sas_url(str): SAS URL to an Azure Blob Storage Container.
delete
Deletes the container specified by the SAS URL.
Parameters
None
Returns
Dict[str, Any]: A dictionary containing deleted container information.
Tip
You can check here an example
Raises:
ClientAuthenticationError: Raised when authentication with Azure fails.ResourceNotFoundError: Raised when the requested resource is not found.HttpResponseError: Raised when an error occurs in the HTTP response.Exception: Raised for any unexpected errors.
upload_file
Uploads a file to a blob in the specified container.
Parameters
key(str): The name of the blob.file_path(str): The path to the file to upload.chunk_size(Optional[int], optional): The size of each chunk to upload. Defaults to 4 * 1024 * 1024.progress_callback(Optional[Callable[[str, int, int], Awaitable[Any]]], optional): A callback function to track the progress of the upload. Defaults to None.
Returns
Dict[str, Any]: A dictionary containing the response information.
Tip
You can check here an example
Raises:
ClientAuthenticationError: Raised when authentication with Azure fails.ResourceNotFoundError: Raised when the requested resource is not found.HttpResponseError: Raised when an error occurs in the HTTP response.Exception: Raised for any unexpected errors.
upload_file_from_bytes
Uploads a file to a blob in the specified container from byte stream.
Parameters
key(str): The name of the blob.buffer(BytesIO): the byte stream of the file.chunk_size(Optional[int], optional): The size of each chunk to upload. Defaults to 4 * 1024 * 1024.progress_callback(Optional[Callable[[str, int, int], Awaitable[Any]]], optional): A callback function to track the progress of the upload. Defaults to None.
Returns
Dict[str, Any]: A dictionary containing the response information.
Tip
You can check here an example
Raises:
ClientAuthenticationError: Raised when authentication with Azure fails.ResourceNotFoundError: Raised when the requested resource is not found.HttpResponseError: Raised when an error occurs in the HTTP response.Exception: Raised for any unexpected errors.
resume_upload
Uploads a file to an Azure Blob Storage container, resuming an interrupted upload if there is an uncommitted block list.
Parameters
key(str): The name of the blob.file_path(str): The path to the file to upload.chunk_size(int, optional): The size of each chunk to upload. Defaults to 4 * 1024 * 1024.progress_callback(Optional[Callable[[str, int, int], Awaitable[Any]]], optional): A callback function to track the progress of the upload. Defaults to None.
Returns
None
Tip
You can check here an example
Raises:
ClientAuthenticationError: Raised when authentication with Azure fails.ResourceNotFoundError: Raised when the requested resource is not found.HttpResponseError: Raised when an error occurs in the HTTP response.Exception: Raised for any unexpected errors.
download_file_to_bytes
Download a file from the specified container and return its content as bytes.
Parameters
key(str): The name of the blob.chunk_size(Optional[int], optional): The size of each chunk to download. Defaults to 4 * 1024 * 1024.progress_callback(Optional[Callable[[str, int, int], Awaitable[Any]]], optional): A callback function to track the progress of the download. Defaults to None.
Returns
bytes: The content of the downloaded file as bytes.
Tip
You can check here an example
Raises:
ClientAuthenticationError: Raised when authentication with Azure fails.ResourceNotFoundError: Raised when the requested resource is not found.HttpResponseError: Raised when an error occurs in the HTTP response.Exception: Raised for any unexpected errors.
download_file
Download a file from the specified container and save it to the specified file path.
Parameters
key(str): The name of the blob.file_path(str): The path where the file will be saved.chunk_size(Optional[int], optional): The size of each chunk to download. Defaults to 4 * 1024 * 1024.progress_callback(Optional[Callable[[str, int, int], Awaitable[Any]]], optional): A callback function to track the progress of the download. Defaults to None.
Returns
None
Tip
You can check here an example
Raises:
ClientAuthenticationError: Raised when authentication with Azure fails.ResourceNotFoundError: Raised when the requested resource is not found.HttpResponseError: Raised when an error occurs in the HTTP response.Exception: Raised for any unexpected errors.
copy_file_from_url
Copies a file from a URL to a blob in the specified container.
Parameters
key(str): The name of the destination blob.source_url(str): The URL of the source file to copy.
Returns
str: The URL of the destination blob.
Tip
You can check here an example
Raises:
ClientAuthenticationError: Raised when authentication with Azure fails.ResourceNotFoundError: Raised when the requested resource is not found.HttpResponseError: Raised when an error occurs in the HTTP response.Exception: Raised for any unexpected errors.
delete_file
Deletes a file with all its snapshots from the specified container.
Parameters
key(str): The name of the blob to delete.
Returns
None
Tip
You can check here an example
Raises:
ClientAuthenticationError: Raised when authentication with Azure fails.ResourceNotFoundError: Raised when the requested resource is not found.HttpResponseError: Raised when an error occurs in the HTTP response.Exception: Raised for any unexpected errors.
batch_delete_files
Deletes a list of files with all their snapshots using the Azure Blob Batch API (up to 256 blobs per request). Batches are sent one at a time. Blobs that are already gone count as deleted. Blobs that fail with a transient error (throttling 429 or server error 5xx) and incomplete or malformed batch responses are retried, for up to 3 attempts in total with exponential backoff (1 second, then 2 seconds). Failures of the batch request itself are left to the SDK's retry policy; if they persist, the whole batch is returned as failed. Other failures, such as 403 or 409 for a single blob, are not retried. The keys that could not be deleted are returned in input order instead of raising. The container SAS needs Write and Delete permissions.
Parameters
keys(List[str]): The names of the blobs to delete.batch_size(int): The number of blobs per batch request, between 1 and 256. Defaults to 256.
Returns
List[str]: The keys that could not be deleted. Empty when everything was deleted.
Raises:
ValueError: Raised whenbatch_sizeis not between 1 and 256.ClientAuthenticationError: Raised when the batch request fails authentication (AuthenticationFailedorInvalidAuthenticationInfo, e.g. an invalid or expired SAS).ResourceNotFoundError: Raised when the batch request fails withContainerNotFound.HttpResponseError: Raised for any other 401/403 on the batch request, such asAuthorizationFailureorAuthorizationPermissionMismatch(e.g. missing SAS permissions).
get_file_url
Returns the URL that can be used to access the specified file in specified container.
Parameters
key(str): The name of the blob.
Returns
str: The URL of the file.
Tip
You can check here an example
Raises:
ClientAuthenticationError: Raised when authentication with Azure fails.ResourceNotFoundError: Raised when the requested resource is not found.HttpResponseError: Raised when an error occurs in the HTTP response.Exception: Raised for any unexpected errors.
get_file_properties
Retrieves the properties of a blob in the specified container.
Parameters
key(str): The name of the blob.
Returns
BlobProperties: The properties of the blob.
Tip
You can check here an example
Raises:
ClientAuthenticationError: Raised when authentication with Azure fails.ResourceNotFoundError: Raised when the requested resource is not found.HttpResponseError: Raised when an error occurs in the HTTP response.Exception: Raised for any unexpected errors.
commit_file
Commits the uncommitted blocks of the blob.
This method commits the uncommitted blocks of the blob using the specified blob SAS URL plus key .
Parameters
key(str): The name of the blob to commit.
Returns
None
Tip
You can check here an example
Raises:
ClientAuthenticationError: Raised when authentication with Azure fails.ResourceNotFoundError: Raised when the requested resource is not found.HttpResponseError: Raised when an error occurs in the HTTP response.Exception: Raised for any unexpected errors.
get_file_chunks
list the information of committed and uncommitted blocks of the blob.
This method lists the committed and uncommitted blocks of the blob using the specified blob SAS URL plus key .
Parameters
key(str): The name of the blob to retrieve the chunks.
Returns
Tuple[List[BlobBlock], List[BlobBlock]]: the return will contain two lists ofBlobBlockobjects, one for committed blocks and one for uncommitted blocks.
Tip
You can check here an example
Raises:
ClientAuthenticationError: Raised when authentication with Azure fails.ResourceNotFoundError: Raised when the requested resource is not found.HttpResponseError: Raised when an error occurs in the HTTP response.Exception: Raised for any unexpected errors.
is_exists
Checks if container in the context exists.
Parameters
None
Returns
bool: True when the container exists and False in case it doesn't
Tip
You can check here an example
Raises:
ClientAuthenticationError: Raised when authentication with Azure fails.ResourceNotFoundError: Raised when the requested resource is not found.HttpResponseError: Raised when an error occurs in the HTTP response.Exception: Raised for any unexpected errors.
is_file_exists
Checks if blob exists in the context container.
Parameters
None
Returns
bool: True when the blob exists and False in case it doesn't
Tip
You can check here an example
Raises:
ClientAuthenticationError: Raised when authentication with Azure fails.ResourceNotFoundError: Raised when the requested resource is not found.HttpResponseError: Raised when an error occurs in the HTTP response.Exception: Raised for any unexpected errors.