-
-
Save elyse-weiss/6bd367da334268b572aa9951446a3877 to your computer and use it in GitHub Desktop.
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
#Azure stuff | |
import adal #https://adal-python.readthedocs.io/en/latest/ | |
from azure.storage import blob | |
from azure.storage.blob import ContainerPermissions | |
from azure.storage.common import TokenCredential | |
import os | |
from datetime import datetime, timedelta | |
class Azure(object): | |
def __init__(self, client_id, client_secret): | |
self.client_id = client_id | |
self.client_secret = client_secret | |
self.token = None | |
self.GetRefreshAzureToken() | |
def GetRefreshAzureToken(self, authority_url): | |
# authority_url = "https://login.microsoftonline.com/xxxx.com" (Example) | |
""" | |
Roughly based on this example | |
https://github.com/AzureAD/azure-activedirectory-library-for-python/blob/dev/sample/client_credentials_sample.py | |
clientid = service application clientid for tmc_textout_service, a registered service app with read blob permissions selectively granted to Azure Blob Containers | |
clientSecret = the secret for service application tmc_textout_service, expires 10/29/2020 | |
""" | |
if self.token is None or self.token_expire_time < datetime.now() + timedelta(minutes=5): | |
RESOURCE = "https://storage.azure.com/" | |
context = adal.AuthenticationContext(authority_url) | |
self.token = context.acquire_token_with_client_credentials(RESOURCE,self.client_id,self.client_secret) | |
self.token_credential = TokenCredential(self.token["accessToken"]) | |
self.token_expire_time = datetime.strptime(self.token['expiresOn'], '%Y-%m-%d %H:%M:%S.%f') | |
#Refresh our dependencies with new tokens | |
self.GetBlockBlobService() | |
return None | |
def GetBlockBlobService(self, account_name='textoutrpt', account_key=None, sas_token=None, is_emulated=False, protocol='https', endpoint_suffix='core.windows.net', custom_domain=None, request_session=None, connection_string=None, socket_timeout=None, token_credential=None): | |
self.GetRefreshAzureToken() | |
self.block_blob_service = blob.BlockBlobService(account_name=account_name, account_key=account_key, sas_token=sas_token, is_emulated=is_emulated, protocol=protocol, endpoint_suffix=endpoint_suffix, custom_domain=custom_domain, request_session=request_session, connection_string=connection_string, socket_timeout=socket_timeout, token_credential=self.token_credential) | |
return None | |
def GetBlobToPath(self, container_name, blob_name, file_path, open_mode='wb', snapshot=None, start_range=None, end_range=None, validate_content=False, progress_callback=None, max_connections=2, lease_id=None, if_modified_since=None, if_unmodified_since=None, if_match=None, if_none_match=None, timeout=None): | |
self.GetRefreshAzureToken() | |
self.block_blob_service.get_blob_to_path(container_name=container_name, blob_name=blob_name, file_path=file_path, open_mode=open_mode, snapshot=snapshot, start_range=start_range, end_range=end_range, validate_content=validate_content, progress_callback=progress_callback, max_connections=max_connections, lease_id=lease_id, if_modified_since=if_modified_since, if_unmodified_since=if_unmodified_since, if_match=if_match, if_none_match=if_none_match, timeout=timeout) | |
return None | |
def ListBlobs(self, container_name, prefix=None, num_results=None, include=None, delimiter=None, marker=None, timeout=None): | |
self.GetRefreshAzureToken() | |
blobs = self.block_blob_service.list_blobs(container_name=container_name, prefix=prefix, num_results=num_results, include=include, delimiter=delimiter, marker=marker, timeout=timeout) | |
return blobs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment