def run_all_samples(self, connection_string): print('Azure Storage File Advanced samples - Starting.') try: # Create an instance of ShareServiceClient service = ShareServiceClient.from_connection_string( conn_str=connection_string) # List shares print('\n\n* List shares *\n') self.list_shares(service) # Set Cors print('\n\n* Set cors rules *\n') self.set_cors_rules(service) # Set Service Properties print('\n\n* Set service properties *\n') self.set_service_properties(service) # Share, directory and file properties and metadata print('\n\n* Metadata and properties *\n') self.metadata_and_properties(service) except Exception as e: print('Error occurred in the sample.', e) finally: print('\nAzure Storage File Advanced samples - Completed.\n')
def get_share_client(self): # [START get_share_client] from azure.storage.fileshare import ShareServiceClient file_service = ShareServiceClient.from_connection_string(self.connection_string) # Get a share client to interact with a specific share share = file_service.get_share_client("fileshare2")
def __init__(self): self.conn_str = settings.FILES_CONN_STRING self.share = f"{settings.ENVIRONMENT}-{settings.FILES_SHARE}" self.client = ShareServiceClient.from_connection_string(self.conn_str) try: self.share_client = ShareClient.from_connection_string( self.conn_str, self.share) self.share_client.create_share() except ResourceExistsError: pass
def get_first_snapshot(self, connection_string): try: # Create a ShareServiceClient from a connection string service_client = ShareServiceClient.from_connection_string(connection_string) # List the shares in the file service shares = list(service_client.list_shares(include_snapshots=True)) for share in shares: if (share["snapshot"]): return share["snapshot"] except ResourceNotFoundError as ex: print("ResourceNotFoundError:", ex.message)
def __init__(self, arguments): super().__init__(arguments) connection_string = self.get_from_env( "AZURE_STORAGE_CONNECTION_STRING") kwargs = {} if self.args.max_range_size: kwargs['max_range_size'] = self.args.max_range_size if not _ServiceTest.service_client or self.args.no_client_share: _ServiceTest.service_client = SyncShareServiceClient.from_connection_string( conn_str=connection_string, **kwargs) _ServiceTest.async_service_client = AsyncShareServiceClient.from_connection_string( conn_str=connection_string, **kwargs) self.service_client = _ServiceTest.service_client self.async_service_client = _ServiceTest.async_service_client
def authentication_shared_access_signature(self): # Instantiate a ShareServiceClient using a connection string # [START generate_sas_token] from azure.storage.fileshare import ShareServiceClient share_service_client = ShareServiceClient.from_connection_string( self.connection_string) # Create a SAS token to use to authenticate a new client from azure.storage.fileshare import generate_account_sas sas_token = generate_account_sas( share_service_client.account_name, share_service_client.credential.account_key, resource_types="object", permission="read", expiry=datetime.utcnow() + timedelta(hours=1))
def file_service_properties(self): # Instantiate the ShareServiceClient from a connection string from azure.storage.fileshare import ShareServiceClient file_service = ShareServiceClient.from_connection_string( self.connection_string) # [START set_service_properties] # Create service properties from azure.storage.fileshare import Metrics, CorsRule, RetentionPolicy # Create metrics for requests statistics hour_metrics = Metrics(enabled=True, include_apis=True, retention_policy=RetentionPolicy(enabled=True, days=5)) minute_metrics = Metrics(enabled=True, include_apis=True, retention_policy=RetentionPolicy(enabled=True, days=5)) # Create CORS rules cors_rule1 = CorsRule(['www.xyz.com'], ['GET']) allowed_origins = ['www.xyz.com', "www.ab.com", "www.bc.com"] allowed_methods = ['GET', 'PUT'] max_age_in_seconds = 500 exposed_headers = [ "x-ms-meta-data*", "x-ms-meta-source*", "x-ms-meta-abc", "x-ms-meta-bcd" ] allowed_headers = [ "x-ms-meta-data*", "x-ms-meta-target*", "x-ms-meta-xyz", "x-ms-meta-foo" ] cors_rule2 = CorsRule(allowed_origins, allowed_methods, max_age_in_seconds=max_age_in_seconds, exposed_headers=exposed_headers, allowed_headers=allowed_headers) cors = [cors_rule1, cors_rule2] # Set the service properties file_service.set_service_properties(hour_metrics, minute_metrics, cors) # [END set_service_properties] # [START get_service_properties] properties = file_service.get_service_properties()
def list_shares_snapshots(self, connection_string): try: # <Snippet_CreateShareServiceClient> # Create a ShareServiceClient from a connection string service_client = ShareServiceClient.from_connection_string(connection_string) # </Snippet_CreateShareServiceClient> # List the shares in the file service shares = list(service_client.list_shares(include_snapshots=True)) for share in shares: if (share["snapshot"]): print("Share:", share["name"], "Snapshot:", share["snapshot"]) else: print("Share:", share["name"]) except ResourceNotFoundError as ex: print("ResourceNotFoundError:", ex.message)
def set_logging_to_debug(): """ Prints in stdout Input: - env:AZ_CONN_STR Returns: Connection_object ShareServiceClient() """ # Create a logger for the 'azure.storage.fileshare' SDK logger = logging.getLogger('azure.storage.fileshare') logger.setLevel(logging.DEBUG) # Configure a console output handler = logging.StreamHandler(stream=sys.stdout) logger.addHandler(handler) # This client will log detailed information about its HTTP sessions, at DEBUG level service_client = ShareServiceClient.from_connection_string(AZ_CONN_STR, logging_enable=True) return service_client
def list_shares_in_service(self): # Instantiate the ShareServiceClient from a connection string from azure.storage.fileshare import ShareServiceClient file_service = ShareServiceClient.from_connection_string(self.connection_string) # [START fsc_create_shares] file_service.create_share(share_name="fileshare1") # [END fsc_create_shares] try: # [START fsc_list_shares] # List the shares in the file service my_shares = list(file_service.list_shares()) # Print the shares for share in my_shares: print(share) # [END fsc_list_shares] finally: # [START fsc_delete_shares] file_service.delete_share(share_name="fileshare1")
def clean_storage_account(connection_string): pool = ThreadPool(16) no_retry = azure.storage.common.retry.no_retry try: blob_service = BlobServiceClient.from_connection_string( connection_string) blob_service.retry = no_retry pool.map( lambda container: delete_container(blob_service, container.name), blob_service.list_containers(timeout=3)) except azure.core.exceptions.ServiceRequestError: print("No blob service") try: file_service = ShareServiceClient.from_connection_string( connection_string) file_service.retry = no_retry pool.map(lambda share: delete_file_share(file_service, share.name), file_service.list_shares(timeout=3)) except azure.core.exceptions.ServiceRequestError: print("No file service") try: queue_service = QueueServiceClient.from_connection_string( connection_string) queue_service.retry = no_retry pool.map(lambda queue: delete_queue(queue_service, queue.name), queue_service.list_queues(timeout=3)) except azure.core.exceptions.ServiceRequestError: print("No queue service") try: table_service = TableService(connection_string=connection_string) table_service.retry = no_retry pool.map(lambda table: delete_table(table_service, table.name), table_service.list_tables(timeout=3)) except azure.common.AzureException: print("No table service")
def run_all_samples(self, connection_string): print('Azure Storage File Basis samples - Starting.') #declare variables filename = 'filesample' + self.random_data.get_random_name(6) sharename = 'sharesample' + self.random_data.get_random_name(6) try: # Create an instance of ShareServiceClient service = ShareServiceClient.from_connection_string( conn_str=connection_string) print('\n\n* Basic file operations *\n') self.basic_file_operations(sharename, filename, service) except Exception as e: print('error:' + e) finally: # Delete all Azure Files created in this sample self.file_delete_samples(sharename, filename, service) print('\nAzure Storage File Basic samples - Completed.\n')
def create_client_with_connection_string(self): # Instantiate the ShareServiceClient from a connection string from azure.storage.fileshare import ShareServiceClient file_service = ShareServiceClient.from_connection_string( self.connection_string)
def authentication_connection_string(self): # Instantiate the ShareServiceClient from a connection string # [START create_share_service_client_from_conn_string] from azure.storage.fileshare import ShareServiceClient share_service_client = ShareServiceClient.from_connection_string( self.connection_string)
from azure.core.exceptions import ( ResourceExistsError, ResourceNotFoundError ) from azure.storage.fileshare import ( ShareServiceClient, ShareClient, ShareDirectoryClient, ShareFileClient ) import os, uuid, base64 connect_str = "DefaultEndpointsProtocol=https;AccountName=ninjagoinsbu;EndpointSuffix=core.windows.net" # Create a ShareServiceClient from a connection string service_client = ShareServiceClient.from_connection_string(connection_string) def create_file_share(self, connection_string, share_name): try: # Create a ShareClient from a connection string share_client = ShareClient.from_connection_string( connection_string, share_name) print("Creating share:", share_name) share_client.create_share() except ResourceExistsError as ex: print("ResourceExistsError:", ex.message)
import importlib import tempfile from azure.storage.fileshare import ShareServiceClient, ShareClient, ShareDirectoryClient, ShareFileClient from azure.storage.fileshare._download import StorageStreamDownloader from app.datacheck.utils import default_checks conn_str = "BlobEndpoint=https://devdcmstorage.blob.core.windows.net/;QueueEndpoint=https://devdcmstorage.queue.core.windows.net/;FileEndpoint=https://devdcmstorage.file.core.windows.net/;TableEndpoint=https://devdcmstorage.table.core.windows.net/;SharedAccessSignature=sv=2019-10-10&ss=bfqt&srt=sco&sp=rwdlacupx&se=2022-07-16T07:57:54Z&st=2020-07-15T23:57:54Z&spr=https&sig=4cDoQPv%2Ba%2FQyBEFcr2pVojyMj4vgsm%2Fld6l9TPveQH0%3D" service: ShareClient = ShareServiceClient.from_connection_string( conn_str).get_share_client('datachecks') def get_domain_checks(domain_id): checks = get_default_checks() + get_check_from_shared( folder_name=domain_id) return checks def get_default_checks(): return default_checks def get_check_from_shared(folder_name='default'): default_checks: ShareDirectoryClient = service.get_directory_client( folder_name) checks = []