Skip to content

Commit ff60415

Browse files
authored
Merge pull request #223231 from pauljewellmsft/pauljewell-dev-guide-python-containers
Add Python dev guide articles for containers
2 parents d1e9fc9 + 8f48377 commit ff60415

8 files changed

+540
-0
lines changed

articles/storage/blobs/TOC.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,22 @@ items:
702702
href: storage-blob-tags-javascript.md
703703
- name: Manage blob properties and metadata
704704
href: storage-blob-properties-metadata-javascript.md
705+
- name: Python
706+
items:
707+
- name: Get started
708+
href: storage-blob-python-get-started.md
709+
- name: Container actions
710+
items:
711+
- name: Create a container
712+
href: storage-blob-container-create-python.md
713+
- name: Delete and restore containers
714+
href: storage-blob-container-delete-python.md
715+
- name: List containers
716+
href: storage-blob-containers-list-python.md
717+
- name: Manage container leases
718+
href: storage-blob-container-lease-python.md
719+
- name: Manage properties and metadata
720+
href: storage-blob-container-properties-metadata-python.md
705721
- name: Test with a storage emulator
706722
items:
707723
- name: Use the Azurite open-source emulator
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
title: Create a blob container with Python
3+
titleSuffix: Azure Storage
4+
description: Learn how to create a blob container in your Azure Storage account using the Python client library.
5+
services: storage
6+
author: pauljewellmsft
7+
8+
ms.service: storage
9+
ms.topic: how-to
10+
ms.date: 01/24/2023
11+
ms.author: pauljewell
12+
ms.subservice: blobs
13+
ms.devlang: python
14+
ms.custom: devx-track-python, devguide-python
15+
---
16+
17+
# Create a blob container with Python
18+
19+
Blobs in Azure Storage are organized into containers. Before you can upload a blob, you must first create a container. This article shows how to create containers with the [Azure Storage client library for Python](/python/api/overview/azure/storage).
20+
21+
## Container names
22+
23+
A container name must be a valid DNS name, as it forms part of the unique URI used to address the container or its blobs. Follow these rules when naming a container:
24+
25+
- Container names can be between 3 and 63 characters long.
26+
- Container names must start with a letter or number, and can contain only lowercase letters, numbers, and the dash (-) character.
27+
- Two or more consecutive dash characters aren't permitted in container names.
28+
29+
The URI for a container is in this format:
30+
31+
`https://accountname.blob.core.windows.net/containername`
32+
33+
## Create a container
34+
35+
To create a container, call the following method from the [BlobServiceClient](/python/api/azure-storage-blob/azure.storage.blob.blobserviceclient) class:
36+
37+
- [BlobServiceClient.create_container](/python/api/azure-storage-blob/azure.storage.blob.blobserviceclient#azure-storage-blob-blobserviceclient-create-container)
38+
39+
You can also create a container using the following method from the [ContainerClient](/python/api/azure-storage-blob/azure.storage.blob.containerclient) class:
40+
41+
- [ContainerClient.create_container](/python/api/azure-storage-blob/azure.storage.blob.containerclient#azure-storage-blob-containerclient-create-container)
42+
43+
Containers are created immediately beneath the storage account. It's not possible to nest one container beneath another. An exception is thrown if a container with the same name already exists.
44+
45+
The following example creates a container from a `BlobServiceClient` object:
46+
47+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-containers.py" id="Snippet_create_container":::
48+
49+
## Create the root container
50+
51+
A root container serves as a default container for your storage account. Each storage account may have one root container, which must be named *$root*. The root container must be explicitly created or deleted.
52+
53+
You can reference a blob stored in the root container without including the root container name. The root container enables you to reference a blob at the top level of the storage account hierarchy. For example, you can reference a blob in the root container as follows:
54+
55+
`https://accountname.blob.core.windows.net/default.html`
56+
57+
The following example creates a new `ContainerClient` object with the container name $root, then creates the container if it doesn't already exist in the storage account:
58+
59+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-containers.py" id="Snippet_create_root_container":::
60+
61+
## Resources
62+
63+
To learn more about creating a container using the Azure Blob Storage client library for Python, see the following resources.
64+
65+
### REST API operations
66+
67+
The Azure SDK for Python contains libraries that build on top of the Azure REST API, allowing you to interact with REST API operations through familiar Python paradigms. The client library methods for creating a container use the following REST API operation:
68+
69+
- [Create Container](/rest/api/storageservices/create-container) (REST API)
70+
71+
### Code samples
72+
73+
- [View code samples from this article (GitHub)](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/python/blob-devguide-py/blob-devguide-containers.py)
74+
75+
[!INCLUDE [storage-dev-guide-resources-python](../../../includes/storage-dev-guides/storage-dev-guide-resources-python.md)]
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
title: Delete and restore a blob container with Python
3+
titleSuffix: Azure Storage
4+
description: Learn how to delete and restore a blob container in your Azure Storage account using the Python client library.
5+
services: storage
6+
author: pauljewellmsft
7+
8+
ms.service: storage
9+
ms.topic: how-to
10+
ms.date: 01/24/2023
11+
ms.author: pauljewell
12+
ms.subservice: blobs
13+
ms.devlang: python
14+
ms.custom: devx-track-python, devguide-python
15+
---
16+
17+
# Delete and restore a blob container with Python
18+
19+
This article shows how to delete containers with the [Azure Storage client library for Python](/python/api/overview/azure/storage). If you've enabled [container soft delete](soft-delete-container-overview.md), you can restore deleted containers.
20+
21+
## Delete a container
22+
23+
To delete a container in Python, use the following method from the [BlobServiceClient](/python/api/azure-storage-blob/azure.storage.blob.blobserviceclient) class:
24+
25+
- [BlobServiceClient.delete_container](/python/api/azure-storage-blob/azure.storage.blob.blobserviceclient#azure-storage-blob-blobserviceclient-delete-container)
26+
27+
You can also delete a container using the following method from the [ContainerClient](/python/api/azure-storage-blob/azure.storage.blob.containerclient) class:
28+
29+
- [ContainerClient.delete_container](/python/api/azure-storage-blob/azure.storage.blob.containerclient#azure-storage-blob-containerclient-delete-container)
30+
31+
After you delete a container, you can't create a container with the same name for *at least* 30 seconds. Attempting to create a container with the same name will fail with HTTP error code `409 (Conflict)`. Any other operations on the container or the blobs it contains will fail with HTTP error code `404 (Not Found)`.
32+
33+
The following example uses a `BlobServiceClient` object to delete the specified container:
34+
35+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-containers.py" id="Snippet_delete_container":::
36+
37+
The following example shows how to delete all containers that start with a specified prefix:
38+
39+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-containers.py" id="Snippet_delete_container_prefix":::
40+
41+
## Restore a deleted container
42+
43+
When container soft delete is enabled for a storage account, a deleted container and its contents may be recovered within a specified retention period. To learn more about container soft delete, see [Enable and manage soft delete for containers](soft-delete-container-enable.md). You can restore a soft deleted container by calling the following method of the `BlobServiceClient` class:
44+
45+
- [BlobServiceClient.undelete_container](/python/api/azure-storage-blob/azure.storage.blob.blobserviceclient#azure-storage-blob-blobserviceclient-undelete-container)
46+
47+
The following example finds a deleted container, gets the version of that deleted container, and then passes the version into the `undelete_container` method to restore the container.
48+
49+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-containers.py" id="Snippet_restore_container":::
50+
51+
## Resources
52+
53+
To learn more about deleting a container using the Azure Blob Storage client library for Python, see the following resources.
54+
55+
### REST API operations
56+
57+
The Azure SDK for Python contains libraries that build on top of the Azure REST API, allowing you to interact with REST API operations through familiar Python paradigms. The client library methods for deleting or restoring a container use the following REST API operations:
58+
59+
- [Delete Container](/rest/api/storageservices/delete-container) (REST API)
60+
- [Restore Container](/rest/api/storageservices/restore-container) (REST API)
61+
62+
### Code samples
63+
64+
- [View code samples from this article (GitHub)](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/python/blob-devguide-py/blob-devguide-containers.py)
65+
66+
[!INCLUDE [storage-dev-guide-resources-python](../../../includes/storage-dev-guides/storage-dev-guide-resources-python.md)]
67+
68+
### See also
69+
70+
- [Soft delete for containers](soft-delete-container-overview.md)
71+
- [Enable and manage soft delete for containers](soft-delete-container-enable.md)
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
title: Create and manage container leases with Python
3+
titleSuffix: Azure Storage
4+
description: Learn how to manage a lock on a container in your Azure Storage account using the Python client library.
5+
services: storage
6+
author: pauljewellmsft
7+
ms.author: pauljewell
8+
9+
ms.service: storage
10+
ms.topic: how-to
11+
ms.date: 01/24/2023
12+
ms.subservice: blobs
13+
ms.devlang: python
14+
ms.custom: devx-track-python, devguide-python
15+
---
16+
17+
# Create and manage container leases with Python
18+
19+
This article shows how to create and manage container leases using the [Azure Storage client library for Python](/python/api/overview/azure/storage).
20+
21+
A lease establishes and manages a lock on a container for delete operations. The lock duration can be 15 to 60 seconds, or can be infinite. A lease on a container provides exclusive delete access to the container. A container lease only controls the ability to delete the container using the [Delete Container](/rest/api/storageservices/delete-container) REST API operation. To delete a container with an active lease, a client must include the active lease ID with the delete request. All other container operations will succeed on a leased container without the lease ID. If you've enabled [container soft delete](soft-delete-container-overview.md), you can restore deleted containers.
22+
23+
You can use the Python client library to acquire, renew, release and break leases. Lease operations are handled by the [BlobLeaseClient](/python/api/azure-storage-blob/azure.storage.blob.blobleaseclient) class, which provides a client containing all lease operations for [ContainerClient](/python/api/azure-storage-blob/azure.storage.blob.containerclient) and [BlobClient](/python/api/azure-storage-blob/azure.storage.blob.blobclient). To learn more about lease states and when you might perform an operation, see [Lease states and actions](#lease-states-and-actions).
24+
25+
## Acquire a lease
26+
27+
When you acquire a lease, you'll obtain a lease ID that your code can use to operate on the container. To acquire a lease, create an instance of the [BlobLeaseClient](/python/api/azure-storage-blob/azure.storage.blob.blobleaseclient) class, and then use the following method:
28+
29+
- [BlobLeaseClient.acquire](/python/api/azure-storage-blob/azure.storage.blob.blobleaseclient#azure-storage-blob-blobleaseclient-acquire)
30+
31+
You can also acquire a lease using the following method from the [ContainerClient](/python/api/azure-storage-blob/azure.storage.blob.containerclient) class:
32+
33+
- [ContainerClient.acquire_lease](/python/api/azure-storage-blob/azure.storage.blob.containerclient#azure-storage-blob-containerclient-acquire-lease)
34+
35+
The following example acquires a 30-second lease on a container:
36+
37+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-containers.py" id="Snippet_acquire_container_lease":::
38+
39+
## Renew a lease
40+
41+
If your lease expires, you can renew it. To renew a lease, use the following method:
42+
43+
- [BlobLeaseClient.renew](/python/api/azure-storage-blob/azure.storage.blob.blobleaseclient#azure-storage-blob-blobleaseclient-renew)
44+
45+
The following example renews a lease for a container:
46+
47+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-containers.py" id="Snippet_renew_container_lease":::
48+
49+
## Release a lease
50+
51+
You can either wait for a lease to expire or explicitly release it. When you release a lease, other clients can obtain a lease. You can release a lease by using the following method:
52+
53+
- [BlobLeaseClient.release](/python/api/azure-storage-blob/azure.storage.blob.blobleaseclient#azure-storage-blob-blobleaseclient-release)
54+
s
55+
The following example releases the lease on a container:
56+
57+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-containers.py" id="Snippet_release_container_lease":::
58+
59+
## Break a lease
60+
61+
When you break a lease, the lease ends, but other clients can't acquire a lease until the lease period expires. You can break a lease by using the following method:
62+
63+
- [BlobLeaseClient.break_lease](/python/api/azure-storage-blob/azure.storage.blob.blobleaseclient#azure-storage-blob-blobleaseclient-break-lease)
64+
65+
The following example breaks the lease on a container:
66+
67+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-containers.py" id="Snippet_break_container_lease":::
68+
69+
## Lease states and actions
70+
71+
The following diagram shows the five states of a lease, and the commands or events that cause lease state changes.
72+
73+
:::image type="content" source="./media/blob-dev-guide/storage-dev-guide-container-lease.png" alt-text="A diagram showing container lease states and state change triggers." lightbox="./media/blob-dev-guide/storage-dev-guide-container-lease.png":::
74+
75+
The following table lists the five lease states, gives a brief description of each, and lists the lease actions allowed in a given state. These lease actions cause state transitions, as shown in the diagram.
76+
77+
| Lease state | Description | Lease actions allowed |
78+
| --- | --- | --- |
79+
| **Available** | The lease is unlocked and can be acquired. | `acquire` |
80+
| **Leased** | The lease is locked. | `acquire` (same lease ID only), `renew`, `change`, `release`, and `break` |
81+
| **Expired** | The lease duration has expired. | `acquire`, `renew`, `release`, and `break` |
82+
| **Breaking** | The lease has been broken, but the lease will continue to be locked until the break period has expired. | `release` and `break` |
83+
| **Broken** | The lease has been broken, and the break period has expired. | `acquire`, `release`, and `break` |
84+
85+
When a lease expires, the lease ID is maintained by the Blob service until the container is modified or leased again. A client may attempt to renew or release the lease using the expired lease ID. If the request fails, the client knows that the container was leased again, or the container was deleted since the lease was last active.
86+
87+
If a lease expires rather than being explicitly released, a client may need to wait up to one minute before a new lease can be acquired for the container. However, the client can renew the lease with the expired lease ID immediately.
88+
89+
## Resources
90+
91+
To learn more about leasing a container using the Azure Blob Storage client library for Python, see the following resources.
92+
93+
### REST API operations
94+
95+
The Azure SDK for Python contains libraries that build on top of the Azure REST API, allowing you to interact with REST API operations through familiar Python paradigms. The client library methods for leasing a container use the following REST API operation:
96+
97+
- [Lease Container](/rest/api/storageservices/lease-container) (REST API)
98+
99+
### Code samples
100+
101+
- [View code samples from this article (GitHub)](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/python/blob-devguide-py/blob-devguide-containers.py)
102+
103+
[!INCLUDE [storage-dev-guide-resources-python](../../../includes/storage-dev-guides/storage-dev-guide-resources-python.md)]
104+
105+
## See also
106+
107+
- [Managing Concurrency in Blob storage](concurrency-manage.md)

0 commit comments

Comments
 (0)