Commit 46812582 authored by Louis Baetens's avatar Louis Baetens
Browse files

Add the retrieval of the SAS for a blob

parent 6b2b8089
...@@ -17,6 +17,8 @@ from azure.storage.blob import ( ...@@ -17,6 +17,8 @@ from azure.storage.blob import (
AccountSasPermissions, AccountSasPermissions,
ContainerClient, ContainerClient,
BlobType, BlobType,
generate_blob_sas,
BlobSasPermissions,
) )
from azure.core.exceptions import ClientAuthenticationError from azure.core.exceptions import ClientAuthenticationError
...@@ -146,3 +148,41 @@ class AzureBlobClient: ...@@ -146,3 +148,41 @@ class AzureBlobClient:
:type blob_name: str :type blob_name: str
""" """
self.blob_container_client(container_name=container_name).delete_blob(blob=blob_name) self.blob_container_client(container_name=container_name).delete_blob(blob=blob_name)
def get_blob_sas_url(self, container_name, blob_name, time_to_expiry=1):
"""
Get the URL of a blob, with the necessary read access
Args:
container_name (str): name of the caontainer
blob_name (str): name (path) of the blob in the container
time_to_expiry (int): in hours, the time of validity for this access
Returns:
(str): the authorized URL
"""
sas_blob = self.get_blob_sas(container_name=container_name, blob_name=blob_name, time_to_expiry=time_to_expiry)
authorized_url = self.account_url + '/' + container_name + '/' + blob_name + '?' + sas_blob
return authorized_url
def get_blob_sas(self, container_name, blob_name, time_to_expiry=1):
"""
Generate a blob SAS, to be able to access it from a standard URL
Args:
container_name (str): name of the caontainer
blob_name (str): name (path) of the blob in the container
time_to_expiry (int): in hours, the time of validity for this access
Returns:
(str): the SAS of the blob
"""
sas_blob = generate_blob_sas(
account_name=self.storage_account,
account_key=self.credentials,
container_name=container_name,
blob_name=blob_name,
permission=BlobSasPermissions(read=True),
expiry=datetime.utcnow() + timedelta(hours=time_to_expiry))
return sas_blob
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment