简体   繁体   English

Python:检查Azure队列存储是否存在

[英]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.

相关问题 Python Firebase 如何查看firebase存储上是否存在文件 - Python Firebase How to check whether a file exists on firebase storage 使用 Azure.Data.Tables.TableClient 检查 Azure 存储表中是否存在一行 - Check if a row exists in Azure Storage Table using the Azure.Data.Tables.TableClient Azure服务总线中是否存在Poison Queue? - Does Poison Queue exists in Azure Service Bus? 如何模拟 Azure 单元测试的队列存储? - How to mock Azure Queue storage for unit test? 如何检查管道是否存在于 Azure 数据工厂中? - How to check if the pipeline exists in the Azure Data Factory? 为 Microsoft Azure Blob 存储自动化 Snowpipe - 错误:找不到通道的队列 - Automating Snowpipe for Microsoft Azure Blob Storage - error: Queue not found for channel 如何使用 SAS 向存储队列验证 Azure REST API? - How to authenticate Azure REST APIs to Storage Queue with SAS? Databricks:Azure Queue Storage structured streaming key not found 错误 - Databricks: Azure Queue Storage structured streaming key not found error 如何从您的 Android 应用程序检查 Firebase 存储中是否存在文件? - How to check if a file exists in Firebase storage from your android application? 检查 Firebase 实时数据库中是否存在条目(Python) - Check if entry exists in Firebase Realtime Databse (Python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM