Operations On Azure Blob with Python

Ana Jessica
featurepreneur
Published in
3 min readNov 30, 2021

Blobs are objects that can hold large amounts of text or binary data, including images, documents, streaming media, and archive data.

A container organizes a set of blobs, similar to a directory in a file system.

A storage account includes an unlimited number of containers and a container that can store an unlimited number of blobs.

In this article, we’ll see how to dump a file into an Azure Blob, using python and copy them from one container into another container.

Let’s see, how to do this with Python.

Prerequisites

  • An Azure account with an active subscription.
  • An Azure Storage account.
  • Python 2.7 or 3.6+.

Uploading a file, into a Blob by creating a Container

  1. Create a new directory for the project and switch to the newly-created directory.
mkdir azure-file-uploadercd azure-file-uploader

2. Inside the azure-file-uploader directory, create another directory called data. This directory is where the blob data files will be created and stored.

mkdir data

3. Install the Azure Blob Storage client library for Python package and all the libraries on which it depends. In this case, that is just the Azure core library for Python.

pip install azure-storage-blob

4. Create a python file, app.py and import the necessary packages.

import os, uuidfrom azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__from dotenv import load_dotenv

5. In the storage account menu pane, under Security + networking, select Access keys. Here, you can view the account access keys and the complete connection string for each key.

In the Access keys pane, select Show keys. In the key1 section, locate the Connection string value. Create an .env file, add a variable and copy the connection string into it.

AZURE_STORAGE_CONNECTION_STRING = "<connection string>"

and add this code to the app.py.

load_dotenv()connect_str = os.environ.get('AZURE_STORAGE_CONNECTION_STRING')

6. Create a try-except block, and add this code to create a container. The BlobServiceClient object will be used to create a container client and a unique name is given to it.

try:    print("Azure Blob Storage v" + __version__ + " - Python  quickstart sample")   blob_service_client = BlobServiceClient.from_connection_string(connect_str)   container_name = str(uuid.uuid4())   container_client = blob_service_client.create_container(container_name)except Exception as ex:   print('Exception:')
print(ex)

7. Add the code to the try block to create a local directory to hold blob data and to create and write a file in the local data directory to upload it into the blob.

local_path = "./data"os.mkdir(local_path)local_file_name = str(uuid.uuid4()) + ".txt"upload_file_path = os.path.join(local_path, local_file_name)file = open(upload_file_path, 'w')file.write("Hello, World!")file.close()

8. Add this code to the try block create a blob client using the local file name as the name for the blob and to upload the created file.

blob_client = blob_service_client.get_blob_client(container=container_name, blob=local_file_name)print("\nUploading to Azure Storage as blob:\n\t" + local_file_name)with open(upload_file_path, "rb") as data:
blob_client.upload_blob(data)

9. Run the python file.

python app.py

Output:

Azure Blob Storage v12 - Python quickstart sample 

Uploading to Azure Storage as blob:
quickstartcf275796-2188-4057-b6fb-038352e35038.txt

Thus a text file is created and uploaded into the Blob with Python.

Copying a blob from one container to another

Add a variable called account_name to the .env file and copy the account name to it.

Add this function, to move or copy Blob from one container to another.

account_name   = os.environ.get('account_name')# Sourcesource_container_name = "<source container name>"source_file_path = "<generated text file name>"blob_service_client = BlobServiceClient.from_connection_string(connection_string)source_blob = (f"https://{account_name}.blob.core.windows.net/{source_container_name}/{source_file_path}")# Targettarget_container_name = "<destination container name>"target_file_path = "<destination text file name>"copied_blob = blob_service_client.get_blob_client(target_container_name, target_file_path)copied_blob.start_copy_from_url(source_blob)

Hope you found this helpful!

--

--