I want to get queue storage and create if it not exist. For most similar scenario I was using exists()
method but when I look at python documentation (https://docs.microsoft.com/en-us/python/api/azure-storage-queue/azure.storage.queue.queueclient?view=azure-python) I can not see any method that can solve this problem
Here is my code:
def send_to_queue(CONN_STR, queue_name, mess):
service_client = QueueServiceClient.from_connection_string(conn_str=CONN_STR)
queue = service_client.get_queue_client(queue_name)
if not queue.exists():
queue.create_queue()
queue.send_message(mess)
What can I use in my if statement to solve this?
CodePudding user response:
You can use try
except
instead. As per the docs create_queue
creates a new queue in the storage account. If a queue with the same name already exists, the operation fails with a ResourceExistsError
.
from azure.core.exceptions import ResourceExistsError
def send_to_queue(CONN_STR, queue_name, mess):
service_client = QueueServiceClient.from_connection_string(conn_str=CONN_STR)
queue = service_client.get_queue_client(queue_name)
try:
queue.create_queue()
except ResourceExistsError:
# Resource exists
pass