DEV Community

Juhana Jauhiainen
Juhana Jauhiainen

Posted on • Originally published at juhanajauhiainen.com

3

How to search files from Azure Blob Storage using Python

Searching for files using index tags is a common use case but the Azure documentation can be quite hard to grasp.

To search for blobs with specific tags in Azure Blob Storage using Python, you can use the find_blobs_by_tags method from the Azure Storage SDK. It takes as a single parameter filter_expression which can be used to query blobs with specific tags, container or name.

Here's an example of how to use find_blobs_by_tags to search for blobs with specific tags

from azure.storage.blob import BlobServiceClient

# Create a BlobServiceClient object
blob_service_client = BlobServiceClient.from_connection_string("<your_connection_string>")

# Get a reference to the container where your blobs are stored
container_client = blob_service_client.get_container_client("<your_container_name>")

# Use the find_blobs_by_tags method to search for blobs with a specific set of tags
results = container_client.find_blobs_by_tags(
    "key1 = 'value1' and key2 = 'value2'"
)

# Print the names of the blobs that were found
for blob in results:
    print(blob["name"])

Enter fullscreen mode Exit fullscreen mode

In this example, the find_blobs_by_tags method is used to search for blobs that have the tags key1 with a value of value1 and key2 with a value of value2.

find_blobs_by_tags returns a iterable response of FilteredBlob objects. The return value is automatically paginated and you can control the number of Blobs per page with the results_per_page keyword argument. You can iterate the results by blob or by page using the iterator returned by the by_page function.

Further reading

Azure SDK for Python documentation

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay