def test_queues_in_account(self, resource_group, location, storage_account, storage_account_key): # Instantiate the QueueServiceClient from a connection string from azure.storage.queue import QueueServiceClient queue_service = QueueServiceClient.from_connection_string( self.connection_string(storage_account, storage_account_key)) # [START qsc_create_queue] queue_service.create_queue("testqueue") # [END qsc_create_queue] try: # [START qsc_list_queues] # List all the queues in the service list_queues = queue_service.list_queues() for queue in list_queues: print(queue) # List the queues in the service that start with the name "test" list_test_queues = queue_service.list_queues( name_starts_with="test") for queue in list_test_queues: print(queue) # [END qsc_list_queues] finally: # [START qsc_delete_queue] queue_service.delete_queue("testqueue")
def queues_in_account(self): # Instantiate the QueueServiceClient from a connection string from azure.storage.queue import QueueServiceClient queue_service = QueueServiceClient.from_connection_string(conn_str=self.connection_string) # [START qsc_create_queue] queue_service.create_queue("myqueue1") # [END qsc_create_queue] try: # [START qsc_list_queues] # List all the queues in the service list_queues = queue_service.list_queues() for queue in list_queues: print(queue) # List the queues in the service that start with the name "my" list_my_queues = queue_service.list_queues(name_starts_with="my") for queue in list_my_queues: print(queue) # [END qsc_list_queues] finally: # [START qsc_delete_queue] queue_service.delete_queue("myqueue1")
def run_all_samples(self, connection_string): try: print('Azure Storage Basic Queue samples - Starting.') # declare variables queuename = "queuesample" + self.random_data.get_random_name(6) queuename2 = "queuesample" + self.random_data.get_random_name(6) # create a new queue service that can be passed to all methods queue_service = QueueServiceClient.from_connection_string( conn_str=connection_string) # Basic queue operations such as creating a queue and listing all queues in your account print('\n\n* Basic queue operations *\n') self.basic_queue_operations(queue_service, queuename, queuename2) # Add a message to a queue in your account print('\n\n* Basic message operations *\n') self.basic_queue_message_operations(queue_service, queuename) except Exception as e: print('Error occurred in the sample.', e) finally: # Delete the queues from your account self.delete_queue(queue_service, queuename) self.delete_queue(queue_service, queuename2) print('\nAzure Storage Basic Queue samples - Completed.\n')
def run_all_samples(self, connection_string): try: print('Azure Storage Advanced Queue samples - Starting.') # create a new queue service that can be passed to all methods queue_service = QueueServiceClient.from_connection_string( conn_str=connection_string) print('\n\n* List queues *\n') self.list_queues(queue_service) print('\n\n* Set cors Rules *\n') self.set_cors_rules(queue_service) print('\n\n* ACL operations *\n') self.queue_acl_operations(queue_service) print('\n\n* Set service logging and metrics properties *\n') self.set_service_properties(queue_service) print('\n\n* Set queue metadata *\n') self.metadata_operations(queue_service) except Exception as e: print('Error occurred in the sample.', e) finally: print('\nAzure Storage Advanced Queue samples - Completed\n')
def get_queue_client(self): # Instantiate the QueueServiceClient from a connection string from azure.storage.queue import QueueServiceClient, QueueClient queue_service = QueueServiceClient.from_connection_string(conn_str=self.connection_string) # [START get_queue_client] # Get the queue client to interact with a specific queue queue = queue_service.get_queue_client(queue="myqueue2")
def create_client_with_connection_string(self): # Instantiate the QueueServiceClient from a connection string from azure.storage.queue import QueueServiceClient queue_service = QueueServiceClient.from_connection_string( conn_str=self.connection_string) # Get queue service properties properties = queue_service.get_service_properties()
def test_create_client_with_connection_string(self, resource_group, location, storage_account, storage_account_key): # Instantiate the QueueServiceClient from a connection string from azure.storage.queue import QueueServiceClient queue_service = QueueServiceClient.from_connection_string(self.connection_string(storage_account, storage_account_key)) # Get queue service properties properties = queue_service.get_service_properties() assert properties is not None
def test_queue_service_properties(self, resource_group, location, storage_account, storage_account_key): # Instantiate the QueueServiceClient from a connection string from azure.storage.queue import QueueServiceClient queue_service = QueueServiceClient.from_connection_string( self.connection_string(storage_account, storage_account_key)) # [START set_queue_service_properties] # Create service properties from azure.storage.queue import Logging, Metrics, CorsRule, RetentionPolicy # Create logging settings logging = Logging(read=True, write=True, delete=True, retention_policy=RetentionPolicy(enabled=True, days=5)) # 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 queue_service.set_service_properties(logging, hour_metrics, minute_metrics, cors) # [END set_queue_service_properties] # [START get_queue_service_properties] properties = queue_service.get_service_properties()
def auth_connection_string(self): # Instantiate a QueueServiceClient using a connection string # [START auth_from_connection_string] from azure.storage.queue import QueueServiceClient queue_service = QueueServiceClient.from_connection_string( conn_str=self.connection_string) # [END auth_from_connection_string] # Get information for the Queue Service properties = queue_service.get_service_properties()
def test_get_queue_client(self, resource_group, location, storage_account, storage_account_key): # Instantiate the QueueServiceClient from a connection string from azure.storage.queue import QueueServiceClient, QueueClient queue_service = QueueServiceClient.from_connection_string( self.connection_string(storage_account, storage_account_key)) # [START get_queue_client] # Get the queue client to interact with a specific queue queue = queue_service.get_queue_client("myqueue")
def test_auth_shared_access_signature(self): # SAS URL is calculated from storage key, so this test runs live only if TestMode.need_recording_file(self.test_mode): return # Instantiate a QueueServiceClient using a connection string from azure.storage.queue import QueueServiceClient queue_service = QueueServiceClient.from_connection_string( self.connection_string) # Create a SAS token to use for authentication of a client sas_token = queue_service.generate_shared_access_signature( resource_types="object", permission="read", expiry=datetime.utcnow() + timedelta(hours=1)) assert sas_token is not None
def test_auth_shared_access_signature(self, resource_group, location, storage_account, storage_account_key): # SAS URL is calculated from storage key, so this test runs live only if not self.is_live: return connection_string = self.connection_string(storage_account, storage_account_key) # Instantiate a QueueServiceClient using a connection string from azure.storage.queue import QueueServiceClient queue_service = QueueServiceClient.from_connection_string( connection_string) # Create a SAS token to use for authentication of a client sas_token = queue_service.generate_shared_access_signature( resource_types="object", permission="read", expiry=datetime.utcnow() + timedelta(hours=1)) assert sas_token is not None
def auth_shared_access_signature(self): # Instantiate a QueueServiceClient using a connection string from azure.storage.queue import QueueServiceClient queue_service = QueueServiceClient.from_connection_string( conn_str=self.connection_string) # Create a SAS token to use for authentication of a client from azure.storage.queue import generate_account_sas sas_token = generate_account_sas(queue_service.account_name, queue_service.credential.account_key, resource_types="object", permission="read", expiry=datetime.utcnow() + timedelta(hours=1)) token_auth_queue_service = QueueServiceClient( account_url=self.account_url, credential=sas_token) # Get information for the Queue Service properties = token_auth_queue_service.get_service_properties()
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 test_queues_in_account(self): # Instantiate the QueueServiceClient from a connection string from azure.storage.queue import QueueServiceClient queue_service = QueueServiceClient.from_connection_string( self.connection_string) # [START qsc_create_queue] queue_service.create_queue("testqueue") # [END qsc_create_queue] try: # [START qsc_list_queues] # List all the queues in the service list_queues = next(queue_service.list_queues()) # List the queues in the service that start with the name "test" list_test_queues = next( queue_service.list_queues(name_starts_with="test")) # [END qsc_list_queues] finally: # [START qsc_delete_queue] queue_service.delete_queue("testqueue")
FILE: consume_cloud_events_from_storage_queue.py DESCRIPTION: These samples demonstrate receiving events from a Storage Queue. USAGE: python consume_cloud_events_from_storage_queue.py Set the environment variables with your own values before running the sample: 1) STORAGE_QUEUE_CONN_STR: The connection string to the Storage account 3) STORAGE_QUEUE_NAME: The name of the storage queue. """ from azure.eventgrid import CloudEvent from azure.storage.queue import QueueServiceClient, BinaryBase64DecodePolicy import os import json # all types of CloudEvents below produce same DeserializedEvent connection_str = os.environ['STORAGE_QUEUE_CONN_STR'] queue_name = os.environ['STORAGE_QUEUE_NAME'] with QueueServiceClient.from_connection_string(connection_str) as qsc: payload = qsc.get_queue_client( queue=queue_name, message_decode_policy=BinaryBase64DecodePolicy() ).peek_messages() ## deserialize payload into a lost of typed Events events = [CloudEvent.from_dict(json.loads(msg.content)) for msg in payload] for event in events: print(type(event)) ## CloudEvent
import json import time from azure.storage.queue import QueueServiceClient, QueueClient # Read the configuration with open('config.json') as f: config = json.load(f) connection_string = config["secrets"]["queuestorage"]["connection_string"] queue_name = config["configuration"]["queue_name"] queue_service_client = QueueServiceClient.from_connection_string( conn_str=connection_string) queue_client = QueueClient.from_connection_string(connection_string, queue_name) """ list_queues = queue_service_client.list_queues() for queue in list_queues: print(queue) print("\nAdding messages to the queue...") # Send several messages to the queue queue_client.send_message(u"First message") queue_client.send_message(u"Second message") saved_message = queue_client.send_message(u"Third message") print("\nPeek at the messages in the queue...") # Peek at messages in the queue peeked_messages = queue_client.peek_messages(max_messages=20) for peeked_message in peeked_messages:
def _get_service_client_from_connection_string(self, connection_string: str): return QueueServiceClient.from_connection_string( conn_str=connection_string)