2

Most (all?) of the Azure Storage Python SDK examples I've seen demonstrate creating a BlobServiceClient in order to then create a BlobClient for uploading / downloading blobs (ref1, ref2, etc.).

Why create a BlobServiceClient then a BlobClient instead of just directly creating a BlobClient?

Example:

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)
SeaDude
  • 3,725
  • 6
  • 31
  • 68

3 Answers3

6

Why create a BlobServiceClient then a BlobClient instead of just directly creating a BlobClient?

BlobClient only allows you to work with blobs so if you want to work with just blobs, you can directly create a BlobClient and work with it. No need for you to create a BlobServiceClient first and then create BlobClient from it.

BlobServiceClient comes into picture If you want to perform operations at the blob service level like setting CORS or list blob containers in a storage account. At that time you will need BlobServiceClient.

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
3

If you're creating a single BlobClient, and that's all you need, then just create it directly. If you're creating more than one BlobClient using the same credentials and within the same storage account, it's better to create each BlobClient from a single BlobServiceClient (which has the credentials and storage account).

If you create them from a single BlobServiceClient, they all share the same BlobClientConfiguration, which is responsible for handling request building and authentication (there's a bit of overhead per BlobClientConfiguration, including an authentication token request in some cases that periodically gets refreshed). If you create each BlobClient separately, each client will create its own pipeline and authentication cache.

Disclaimer: I have not verified this in the Python Azure SDK code base, but it is true in the .NET Azure SDK code, and I'm pretty sure they use the same logic - they do use the same API design.

crimbo
  • 10,308
  • 8
  • 51
  • 55
1

BlobServiceClient.get_blob_client is just a convenience method to easily get a BlobClient if you've already created a BlobServiceClient and provided the connection information. The BlobServiceClient API says:

This client provides operations to retrieve and configure the account properties as well as list, create and delete containers within the account. For operations relating to a specific container or blob, clients for those entities can also be retrieved using the get_client functions.

There's nothing wrong with creating a BlobClient directly if that's all you need for your use case (e.g. your container is already created). It's likely the docs and examples use the BlobServiceClient.get_blob_client method because they are typically end-to-end i.e. create a container, upload a blob, delete container etc.

marcusturewicz
  • 2,394
  • 2
  • 23
  • 38
  • Thank you for the clarification. Coming in as a Python newb, I was never quite sure if I was missing some function that `BlobServiceClient` served. – SeaDude Aug 02 '21 at 06:26
  • Is there any benefit to spawning a `BlobClient` from a `BlobServiceClient` when using the `azure.storage.blob.aio` libraries and `asyncio`? – SeaDude Aug 06 '21 at 03:55