Azure Python SDK:BlobServiceClient 与 BlobClient?

Sea*_*ude 2 python azure-blob-storage azure-python-sdk

我见过的大多数(全部?)Azure 存储 Python SDK 示例都演示了如何创建一个BlobServiceClient用于BlobClient上传/下载 blob(ref1ref2等)的 。

为什么要创建一个BlobServiceClientthen aBlobClient而不是直接创建一个BlobClient

例子:

from azure.storage.blob import BlobClient


def create_blob_client(connection_string):
    try:
        blob_client = BlobClient.from_connection_string(connection_string)
    except Exception as e:
        logging.error(f"Error creating Blob Service Client: {e}")
    return blob_client

connection_string = os.environ["CONNECTION_STRING"]

blob_client = create_blob_client(connection_string)
Run Code Online (Sandbox Code Playgroud)

Gau*_*tri 6

为什么先创建 BlobServiceClient,然后再创建 BlobClient,而不是直接创建 BlobClient?

BlobClient只允许您使用 blob,因此如果您只想使用 blob,您可以直接创建 BlobClient 并使用它。无需先创建 BlobServiceClient,然后再从中创建 BlobClient。

BlobServiceClient如果您想要在 Blob 服务级别执行操作(例如设置 CORS 或列出存储帐户中的 Blob 容器),则会出现这种情况。这时候你就需要BlobServiceClient。