[英]Python: Check if Azure queue storage exists
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://learn.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:对于大多数类似的场景,我使用的是
exists()
方法,但是当我查看 python 文档时( https://learn.microsoft.com/en-us/python/api/azure-storage-queue/azure.storage.queue.queueclient ?view=azure-python ) 我看不到任何可以解决此问题的方法这是我的代码:
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?我可以在 if 语句中使用什么来解决这个问题?
You can use try
except
instead.您可以改用
try
except
。 As per the docs create_queue
creates a new queue in the storage account.根据文档
create_queue
在存储帐户中创建一个新队列。 If a queue with the same name already exists, the operation fails with a ResourceExistsError
. 如果已存在同名队列,则操作失败并出现
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.