我想要获取队列存储并创建(如果不存在)。对于大多数类似的场景,我使用的是 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)
我可以在 if 语句中使用什么来解决这个问题?
最佳答案
您可以使用 try
except
代替。根据文档 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
关于Python:检查Azure队列存储是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69449622/