Python Azure Blob Storage Tips and Ticks 1
Table of Contents
This tutorial will explain how to use Python to list blob files, upload blob files, copy blob files, check if blob file exists, delete blob files.
Preparing
Prepare Azure account
Create a new Storage Account
Get the connection string
Click [Access Keys] on the left side pane, and click [Show] button under key1
section to show the connection string.
Click copy button to copy the connection string.
Set the environment variable
- Windows
setx AZURE_STORAGE_CONNECTION_STRING "<yourconnectionstring>"
- Linux and macOS
export AZURE_STORAGE_CONNECTION_STRING="<yourconnectionstring>"
Python azure blob storage list files
For example, we have the following container and files.
import os
from azure.storage.blob import BlobServiceClient
# Get the connection string from the environment variables
connect_str = os.getenv('AZURE_STORAGE_CONNECTION_STRING')
# Create the BlobServiceClient object by using the connection string
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
try:
# List all the blobs under the container
list_response = blob_service_client.get_container_client("test").list_blobs()
for r in list_response:
print(r.name)
except Exception as e:
print("Failed to get the blob list in the container. Error:" + str(e))
The result:
123/test002.txt
test001.txt
Python azure blob storage list files inside a specific directory
For example, if we want to get the blob files inside 123
folder, we can use the following code. We can specify the prefix (123/) to get the blob files inside the specific directory.
import os
from azure.storage.blob import BlobServiceClient
# Get the connection string from the environment variables
connect_str = os.getenv('AZURE_STORAGE_CONNECTION_STRING')
# Create the BlobServiceClient object by using the connection string
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
try:
# List all the blobs under the container in the specific directory
list_response = blob_service_client.get_container_client("test").list_blobs(name_starts_with="123/")
for r in list_response:
print(r.name)
except Exception as e:
print("Failed to get the blob list in the container. Error:" + str(e))
123/test002.txt
Python azure blob storage upload file
For example, we have the following file in the local folder. We will upload the test2.txt to the root of the container.
import os
from azure.storage.blob import BlobServiceClient
# Get the connection string from the environment variables
connect_str = os.getenv('AZURE_STORAGE_CONNECTION_STRING')
# Create the BlobServiceClient object by using the connection string
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
try:
# We will keep the blob name same as the local file "test2.txt"
blob_client = blob_service_client.get_blob_client(container="test", blob="test2.txt")
# Upload the local file
with open(r"D:\test\test2.txt", "rb") as data:
blob_client.upload_blob(data)
print("Blob uploaded successfully!")
except Exception as e:
print("Failed to upload files to container. Error:" + str(e))
Blob uploaded successfully!
We can find the uploaded file in the container.
If you want to learn more about how to read and write files in python, you can read the following tutorial.
Python how to read and write files
https://thats-it-code.com/python/python__tips-and-tricks-5/#python-how-to-read-and-write-files
Python azure blob storage upload file into a specific directory
For example, we have the following file in the local folder. We will upload the test2.txt to the specified directory in the container.
import os
from azure.storage.blob import BlobServiceClient
# Get the connection string from the environment variables
connect_str = os.getenv('AZURE_STORAGE_CONNECTION_STRING')
# Create the BlobServiceClient object by using the connection string
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
try:
# We will keep the blob name same as the local file "test2.txt"
# To upload it to a specific directory, we need to specify the directory name (123/) in front of the blob name
blob_client = blob_service_client.get_blob_client(container="test", blob="123/test2.txt")
# Upload the local file
with open(r"D:\test\test2.txt", "rb") as data:
blob_client.upload_blob(data)
print("Blob uploaded successfully!")
except Exception as e:
print("Failed to upload files to container. Error:" + str(e))
Blob uploaded successfully!
We can find the uploaded file in the specified directory.
Python azure blob storage copy blob file
We can copy one blob file inside a container, or copy to another container.
import os
from azure.storage.blob import BlobServiceClient
# Get the connection string from the environment variables
connect_str = os.getenv('AZURE_STORAGE_CONNECTION_STRING')
# Create the BlobServiceClient object by using the connection string
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
# Get the source blob client
src_blob_client = blob_service_client.get_blob_client(container="test", blob="123/test2.txt")
# Create the destination blob client
dst_blob_client = blob_service_client.get_blob_client(container="test-b", blob="456/test2.txt")
try:
# Copy the blob using start_copy_from_url() method and specify the source blob URL
copy = dst_blob_client.start_copy_from_url(src_blob_client.url)
# Get the copy properties and print status
props = dst_blob_client.get_blob_properties()
print(props.copy.status)
except Exception as e:
print("Failed to copy blob. Error:" + str(e))
success
We can find the copied blob file in the specified directory.
Python azure blob storage check if blob exists
We can use exists()
method of blob client to check if blob file exists or not.
import os
from azure.storage.blob import BlobServiceClient
# Get the connection string from the environment variables
connect_str = os.getenv('AZURE_STORAGE_CONNECTION_STRING')
# Create the BlobServiceClient object by using the connection string
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
# Create a blob client which exists
exist_blob_client = blob_service_client.get_blob_client(container="test", blob="123/test2.txt")
# Create a blob client which does not exist
nonexist_blob_client = blob_service_client.get_blob_client(container="test", blob="456/test2.txt")
print(exist_blob_client.exists())
print(nonexist_blob_client.exists())
True
False
Python azure blob storage delete blob file
We can use delete_blob() method of blob client to delete blob file.
Before deleting the blob, we should check if the blob exists or not.
Blob storage status before deleting.
import os
from azure.storage.blob import BlobServiceClient
# Get the connection string from the environment variables
connect_str = os.getenv('AZURE_STORAGE_CONNECTION_STRING')
# Create the BlobServiceClient object by using the connection string
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
# Get the blob client to be deleted
todelete_blob_client = blob_service_client.get_blob_client(container="test", blob="test001.txt")
try:
# Check if the blob exists
if todelete_blob_client.exists():
# Delete blob
todelete_blob_client.delete_blob()
print("Blob deleted successfully!")
else:
print("Blob does not exist!")
except Exception as e:
print("Failed to delete blob. Error:" + str(e))
Blob deleted successfully!
The blob file is deleted.