You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: In this quickstart, you learn how to use the Azure Blob Storage client library for Python to create a container and a blob in Blob (object) storage. Next, you learn how to download the blob to your local computer, and how to list all of the blobs in a container.
# Quickstart: Azure Blob Storage client library for Python
16
17
17
-
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.
18
+
::: zone pivot="blob-storage-quickstart-scratch"
19
+
20
+
> [!NOTE]
21
+
> The **Build from scratch** option walks you step by step through the process of creating a new project, installing packages, writing the code, and running a basic console app. This approach is recommended if you want to understand all the details involved in creating an app that connects to Azure Blob Storage. If you prefer to automate deployment tasks and start with a completed project, choose [Start with a template](storage-quickstart-blobs-python.md?pivots=blob-storage-quickstart-template).
22
+
23
+
::: zone-end
24
+
25
+
::: zone pivot="blob-storage-quickstart-template"
26
+
27
+
> [!NOTE]
28
+
> The **Start with a template** option uses the Azure Developer CLI to automate deployment tasks and starts you off with a completed project. This approach is recommended if you want to explore the code as quickly as possible without going through the setup tasks. If you prefer step by step instructions to build the app, choose [Build from scratch](storage-quickstart-blobs-python.md?pivots=blob-storage-quickstart-scratch).
29
+
30
+
::: zone-end
31
+
32
+
Get started with the Azure Blob Storage client library for Python to manage blobs and containers.
33
+
34
+
::: zone pivot="blob-storage-quickstart-scratch"
35
+
36
+
In this article, you follow steps to install the package and try out example code for basic tasks.
37
+
38
+
::: zone-end
39
+
40
+
::: zone pivot="blob-storage-quickstart-template"
41
+
42
+
In this article, you use the [Azure Developer CLI](/azure/developer/azure-developer-cli/overview) to deploy Azure resources and run a completed console app with just a few commands.
This video shows you how to start using the Azure Blob Storage client library for Python.
22
51
> [!VIDEO f663a554-96ca-4bc3-b3b1-48376a7efbdf]
23
52
24
53
The steps in the video are also described in the following sections.
25
54
55
+
::: zone-end
56
+
26
57
## Prerequisites
27
58
59
+
::: zone pivot="blob-storage-quickstart-scratch"
60
+
28
61
- Azure account with an active subscription - [create an account for free](https://azure.microsoft.com/free/?ref=microsoft.com&utm_source=microsoft.com&utm_medium=docs&utm_campaign=visualstudio)
29
62
- Azure Storage account - [create a storage account](../common/storage-account-create.md)
30
63
-[Python](https://www.python.org/downloads/) 3.8+
31
64
65
+
::: zone-end
66
+
67
+
::: zone pivot="blob-storage-quickstart-template"
68
+
69
+
- Azure subscription - [create one for free](https://azure.microsoft.com/free/)
With [Azure Developer CLI](/azure/developer/azure-developer-cli/install-azd) installed, you can create a storage account and run the sample code with just a few commands. You can run the project in your local development environment, or in a [DevContainer](https://code.visualstudio.com/docs/devcontainers/containers).
120
+
121
+
### Initialize the Azure Developer CLI template and deploy resources
122
+
123
+
From an empty directory, follow these steps to initialize the `azd` template, provision Azure resources, and get started with the code:
124
+
125
+
- Clone the quickstart repository assets from GitHub and initialize the template locally:
- **Environment name**: This value is used as a prefix for all Azure resources created by Azure Developer CLI. The name must be unique across all Azure subscriptions and must be between 3 and 24 characters long. The name can contain numbers and lowercase letters only.
134
+
135
+
- Log in to Azure:
136
+
137
+
```console
138
+
azd auth login
139
+
```
140
+
- Provision and deploy the resources to Azure:
141
+
142
+
```console
143
+
azd up
144
+
```
145
+
146
+
You'll be prompted for the following information:
147
+
148
+
- **Subscription**: The Azure subscription that your resources are deployed to.
149
+
- **Location**: The Azure region where your resources are deployed.
150
+
151
+
The deployment might take a few minutes to complete. The output from the `azd up` command includes the name of the newly created storage account, which you'll need later to run the code.
152
+
153
+
## Run the sample code
154
+
155
+
At this point, the resources are deployed to Azure and the code is ready to run. Follow these steps to update the name of the storage account in the code and run the sample console app:
156
+
157
+
- **Update the storage account name**: In the local directory, edit the file named **blob_quickstart.py**. Find the `<storage-account-name>` placeholder and replace it with the actual name of the storage account created by the `azd up` command. Save the changes.
158
+
- **Run the project**: Execute the following command to run the app: `python blob_quickstart.py`.
159
+
- **Observe the output**: This app creates a test file in your local *data* folder and uploads it to a container in the storage account. The example then lists the blobs in the container and downloads the file with a new name so that you can compare the old and new files.
160
+
161
+
To learn more about how the sample code works, see [Code examples](#code-examples).
162
+
163
+
When you're finished testing the code, see the [Clean up resources](#clean-up-resources) section to delete the resources created by the `azd up` command.
164
+
165
+
::: zone-end
166
+
69
167
## Object model
70
168
71
169
Azure Blob Storage is optimized for storing massive amounts of unstructured data. Unstructured data is data that doesn't adhere to a particular data model or definition, such as text or binary data. Blob storage offers three types of resources:
@@ -95,6 +193,13 @@ These example code snippets show you how to do the following tasks with the Azur
95
193
- [Download blobs](#download-blobs)
96
194
- [Delete a container](#delete-a-container)
97
195
196
+
::: zone pivot="blob-storage-quickstart-template"
197
+
198
+
> [!NOTE]
199
+
> The Azure Developer CLI template includes a file with sample code already in place. The following examples provide detail foreach part of the sample code. The template implements the recommended passwordless authentication method, as describedin the [Authenticate to Azure](#authenticate-to-azure-and-authorize-access-to-blob-data) section. The connection string method is shown as an alternative, but isn't used in the template and isn't recommended for production code.
200
+
201
+
::: zone-end
202
+
98
203
### Authenticate to Azure and authorize access to blob data
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.
324
+
Create a new container in your storage account by calling the [create_container](/python/api/azure-storage-blob/azure.storage.blob.blobserviceclient#azure-storage-blob-blobserviceclient-create-container) method on the `blob_service_client` object. In this example, the code appends a GUID value to the container name to ensure that it's unique.
220
325
221
-
> [!IMPORTANT]
222
-
> Container names must be lowercase. For more information about naming containers and blobs, see [Naming and Referencing Containers, Blobs, and Metadata](/rest/api/storageservices/naming-and-referencing-containers--blobs--and-metadata).
223
-
224
-
Call the [create_container](/python/api/azure-storage-blob/azure.storage.blob.blobserviceclient#create-container-name--metadata-none--public-access-none----kwargs-) method to actually create the container in your storage account.
To learn more about creating a container, and to explore more code samples, see [Create a blob container with Python](storage-blob-container-create-python.md).
231
335
336
+
> [!IMPORTANT]
337
+
> Container names must be lowercase. For more information about naming containers and blobs, see [Naming and Referencing Containers, Blobs, and Metadata](/rest/api/storageservices/naming-and-referencing-containers--blobs--and-metadata).
338
+
232
339
### Upload blobs to a container
233
340
234
-
The following code snippet:
341
+
Upload a blob to a container using [upload_blob](/python/api/azure-storage-blob/azure.storage.blob.blobclient#azure-storage-blob-blobclient-upload-blob). The example code creates a text file in the local *data* directory to upload to the container.
235
342
236
-
1. Creates a local directory to hold data files.
237
-
1. Creates a text file in the local directory.
238
-
1. Gets a reference to a [BlobClient](/python/api/azure-storage-blob/azure.storage.blob.blobclient) object by calling the [get_blob_client](/python/api/azure-storage-blob/azure.storage.blob.containerclient#get-blob-client-blob--snapshot-none-) method on the [BlobServiceClient](/python/api/azure-storage-blob/azure.storage.blob.blobserviceclient) from the [Create a container](#create-a-container) section.
239
-
1. Uploads the local text file to the blob by calling the [upload_blob](/python/api/azure-storage-blob/azure.storage.blob.blobclient#upload-blob-data--blob-type--blobtype-blockblob---blockblob----length-none--metadata-none----kwargs-) method.
To learn more about uploading blobs, and to explore more code samples, see [Upload a blob with Python](storage-blob-upload-python.md).
246
352
247
353
### List the blobs in a container
248
354
249
-
List the blobs in the container by calling the [list_blobs](/python/api/azure-storage-blob/azure.storage.blob.containerclient#list-blobs-name-starts-with-none--include-none----kwargs-) method. In this case, only one blob has been added to the container, so the listing operation returns just that one blob.
355
+
List the blobs in the container by calling the [list_blobs](/python/api/azure-storage-blob/azure.storage.blob.containerclient#azure-storage-blob-containerclient-list-blobs) method. In this case, only one blob has been added to the container, so the listing operation returns just that one blob.
To learn more about listing blobs, and to explore more code samples, see [List blobs with Python](storage-blobs-list-python.md).
256
366
257
367
### Download blobs
258
368
259
-
Download the previously created blob by calling the [download_blob](/python/api/azure-storage-blob/azure.storage.blob.blobclient#download-blob-offset-none--length-none----kwargs-) method. The example code adds a suffix of "DOWNLOAD" to the file name so that you can see both files in local file system.
369
+
Download the previously created blob by calling the [download_blob](/python/api/azure-storage-blob/azure.storage.blob.containerclient#azure-storage-blob-containerclient-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.
To learn more about downloading blobs, and to explore more code samples, see [Download a blob with Python](storage-blob-download-python.md).
266
380
267
381
### Delete a container
268
382
269
-
The following code cleans up the resources the app created by removing the entire container using the [delete_container](/python/api/azure-storage-blob/azure.storage.blob.containerclient#delete-container---kwargs-) method. You can also delete the local files, if you like.
383
+
The following code cleans up the resources the app created by removing the entire container using the [delete_container](/python/api/azure-storage-blob/azure.storage.blob.containerclient#azure-storage-blob-containerclient-delete-container) method. You can also delete the local files, if you like.
270
384
271
385
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.
To learn more about deleting a container, and to explore more code samples, see [Delete and restore a blob container with Python](storage-blob-container-delete-python.md).
278
396
397
+
::: zone pivot="blob-storage-quickstart-scratch"
398
+
279
399
## Run the code
280
400
281
401
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.
@@ -309,10 +429,28 @@ Done
309
429
310
430
Before you begin the cleanup process, check your *data* folder for the two files. You can compare them and observe that they're identical.
311
431
432
+
::: zone-end
433
+
312
434
## Clean up resources
313
435
436
+
::: zone pivot="blob-storage-quickstart-scratch"
437
+
314
438
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](storage-quickstart-blobs-cli.md#clean-up-resources) to delete resources.
315
439
440
+
::: zone-end
441
+
442
+
::: zone pivot="blob-storage-quickstart-template"
443
+
444
+
When you're done with the quickstart, you can clean up the resources you created by running the following command:
445
+
446
+
```console
447
+
azd down
448
+
```
449
+
450
+
You'll be prompted to confirm the deletion of the resources. Enter `y` to confirm.
451
+
452
+
::: zone-end
453
+
316
454
## Next steps
317
455
318
456
In this quickstart, you learned how to upload, download, and list blobs using Python.
0 commit comments