How to upload and download blobs from Azure Blob Storage with Python
This sample shows how to do the following operations of Storage Blobs with Storage SDK
- Create a Storage Account using the Azure Portal.
- Create a container.
- Upload a file to block blob.
- List blobs.
- Download a blob to file.
- Delete a blob.
- Delete the container.
If you don't have an Azure subscription, create a free account before you begin.
Create a Storage Account using the Azure Portal
Step 1 : Create a new general-purpose Storage Account to use for this tutorial.
- Go to the Azure Portal and log in using your Azure account.
- Select New > Storage > Storage account.
- Select your Subscription.
Resource group, create a new one and give it a unique name.
- Enter a name for your storage Account.
- Select the
Locationto use for your Storage Account.
Account kindto StorageV2(general purpose v2).
Replicationto Locally-redundant storage (LRS).
Secure transfer requiredto Disabled.
- Check Review + create and click Create to create your Storage Account.
Step 2 : Copy and save Connection string.
After your Storage Account is created. Click on it to open it. Select Settings > Access keys > Key1/key, copy the associated Connection string to the clipboard, then paste it into a text editor for later use.
Put the connection string in an environment variable
This solution requires a connection string be stored in an environment variable securely on the machine running the sample. Follow one of the examples below depending on your operating system to create the environment variable. If using Windows close your open IDE or shell and restart it to be able to read the environment variable.
setx AZURE_STORAGE_CONNECTIONSTRING "<YourConnectionString>"
First, clone the repository on your machine:
git clone https://github.com/Azure-Samples/azure-sdk-for-python-storage-blob-upload-download.git
Then, install the dependencies:
pip install -r requirements.txt
Finally, execute the following command to run this sample:
Use latest Storage SDK
The storage SDK package version here is 2.x.x, if you are using the latest version of the storage SDK package, please reference to the following examples:
- blob_samples_hello_world.py - Examples for common Storage Blob tasks:
- Create a container
- Create a block, page, or append blob
- Upload a file to blob
- Download a blob
- Delete a blob
- Delete the container
- blob_samples_enumerate_blobs.py- Examples to enumerate blobs
- List blobs.
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.