Home Azure Cloud Explaining the Python Program That Connects to Azure Blob Storage

Explaining the Python Program That Connects to Azure Blob Storage

258
0

In the post Using Python with Jupyter Notebooks for Azure Projects, we set up Python and worked with Azure SDKs from a Jupytor notebook. One of the most common needs in Azure projects is working with storage — especially Blob Storage, which is designed for storing unstructured data such as documents, images, or logs. In this post, we’ll break down the sample program we used in that blog post that creates a container in Azure Blob Storage. The code for creating resource group was already explained in the blog post Explaining the Python Program That Connects to Azure via CLI Login.

We’ll explain each step from two viewpoints:

  • the Python perspective (what’s happening in the code), and
  • the Azure perspective (what’s happening in the cloud).

Recap: The Program

from azure.storage.blob import BlobServiceClient

# Connect to Blob Storage
storage_connection_string = "<your-storage-connection-string>"
blob_service_client = BlobServiceClient.from_connection_string(storage_connection_string)

# Create a container
container_name = "mycontainer"
container_client = blob_service_client.create_container(container_name)

print(f"Container '{container_name}' created successfully")

Breaking It Down

1. Imports

from azure.storage.blob import BlobServiceClient

Python perspective:
The import statement makes classes and functions from an external module available in your script. Here, BlobServiceClient is imported from the Azure Storage SDK. Without this import, Python wouldn’t recognize the class name.

Azure perspective:
The BlobServiceClient class is your entry point for working with Azure Blob Storage. It provides methods for connecting to a storage account, creating containers, and managing blobs.


2. Setting the Connection String

storage_connection_string = "<your-storage-connection-string>"

Python perspective:
A variable named storage_connection_string is created to store the connection string. In Python, variables are simply labels pointing to values stored in memory.

Azure perspective:
The connection string comes from your Azure Storage Account. It contains all the necessary authentication information (like account name and key) to allow your code to securely connect to that storage account. The connection string is like a digital “key” that gives your program access to your Azure Storage Account. You don’t invent it yourself — Azure generates it for you. To find it, go to the Azure Portal, open your Storage Account, and in the left-hand menu choose Access keys under the Security + networking section. There, you’ll see two keys, each with its own connection string. You can copy one of them and paste it into your program. Azure provides two sets so you can rotate them (switch between keys) without downtime, which is a security best practice. In short: this single string contains your account name and key bundled together. By pasting it here, you’re giving the SDK enough information to authenticate and communicate with Azure Blob Storage.


3. Creating the Blob Service Client

blob_service_client = BlobServiceClient.from_connection_string(storage_connection_string)

Python perspective:
This line creates an object called blob_service_client using the from_connection_string class method. In Python, there are generally two ways to create an object:

  1. Using a constructor — you directly call the class like BlobServiceClient(account_url, credential). This is the standard way most Python classes are instantiated.
  2. Using a class method (like from_connection_string) — this is a convenience method provided by the SDK. Instead of passing account URL and credentials separately, you can just pass the entire connection string and let the method handle parsing and setup for you.

Think of from_connection_string as a shortcut constructor. Both approaches ultimately create a BlobServiceClient object, but this method reduces boilerplate and is especially beginner-friendly.

In our example, we created the client object using the convenience method. If we had followed the constructor approach, code would have looked like the following:

blob_service_client = BlobServiceClient(account_url="https://<account_name>.blob.core.windows.net/", credential=account_key)

This gives you more control, since you explicitly provide the account URL and credentials.

Azure perspective:
The BlobServiceClient class is the primary gateway into Azure Blob Storage. It represents a connection to your storage account and provides the methods you need to perform operations like creating containers, uploading files, or listing blobs.

When you call from_connection_string, the class parses the connection string to understand two key things:

  1. Which storage account to connect to – identified by the account name and endpoint inside the string.
  2. How to authenticate – typically using an account key, but it could also be a SAS token or other credential type embedded in the string.
    👉 How authentication actually happened in our case: We used the connection string copied from the Azure Portal, which contains the account key. This key works like a master password. Each request the BlobServiceClient sends to Azure is cryptographically signed with that key. Azure Storage then validates the signature on its side — if it matches, the request is authenticated and authorized to proceed.

From Azure’s point of view, this is equivalent to establishing a secure session between your Python program and the storage account. Once the client is created, every operation you call on it (like creating a container or uploading a blob) translates into a REST API request that gets sent to the Azure Storage service, with authentication details already handled by the client.

So whether you use the constructor or from_connection_string, the result is the same: a fully authenticated client object that speaks Azure’s storage APIs on your behalf.


4. Creating a Container

container_name = "mycontainer"
container_client = blob_service_client.create_container(container_name)

Python perspective:

  • A variable container_name is set to "mycontainer".
  • The method create_container is then called on the blob_service_client. This method creates a container in Azure and returns a ContainerClient object, which you store in container_client.

Azure perspective:
A container in Azure Blob Storage is like a folder, but at the storage service level. It groups blobs (files) together, provides a namespace, and can have its own access policies. When you run create_container, the SDK sends a REST API request to the Azure Storage service, asking it to provision a new container under your storage account.

👉 How authentication works for this call: The request is automatically signed using the credentials that were set up in the blob_service_client (in our case, the account key from the connection string). Azure validates the signature and, if it’s valid, authorizes the container creation.

So, what looks like a simple method call in Python is actually:

  1. The SDK constructing a properly formatted HTTP request (REST API call).
  2. The request being cryptographically signed with your credentials.
  3. Azure Storage validating the signature and, if valid, creating a new container resource.

At this point, your container_client object can be used to perform further operations like uploading blobs, listing blobs, or setting access levels on that container.


5. Printing Success

print(f"Container '{container_name}' created successfully")

Python perspective:
This line simply outputs a confirmation message to the console. In Python, print() is a built-in function that displays text for the user. While it doesn’t affect the logic of your program, it’s often used for feedback and debugging — letting you (or anyone running the script) know that the previous operation worked as expected.

Here, printing "Container created successfully!" acts as a lightweight checkpoint. If you see this message, it means no exceptions were raised during create_container, so you can be confident the container exists. In production code, you’d typically rely on logging frameworks (like Python’s logging module) instead of plain print() for more structured and configurable output, but for tutorials and quick scripts, print() is perfect.

Notice also the f before the string. This makes it a formatted string literal (an f-string). Inside an f-string, anything inside curly braces {} is evaluated at runtime. In this case, {container_name} is replaced by the actual name of the container variable. For example, if container_name = "mycontainer", the output would be:

Container 'mycontainer' created successfully

This is cleaner and more readable than concatenating strings with +, and it ensures the message always reflects the actual container you just created. In short, the f is a handy way to embed variables or even expressions directly inside strings without needing concatenation like:

print("Container '" + container_name + "' created successfully")

Azure perspective:
There’s no cloud-side action here — it’s just local confirmation. But it’s a useful way to quickly validate that your request to Azure succeeded.


Conclusion

In this walkthrough, we dissected the Python program that creates a container in Azure Blob Storage. By looking at it from both the Python and Azure angles, we see how basic programming concepts (imports, variables, objects, and print statements) connect directly to Azure services (authentication, storage accounts, containers, and data organization).

The key takeaway is that BlobServiceClient bridges your Python code to Azure Blob Storage, letting you work with cloud storage as easily as with local files.

From here, you can build on this foundation by:

  • Uploading and downloading blobs (files),
  • Listing containers and blobs,
  • Managing access with SAS tokens or RBAC, and
  • Integrating Blob Storage with other Azure services like Functions or Data Lake.

References

Previous articleUsing Python with Jupyter Notebooks for Azure Projects
Next articleUsing Python with Google Colab for Azure Projects
Heartin Kanikathottu
As a seasoned Cloud and Security Architect, I’ve led transformative initiatives in key roles, including Vice President at Morgan Stanley, Principal Architect at Societe Generale, and Tech Lead & Cloud Security Architect at VMware, among others. I’m also an internationally published author with multiple books available on platforms like Amazon and O'Reilly. Notably, one of my books was recognized as the 8th best cloud computing book of all time in 2020, reflecting the impact of my contributions to the field. With over 15 professional certifications from providers such as Microsoft (Azure), Amazon (AWS), Oracle (Java), Pivotal (Spring), and IBM, I bring a wealth of expertise to my work. Academically, I hold dual Master’s degrees in Cloud Computing and Data Analytics. I’m passionate about sharing knowledge and mentoring others, which is why I actively speak at global technical forums such as Tech Opportunities Fest at Platform Calgary, Google's Kubernetes Meetup, Java User Group, Elasticsearch Meetup, and the Agile India Conference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here