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

标签 python azure azure-storage-queues

我想要获取队列存储并创建(如果不存在)。对于大多数类似的场景,我使用的是 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/

相关文章:

python - 从 CSV 文件读取/写入嵌套字典列表 (Python)

java - 与主键、数据类型映射相关的Azure离线sqlite存储同步问题

azure - 在 Azure ML 服务中安装 FileDatasets

c# - 如何在 C# 中将所有消息存储在 Azure 存储队列上而不出列

nservicebus - 是否可以在 NServiceBus 中对事件发布者进行范围/分组?

Azure队列存储: Send files in messages

python - 无法连接 'str' 和 'int' 对象,但我可以通过我自己的 IDLE 正常运行它?

python - 在树莓派上安装pypy3

python - 如何将列表值与字典键进行比较并使用 python 创建一个新字典

c# - 从 Azure 应用程序配置动态更新 .net core 配置