0 ratings0% found this document useful (0 votes) 116 views13 pagesAzure Blob Storage Client Library For Python
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
‘2124, 5:13 PM Quickstart: Azure Blob Storage clent brary for Python - Azure Storage | Microsoft Loam
Quickstart: Azure Blob Storage client
library for Python
Article + 01/31/2023
© Al-assisted content. This article was partially created with the help of Al. An author reviewed and
revised the content as needed. Learn more
Get started with the Azure Blob Storage client library for Python to manage blobs and
containers. Follow these steps to install the package and try out example code for basic
tasks in an interactive console app.
API reference documentation | Library source code _| Package (PyPi)_| Samples
This video shows you how to start using the Azure Blob Storage client library for Python.
https://learn-video.azurefd.net/vod/player?id=#663a554-96ca-4bc3-b3b1-
48376a7efbdf&locale=en-us&embedUrl=%2Fazure%2F storage%2Fblobs%2Fstorage-
quickstart-blobs-python
The steps in the video are also described in the following sections.
Prerequisites
* Azure account with an active subscription - create an account for free
* Azure Storage account - create a storage account
* Python 37+
Setting up
This section walks you through preparing a project to work with the Azure Blob Storage
client library for Python.
Create the project
Create a Python application named blob-quickstart.
1, In a console window (such as PowerShell or Bash), create a new directory for the
project:
nttps:/leam microsotcomlen-uslazurelstorage/olobsistorage-quickstat-blobs-pythontabs=managed-Wentiyik2Croles-azure-portalik2Csign-in-azu.... 1/13‘2124, 5:13 PM Quickstart: Azure Blob Storage clent brary for Python - Azure Storage | Microsoft Lam
Console
mkdir blob-quickstart
2, Switch to the newly created blob-quickstart directory:
Console
cd blob-quickstart
Install the packages
From the project directory, install packages for the Azure Blob Storage and Azure Identity
client libraries using the pip install command. The azure-identity package is needed for
passwordless connections to Azure services.
Console
pip install azure-storage-blob azure-identity
Set up the app framework
From the project directory, follow steps to create the basic structure of the app:
1. Open a new text file in your code editor.
2. Add inport statements, create the structure for the program, and include basic
exception handling, as shown below.
3, Save the new file as blob-quickstart py in the blob-quickstart directory.
Python
import os, uid
from azure. identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
try:
print(“Azure Blob Storage Python quickstart sample”)
# Quickstart code goes here
except Exception as ex:
nttps:/leam microsotcomlen-uslazurelstorage/olobsistorage-quicksta-blobs-pythontabs=managed-Wentiyik2Croles-azure-portalik2Csign-in-azu.... 2/13112024, 5:19 Pa Cuickatart: Azure lab Storage cent Roary for Python - Azure Storage | Mirosof Leer
print( ‘Exception: ")
print(ex)
Object model
Azure Blob Storage is optimized for storing massive amounts of unstructured data.
ion, such
Unstructured data is data that doesn't adhere to a particular data model or defi
as text or binary data. Blob storage offers three types of resources:
‘* The storage account
* Acontainer in the storage account
* Ablob in the container
The following diagram shows the relationship between these resources:
. LL
EM pictures
Use the following Python classes to interact with these resources
‘© BlobServiceClient: The slobservicect ient class allows you to manipulate Azure
Storage resources and blob containers.
* ContainerClient: The containerclient class allows you to manipulate Azure Storage
containers and their blobs.
* BlobClient: The Blobclient class allows you to manipulate Azure Storage blobs.
Code examples
These example code snippets show you how to do the following tasks with the Azure Blob
Storage client library for Python:
‘© Authenticate to Azure and authorize access to blob data
© Create a container
‘© Upload blobs to a container
¢ List the blobs in a container
* Download blobs
nttps:/leam microsotcomlen-uslazuelstorage/olobsistorage-quicksta-blobs-pythontabs=managed-Wentiyik2Croles-azure-portalik2Csign-in-azu.... 3/13‘2124, 5:13 PM Quickstart: Azure Blob Storage clent brary for Python - Azure Storage | Microsoft Lam
© Delete a container
Authenticate to Azure and authorize access to blob data
Application requests to Azure Blob Storage must be authorized. Using the
Defaultazurecredential class provided by the Azure Identity client library is the
recommended approach for implementing passwordless connections to Azure services in
your code, including Blob Storage.
You can also authorize requests to Azure Blob Storage by using the account access key.
However, this approach should be used with caution, Developers must be diligent to never
expose the access key in an unsecure location. Anyone who has the access key is able to
authorize requests against the storage account, and effectively has access to all the data
Defaultazurecredential offers improved management and security benefits over the
account key to allow passwordless authentication. Both options are demonstrated in the
following example.
Passwordless (Recommended)
DefaultazureCredential supports multiple authentication methods and determines
which method should be used at runtime. This approach enables your app to use
different authentication methods in different environments (local vs. production)
without implementing environment-specific code,
The order and locations in which pefaultAzureCredential looks for credentials can be
found in the Azure Identity library overview.
For example, your app can authenticate using your Azure CLI sign-in credentials with
when developing locally. Your app can then use a managed identity once it has been
deployed to Azure, No code changes are required for this transition.
Assign roles to your Microsoft Entra user account
When developing locally, make sure that the user account that is accessing blob data
has the correct permissions. You'll need Storage Blob Data Contributor to read and
write blob data. To assign yourself this role, you'll need to be assigned the User Access
Administrator role, or another role that includes the
Microsoft. Authorization/roleAssignments/write action. You can assign Azure RBAC
nttps:/leam microsotcomlen-uslazurelstorage/olobsistorage-quickstat-blobs-pythontabs=managed-entiyik2Croles-azure-portalik2Csign-in-azu.... 4/131122724, 5:19 PM Cuickter: Azure Bad Storage cent brary for Python - Azure Storage | Microsoft Lea
roles to a user using the Azure portal, Azure CLI, or Azure PowerShell. You can learn
more about the available scopes for role assignments on the scope overview page
In this scenario, you'll assign permissions to your user account, scoped to the storage
account, to follow the Principle of Least Privilege. This practice gives users only the
minimum permis
ns needed and creates more secure production environments
The following example will assign the Storage Blob Data Contributor role to your user
account, which provides both read and write access to blob data in your storage
account.
© Important
In most cases it will take a minute or two for the role assignment to propagate in
Azure, but in rare cases it may take up to eight minutes. If you receive
authentication errors when you first run your code, wait a few moments and try
again.
‘Azure portal
1. In the Azure portal, locate your storage account using the main search bar or
left navigation,
2. On the storage account overview page, select Access control (IAM) from the
left-hand menu.
3. On the Access control (IAM) page, select the Role assignments tab.
4, Select + Add from the top menu and then Add role assignment from the
resulting drop-down menu.
nitps:/leam microsotcomlen-uslazurelstorage/olobsistorage-quickstat-blobs-pythontabs=managed-Wentiyik2Croles-azure-portaik2Csign-in-azu.... 5/13‘22124, 513 PM
Cuickstar: Azure Blob Storage ch
lent brary for Python - Azure Storage | Microsoft eam
ig identitymigrationstorage | Access Control (VAM)
Use the search box to filter the results to the desired role. For this example,
search for Storage Blob Data Contributor and select the matching result and
then choose Next.
choose + Select members.
In the dialog, search for your Microso}
Under Assign access to, select User, group, or service principal, and then
ft Entra username (usually your
user@domain email address) and then choose Select at the bottom of the
dialog
8, Select Review + assign to go to the fi
again to complete the process.
inal page, and then Review + assign
Sign in and connect your app code to Azure using
DefaultAzureCredential
You can authorize access to data in your storage account using the following steps:
nitps:/leam mierosot.comlen-ustazuelstoragelobsistocage-quickstart-blabs-python7tabs*managed-identty%s2Croles-azure-portalis2Csigh-in-az
1, Make sure you're authenticated with the same Microsoft Entra account you
assigned the role to on your storage account. You can authenticate via the Azure
CLI, Visual Studio Code, or Azure PowerShell.
Azure CLI
Sign-in to Azure through the Azure C!
LI using the following command:
ena‘22124, 513 PM Quickstart: Azure Blob Storage clent brary for Python - Azure Storage | Microsoft Lam
Azure CU
az login
2. To use DefaultazureCredential, make sure that the azure-identity package is
installed, and the class is imported:
Python
from azure. identity import DefaultAzureCredential
3. Add this code inside the try block. When the code runs on your local
workstation, DefaultazureCredential uses the developer credentials of the
prioritized tool you're logged into to authenticate to Azure. Examples of these
tools include Azure CLI or Visual Studio Code.
Python
account_url = "https://.blob.core.windows net”
default_credential = DefaultAzureCredential()
# Create the BlobServiceClient object
blob_service_client = BlobServiceClient(account_url,
credential=default_credential)
4, Make sure to update the storage account name in the URI of your
BlobServiceClient object. The storage account name can be found on the
overview page of the Azure portal.
nttps:/leam microsotcomlen-uslazuelstorage/olobsistorage-quickstat-blobs-pythontabs=managed-Wentiyik2Croles-azure-portalik2Csign-in-azu.... 7/13‘yo2i24, 5:13 PM Quickstart Azure Blob Storage client library for Python - Azure Storage | MicrosoftLearn
Home
«a [dentitymigrationstorage] ° =
cess ek ner to tags
F vplead Ey 0 joree GI Daiete > Move ~ C) netesh
= = A terete
a Resource group ave) + slenieny-reamp
e Location cast
rimary/Secondary Primary: as US, Seconda: West US
? solve problems "
Subseption is) (CL Cross Service Content Team Testing
Fa ecese conan
Subserption 19
© canrmricn Diskstate Primang Avaliable Secondary Avaliable
’
=
storage bowzer
© Note
When deployed to Azure, this same code can be used to authorize requests
to Azure Storage from an application running in Azure. However, you'll need
to enable managed identity on your app in Azure. Then configure your
storage account to allow that managed identity to connect. For detailed
instructions on configuring this connection between Azure services, see the
Auth from Azure-hosted apps tutorial.
Create a container
Decide on a name for the new container. The code below appends a UUID value to the
container name to ensure that it's unique.
© Important
Container names must be lowercase. For more information about naming containers
and blobs, see Naming and Referencing Containers, Blobs, and Metadata.
Call the create_container method to actually create the container in your storage account.
Add this code to the end of the try block:
Python
‘ntps:leam microsof. comier-uslazure/storagetolobs/storage-quickstar-blobs-python tabs=managed-identty2s2Croles-azure-portals2Csignin-azu... 8/13‘uo2i24, 5:13 PM (Quickstart Azure Blob Storage client library for Python - Azure Storage | MicrosoftLearn
# Create 2 unique name for the container
container_name = str(uuid.uuida())
# Create the container
container_client = blob_service_client.create_container(container_name)
To learn more about creating a container, and to explore more code samples, see Create a
blob container with Python.
Upload blobs to a container
The following code snippet:
1. Creates a local directory to hold data files.
2. Creates a text file in the local directory.
3. Gets a reference to a BlobClient object by calling the get_blob_client method on the
BlobServiceClient from the Create a container section
4, Uploads the local text file to the blob by calling the upload_blob method.
Add this code to the end of the try block:
Python
# Create a local directory to hold blob data
local_path = "./data"
os.mkdir(local_path)
# Create a file in the local data directory to upload and download
local_file_name = str(uuid.uuida()) + "-txt"
upload_file_path = os.path.join(local_path, local_file_name)
# Write text to the file
file = open(file=upload_file_path, mod.
file.write("Hello, World!")
file.close()
w)
# Create a blob client using the local file name as the name for the blob
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)
# Upload the created file
-ntps:leam microsof. comier-uslazure/storagetblobs/storage-quickstar-blobs-python ?tabs=managed-identty¥s2Croles-azure-portals2Csign-n-azu... 9/131122124, 5:13 PM Quickstart: Azure Blob Storage cent brary fr Python - Azure Storage | Microsoft Leam
with open(filesupload_file_path, mode="rb") as data:
blob_client.upload_blob(data)
To learn more about uploading blobs, and to explore more code samples, see Upload a
blob with Python
List the blobs in a container
List the blobs in the container by calling the list_blobs method. In this case, only one blob
has been added to the container, so the listing operation returns just that one blob.
Add this code to the end of the try block:
Python
print("\nListing blobs...")
# List the blobs in the container
blob_list = container_client.1ist_blobs()
for blob in blob_lis
print("\t" + blob.name)
To learn more about listing blobs, and to explore more code samples, see List blobs with
Python.
Download blobs
Download the previously created blob by calling the download_blob method, The example
code adds a suffix of "DOWNLOAD" to the file name so that you can see both files in local
file system
Add this code to the end of the try block:
Python
# Download the blob to a local file
#t Add ‘DOWNLOAD’ before the .txt extension so you can see both files in the
data directory
download_file_path = os.path.join(local_path, str.replace(local_file_name
a‘ .txt', "DOWNLOAD. txt'))
container_client = blob_service_client.get_container_client(container=
container_name)
print("\nDownloading blob to \n\t" + download_file_path)
nttps:/leam mierosotcomlen-uslazurelstorageolobsistorage-quicksta-blobs-pythontabs=managed-Wentiyik2Croles-azure-portaik2Csign-in-az... 10/13,‘22124, 5:13 PM Quickstart: Azure Blob Storage clent brary for Python - Azure Storage | Microsoft Loam
with open(file=download_file_path, mode="wb") as download_file:
download_file.write(container_client.download_blob(blob.name) .readall())
To learn more about downloading blobs, and to explore more code samples, see Download
a blob with Python
Delete a container
The following code cleans up the resources the app created by removing the entire
container using the delete_container method. You can also delete the local files, if you like
The app pauses for user input by calling input() before it deletes the blob, container, and
local files. Verify that the resources were created correctly before they're deleted.
Add this code to the end of the try block:
Python
# Clean up
print("\nPress the Enter key to begin clean up")
input ()
print("Deleting blob container.
container_client.delete_container()
print("Deleting the local source and downloaded files...")
os .remove(upload_file_path)
os remove (download_file_path)
0s .rmdir(local_path)
print("Done")
To learn more about deleting a container, and to explore more code samples, see Delete
and restore a blob container with Python.
Run the code
This app creates a test file in your local folder and uploads it to Azure Blob Storage. The
‘example then lists the blobs in the container, and downloads the file with a new name. You
‘can compare the old and new files.
nitps:/leam microsotcomlen-uslazurelstorage/olobsistorage-quicksta-blobs-pythontabs=managed-entiyik2Croles-azure-portalik2Csign-in-az.. 11/13,‘1204, 5:13 Pm Cickatart Azure lab Storage cent Roary for Python - Azure Storage | Micros Learn
Navigate to the directory containing the blob-quickstartppy file, then execute the following
python command to run the app:
Console
python blob-quickstart.py
The output of the app is similar to the following example (UUID values omitted for
readability):
Output
Azure Blob Storage Python quickstart sample
Uploading to Azure Storage as blob:
quickstartUUID. txt
Listing blobs...
quickstartUUID. txt
Downloading blob to
-/data/quickstartUUIDDOWNLOAD. txt
Press the Enter key to begin clean up
Deleting blob container.
Deleting the local source and downloaded files...
Done
Before you begin the cleanup process, check your data folder for the two files. You can
compare them and observe that they're identical.
Clean up resources
After you've verified the files and finished testing, press the Enter key to delete the test files
along with the container you created in the storage account. You can also use Azure CLI to
delete resources.
Next steps
In this quickstart, you leamed how to upload, download, and list blobs using Python.
nttps:/leam microsotcomlen-uslazurelstorage/olobsistorage-quickstat-blobs-pythontabs=managed-entiyik2Croles-azure-portaik2Csign-in-az.. 12/13‘yo2i24, 5:13 PM Quickstart Azure Blob Storage client library for Python - Azure Storage | MicrosoftLearn
To see Blob storage sample apps, continue to:
b Storage library for Python samples
* To learn more, see the Azure Blob Storage client libraries for Python.
* For tutorials, samples, quickstarts, and other documentation, visit Azure for Python
Developers.
/ntps:leam microsof. comler-uslazure/storagetblobs/storage-quickstar-blobs-python ?tabs=managed-identty¥s2Croles-azure-portals2Csignn-az... 19/13