This guide will show you how to perform common scenarios using the Azure Blob storage service. The samples are written in Python and use the Python Azure package. The scenarios covered include uploading, listing, downloading, and deleting blobs.
Azure Blob storage is a service for storing large amounts of unstructured data, such as text or binary data, that can be accessed from anywhere in the world via HTTP or HTTPS. You can use Blob storage to expose data publicly to the world, or to store application data privately.
Common uses of Blob storage include:
The Blob service contains the following components:
Storage Account: All access to Azure Storage is done through a storage account. See Azure Storage Scalability and Performance Targets for details about storage account capacity.
Container: A container provides a grouping of a set of blobs. All blobs must be in a container. An account can contain an unlimited number of containers. A container can store an unlimited number of blobs.
Blob: A file of any type and size. There are two types of blobs that can be stored in Azure Storage: block and page blobs. Most files are block blobs. A single block blob can be up to 200 GB in size. This tutorial uses block blobs. Page blobs, another blob type, can be up to 1 TB in size, and are more efficient when ranges of bytes in a file are modified frequently. For more information about blobs, see Understanding Block Blobs and Page Blobs.
URL format: Blobs are addressable using the following URL format:
The following example URL could be used to address one of the blobs in the diagram above:
To use Azure storage, you'll need a storage account. You can create a storage account by following these steps. (You can also create a storage account by using the Azure service management client library or the service management REST API.)
Log into the Azure Management Portal.
At the bottom of the navigation pane, click NEW.
Click DATA SERVICES, then STORAGE, and then click QUICK CREATE.
In URL, type a subdomain name to use in the URI for the storage account. The entry can contain from 3-24 lowercase letters and numbers. This value becomes the host name within the URI that is used to address Blob, Queue, or Table resources for the subscription.
Choose a Region/Affinity Group in which to locate the storage. If you will be using storage from your Azure application, select the same region where you will deploy your application.
Optionally, you can select the type of replication you desire for your account. Geo-redundant replication is the default and provides maximum durability. For more details on replication options, see Azure Storage Redundancy Options and the Azure Storage Team Blog.
Click CREATE STORAGE ACCOUNT.
The BlobService object lets you work with containers and blobs. The following code creates a BlobService object. Add the following near the top of any Python file in which you wish to programmatically access Azure Storage:
from azure.storage import BlobService
The following code creates a BlobService object using the storage account name and account key. Replace 'myaccount' and 'mykey' with the real account and key.
blob_service = BlobService(account_name='myaccount', account_key='mykey')
All storage blobs reside in a container. You can use a BlobService object to create the container if it doesn't exist:
By default, the new container is private, so you must specify your storage access key (as you did above) to download blobs from this container. If you want to make the files within the container available to everyone, you can create the container and pass the public access level using the following code:
Alternatively, you can modify a container after you have created it using the following code:
After this change, anyone on the Internet can see blobs in a public container, but only you can modify or delete them.
To upload data to a blob, use the put_block_blob_from_path, put_block_blob_from_file, put_block_blob_from_bytes or put_block_blob_from_text methods. They are high-level methods that perform the necessary chunking when the size of the data exceeds 64 MB.
put_block_blob_from_path uploads the contents of a file from the specified path, put_block_blob_from_file uploads the contents from an already opened file/stream. put_block_blob_from_bytes uploads an array of bytes, put_block_blob_from_text uploads the specified text value using the specified encoding (defaults to UTF-8).
The following example uploads the contents of the sunset.png file into the myblob blob.
blob_service.put_block_blob_from_path( 'mycontainer', 'myblob', 'sunset.png', x_ms_blob_content_type='image/png' )
To list the blobs in a container, use the list_blobs method with a for loop to display the name of each blob in the container. The following code outputs the name and url of each blob in a container to the console.
blobs = blob_service.list_blobs('mycontainer') for blob in blobs: print(blob.name) print(blob.url)
To download data from a blob, use get_blob_to_path, get_blob_to_file, get_blob_to_bytes or get_blob_to_text. They are high-level methods that perform the necessary chunking when the size of the data exceeds 64 MB.
The following example demonstrates using get_blob_to_path to download the contents of the myblob blob and store it to the out-sunset.png file:
blob_service.get_blob_to_path('mycontainer', 'myblob', 'out-sunset.png')
Finally, to delete a blob, call delete_blob.
Now that you have learned the basics of blob storage, follow these links to learn about more complex storage tasks.