检查python azure中是否存在blob

Rap*_*aël 3 python azure azure-storage-blobs azure-blob-storage

由于 azure-storage-blob 新更新,blockblobservice 折旧了

如何检查 blob 是否存在?

此答案不适用于新版本的 azure-storage-blob Faster Azure blob name search with python?

我在 GitHub 上发现了这个问题:https : //github.com/Azure/azure-sdk-for-python/issues/12744

kri*_*shg 6

2020 年 9 月 10 日发布的12.5.0版本现已exists在新 SDK 中包含该方法。

例如,

同步:

from azure.storage.blob import BlobClient

blob = BlobClient.from_connection_string(conn_str="my_connection_string", container_name="mycontainer", blob_name="myblob")
exists = blob.exists()
print(exists)
Run Code Online (Sandbox Code Playgroud)

异步:

import asyncio

async def check():
    from azure.storage.blob.aio import BlobClient
    blob = BlobClient.from_connection_string(conn_str="my_connection_string", container_name="mycontainer", blob_name="myblob")
    async with blob:
        exists = await blob.exists()
        print(exists)
Run Code Online (Sandbox Code Playgroud)