Skip to content

Commit f96d772

Browse files
Merge pull request #267003 from pauljewellmsft/azd-python
Add azd support to Python quickstart for blobs
2 parents 13eaa85 + 452beff commit f96d772

File tree

1 file changed

+153
-15
lines changed

1 file changed

+153
-15
lines changed

articles/storage/blobs/storage-quickstart-blobs-python.md

Lines changed: 153 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,78 @@ titleSuffix: Azure Storage
44
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.
55
author: pauljewellmsft
66
ms.author: pauljewell
7-
ms.date: 10/24/2022
7+
ms.date: 02/22/2024
88
ms.topic: quickstart
99
ms.service: azure-blob-storage
1010
ms.devlang: python
1111
ms.custom: devx-track-python, mode-api, passwordless-python, ai-video-demo
1212
ai-usage: ai-assisted
13+
zone_pivot_groups: azure-blob-storage-quickstart-options
1314
---
1415

1516
# Quickstart: Azure Blob Storage client library for Python
1617

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.
43+
44+
::: zone-end
1845

1946
[API reference documentation](/python/api/azure-storage-blob) | [Library source code](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-blob) | [Package (PyPi)](https://pypi.org/project/azure-storage-blob/) | [Samples](../common/storage-samples-python.md?toc=/azure/storage/blobs/toc.json#blob-samples)
2047

48+
::: zone pivot="blob-storage-quickstart-scratch"
49+
2150
This video shows you how to start using the Azure Blob Storage client library for Python.
2251
> [!VIDEO f663a554-96ca-4bc3-b3b1-48376a7efbdf]
2352
2453
The steps in the video are also described in the following sections.
2554

55+
::: zone-end
56+
2657
## Prerequisites
2758

59+
::: zone pivot="blob-storage-quickstart-scratch"
60+
2861
- 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)
2962
- Azure Storage account - [create a storage account](../common/storage-account-create.md)
3063
- [Python](https://www.python.org/downloads/) 3.8+
3164

65+
::: zone-end
66+
67+
::: zone pivot="blob-storage-quickstart-template"
68+
69+
- Azure subscription - [create one for free](https://azure.microsoft.com/free/)
70+
- [Python](https://www.python.org/downloads/) 3.8+
71+
- [Azure Developer CLI](/azure/developer/azure-developer-cli/install-azd)
72+
73+
::: zone-end
74+
3275
## Setting up
3376

77+
::: zone pivot="blob-storage-quickstart-scratch"
78+
3479
This section walks you through preparing a project to work with the Azure Blob Storage client library for Python.
3580

3681
### Create the project
@@ -64,8 +109,61 @@ From the project directory, follow steps to create the basic structure of the ap
64109
1. Open a new text file in your code editor.
65110
1. Add `import` statements, create the structure for the program, and include basic exception handling, as shown below.
66111
1. Save the new file as *blob_quickstart.py* in the *blob-quickstart* directory.
112+
67113
:::code language="python" source="~/azure-storage-snippets/blobs/quickstarts/python/app-framework-qs.py":::
68114

115+
::: zone-end
116+
117+
::: zone pivot="blob-storage-quickstart-template"
118+
119+
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:
126+
127+
```console
128+
azd init --template blob-storage-quickstart-python
129+
```
130+
131+
You'll be prompted for the following information:
132+
133+
- **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+
69167
## Object model
70168

71169
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
95193
- [Download blobs](#download-blobs)
96194
- [Delete a container](#delete-a-container)
97195

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 for each part of the sample code. The template implements the recommended passwordless authentication method, as described in 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+
98203
### Authenticate to Azure and authorize access to blob data
99204

100205
[!INCLUDE [storage-quickstart-passwordless-auth-intro](../../../includes/storage-quickstart-passwordless-auth-intro.md)]
@@ -216,66 +321,81 @@ blob_service_client = BlobServiceClient.from_connection_string(connect_str)
216321

217322
### Create a container
218323

219-
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.
220325

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.
326+
::: zone pivot="blob-storage-quickstart-scratch"
225327

226328
Add this code to the end of the `try` block:
227329

330+
::: zone-end
331+
228332
:::code language="python" source="~/azure-storage-snippets/blobs/quickstarts/python/blob-quickstart.py" id="Snippet_CreateContainer":::
229333

230334
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).
231335

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+
232339
### Upload blobs to a container
233340

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.
235342

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.
343+
::: zone pivot="blob-storage-quickstart-scratch"
240344

241345
Add this code to the end of the `try` block:
242346

347+
::: zone-end
348+
243349
:::code language="python" source="~/azure-storage-snippets/blobs/quickstarts/python/blob-quickstart.py" id="Snippet_UploadBlobs":::
244350

245351
To learn more about uploading blobs, and to explore more code samples, see [Upload a blob with Python](storage-blob-upload-python.md).
246352

247353
### List the blobs in a container
248354

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.
356+
357+
::: zone pivot="blob-storage-quickstart-scratch"
250358

251359
Add this code to the end of the `try` block:
252360

361+
::: zone-end
362+
253363
:::code language="python" source="~/azure-storage-snippets/blobs/quickstarts/python/blob-quickstart.py" id="Snippet_ListBlobs":::
254364

255365
To learn more about listing blobs, and to explore more code samples, see [List blobs with Python](storage-blobs-list-python.md).
256366

257367
### Download blobs
258368

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.
370+
371+
::: zone pivot="blob-storage-quickstart-scratch"
260372

261373
Add this code to the end of the `try` block:
262374

375+
::: zone-end
376+
263377
:::code language="python" source="~/azure-storage-snippets/blobs/quickstarts/python/blob-quickstart.py" id="Snippet_DownloadBlobs":::
264378

265379
To learn more about downloading blobs, and to explore more code samples, see [Download a blob with Python](storage-blob-download-python.md).
266380

267381
### Delete a container
268382

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.
270384

271385
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.
272386

387+
::: zone pivot="blob-storage-quickstart-scratch"
388+
273389
Add this code to the end of the `try` block:
274390

391+
::: zone-end
392+
275393
:::code language="python" source="~/azure-storage-snippets/blobs/quickstarts/python/blob-quickstart.py" id="Snippet_CleanUp":::
276394

277395
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).
278396

397+
::: zone pivot="blob-storage-quickstart-scratch"
398+
279399
## Run the code
280400

281401
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
309429

310430
Before you begin the cleanup process, check your *data* folder for the two files. You can compare them and observe that they're identical.
311431

432+
::: zone-end
433+
312434
## Clean up resources
313435

436+
::: zone pivot="blob-storage-quickstart-scratch"
437+
314438
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.
315439

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+
316454
## Next steps
317455

318456
In this quickstart, you learned how to upload, download, and list blobs using Python.

0 commit comments

Comments
 (0)