Skip to content

Commit f12c3f0

Browse files
Merge pull request #224591 from pauljewellmsft/pauljewell-dev-guide-python-blobs
Add blob articles for Python developer guide
2 parents a3fdac3 + 02d7e5e commit f12c3f0

10 files changed

+778
-0
lines changed

articles/storage/blobs/TOC.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,24 @@ items:
718718
href: storage-blob-container-lease-python.md
719719
- name: Manage properties and metadata
720720
href: storage-blob-container-properties-metadata-python.md
721+
- name: Blob actions
722+
items:
723+
- name: Upload blobs
724+
href: storage-blob-upload-python.md
725+
- name: Download blobs
726+
href: storage-blob-download-python.md
727+
- name: Copy blobs
728+
href: storage-blob-copy-python.md
729+
- name: List blobs
730+
href: storage-blobs-list-python.md
731+
- name: Delete and restore blobs
732+
href: storage-blob-delete-python.md
733+
- name: Find blobs using tags
734+
href: storage-blob-tags-python.md
735+
- name: Manage blob leases
736+
href: storage-blob-lease-python.md
737+
- name: Manage blob properties and metadata
738+
href: storage-blob-properties-metadata-python.md
721739
- name: Test with a storage emulator
722740
items:
723741
- name: Use the Azurite open-source emulator
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
---
2+
title: Copy a blob with Python
3+
titleSuffix: Azure Storage
4+
description: Learn how to copy a blob in Azure Storage by using the Python client library.
5+
services: storage
6+
author: pauljewellmsft
7+
8+
ms.author: pauljewell
9+
ms.date: 01/25/2023
10+
ms.service: storage
11+
ms.subservice: blobs
12+
ms.topic: how-to
13+
ms.devlang: python
14+
ms.custom: devx-track-python, devguide-python
15+
---
16+
17+
# Copy a blob with Python
18+
19+
This article shows how to copy a blob in a storage account using the [Azure Storage client library for Python](/python/api/overview/azure/storage). It also shows how to abort a pending copy operation.
20+
21+
## About copying blobs
22+
23+
A copy operation can perform any of the following actions:
24+
25+
- Copy a source blob to a destination blob with a different name. The destination blob can be an existing blob of the same blob type (block, append, or page), or can be a new blob created by the copy operation.
26+
- Copy a source blob to a destination blob with the same name, effectively replacing the destination blob. Such a copy operation removes any uncommitted blocks and overwrites the destination blob's metadata.
27+
- Copy a source file in the Azure File service to a destination blob. The destination blob can be an existing block blob, or can be a new block blob created by the copy operation. Copying from files to page blobs or append blobs isn't supported.
28+
- Copy a snapshot over its base blob. By promoting a snapshot to the position of the base blob, you can restore an earlier version of a blob.
29+
- Copy a snapshot to a destination blob with a different name. The resulting destination blob is a writeable blob and not a snapshot.
30+
31+
The source blob for a copy operation may be one of the following types:
32+
- Block blob
33+
- Append blob
34+
- Page blob
35+
- Blob snapshot
36+
- Blob version
37+
38+
If the destination blob already exists, it must be of the same blob type as the source blob. An existing destination blob will be overwritten.
39+
40+
The destination blob can't be modified while a copy operation is in progress. A destination blob can only have one outstanding copy operation. One way to enforce this requirement is to use a blob lease, as shown in the earlier code example. For more information on blob leases, see [Create and manage blob leases with Python](storage-blob-lease-python.md).
41+
42+
The entire source blob or file is always copied. Copying a range of bytes or set of blocks isn't supported. When a blob is copied, its system properties are copied to the destination blob with the same values.
43+
44+
## Copy a blob
45+
46+
To copy a blob, use the following method:
47+
48+
- [BlobClient.start_copy_from_url](/python/api/azure-storage-blob/azure.storage.blob.blobclient#azure-storage-blob-blobclient-start-copy-from-url)
49+
50+
This method returns a dictionary containing *copy_status* and *copy_id*, which can be used to check the status of the copy operation. The *copy_status* property will be 'success' if the copy completed synchronously or 'pending' if the copy has been started asynchronously. For asynchronous copies, the status can be checked by polling the [get_blob_properties](/python/api/azure-storage-blob/azure.storage.blob.blobclient#azure-storage-blob-blobclient-get-blob-properties) method and checking *copy_status*. To force the copy operation to be synchronous, set *requires_sync* to `True`. To learn more about the underlying operation, see [REST API operations](#rest-api-operations).
51+
52+
The following code example gets a `BlobClient` object representing an existing blob and copies it to a new blob in a different container within the same storage account. This example also gets a lease on the source blob before copying so that no other client can modify the blob until the copy is complete and the lease is broken.
53+
54+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-blobs.py" id="Snippet_copy_blob":::
55+
56+
Sample output is similar to:
57+
58+
```console
59+
Source blob lease state: leased
60+
Copy status: success
61+
Copy progress: 5/5
62+
Copy completion time: 2022-11-14T16:53:54Z
63+
Total bytes copied: 5
64+
Source blob lease state: broken
65+
```
66+
67+
## Abort a copy operation
68+
69+
If you have a pending copy operation and need to cancel it, you can abort the operation. Aborting a copy operation results in a destination blob of zero length and full metadata. To learn more about the underlying operation, see [REST API operations](#rest-api-operations).
70+
71+
The metadata for the destination blob will have the new values copied from the source blob or set explicitly during the copy operation. To keep the original metadata from before the copy, make a snapshot of the destination blob before calling one of the copy methods. The final blob will be committed when the copy completes.
72+
73+
To abort a copy operation, use the following method:
74+
75+
- [BlobClient.abort_copy](/python/api/azure-storage-blob/azure.storage.blob.blobclient#azure-storage-blob-blobclient-abort-copy)
76+
77+
The following example stops a pending copy and leaves a destination blob with zero length and full metadata:
78+
79+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-blobs.py" id="Snippet_abort_copy":::
80+
81+
## Resources
82+
83+
To learn more about copying blobs using the Azure Blob Storage client library for Python, see the following resources.
84+
85+
### REST API operations
86+
87+
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 copying blobs use the following REST API operations:
88+
89+
- [Copy Blob From URL](/rest/api/storageservices/copy-blob-from-url) (REST API)
90+
- [Abort Copy Blob](/rest/api/storageservices/abort-copy-blob) (REST API)
91+
92+
### Code samples
93+
94+
- [View code samples from this article (GitHub)](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/python/blob-devguide-py/blob-devguide-blobs.py)
95+
96+
[!INCLUDE [storage-dev-guide-resources-python](../../../includes/storage-dev-guides/storage-dev-guide-resources-python.md)]
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
title: Delete and restore a blob with Python
3+
titleSuffix: Azure Storage
4+
description: Learn how to delete and restore a blob in your Azure Storage account using the Python client library
5+
services: storage
6+
author: pauljewellmsft
7+
8+
ms.author: pauljewell
9+
ms.date: 01/25/2023
10+
ms.service: storage
11+
ms.subservice: blobs
12+
ms.topic: how-to
13+
ms.devlang: python
14+
ms.custom: devx-track-python, devguide-python
15+
---
16+
17+
# Delete and restore a blob with Python
18+
19+
This article shows how to delete blobs using the [Azure Storage client library for Python](/python/api/overview/azure/storage). If you've enabled [soft delete for blobs](soft-delete-blob-overview.md), you can restore deleted blobs during the retention period.
20+
21+
## Delete a blob
22+
23+
To delete a blob, call the following method:
24+
25+
- [BlobClient.delete_blob](/python/api/azure-storage-blob/azure.storage.blob.blobclient#azure-storage-blob-blobclient-delete-blob)
26+
27+
The following example deletes a blob:
28+
29+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-blobs.py" id="Snippet_delete_blob":::
30+
31+
If the blob has any associated snapshots, you must delete all of its snapshots to delete the blob. The following example deletes a blob and its snapshots:
32+
33+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-blobs.py" id="Snippet_delete_blob_snapshots":::
34+
35+
To delete *only* the snapshots and not the blob itself, you can pass the parameter `delete_snapshots="only"`.
36+
37+
## Restore a deleted blob
38+
39+
Blob soft delete protects an individual blob and its versions, snapshots, and metadata from accidental deletes or overwrites by maintaining the deleted data in the system for a specified period of time. During the retention period, you can restore the blob to its state at deletion. After the retention period has expired, the blob is permanently deleted. For more information about blob soft delete, see [Soft delete for blobs](soft-delete-blob-overview.md).
40+
41+
You can use the Azure Storage client libraries to restore a soft-deleted blob or snapshot.
42+
43+
#### Restore soft-deleted objects when versioning is disabled
44+
45+
To restore deleted blobs when versioning is disabled, call the following method:
46+
47+
- [BlobClient.undelete_blob](/python/api/azure-storage-blob/azure.storage.blob.blobclient#azure-storage-blob-blobclient-undelete-blob)
48+
49+
This method restores the content and metadata of a soft-deleted blob and any associated soft-deleted snapshots. Calling this method for a blob that hasn't been deleted has no effect.
50+
51+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-blobs.py" id="Snippet_restore_blob":::
52+
53+
#### Restore soft-deleted objects when versioning is enabled
54+
55+
To restore a soft-deleted blob when versioning is enabled, copy a previous version over the base blob. You can use the following method:
56+
57+
- [start_copy_from_url](/python/api/azure-storage-blob/azure.storage.blob.blobclient#azure-storage-blob-blobclient-start-copy-from-url)
58+
59+
The following code example gets the latest version of a deleted blob, and restores the latest version by copying it to the base blob:
60+
61+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-blobs.py" id="Snippet_restore_blob_version":::
62+
63+
## Resources
64+
65+
To learn more about how to delete blobs and restore deleted blobs using the Azure Blob Storage client library for Python, see the following resources.
66+
67+
### REST API operations
68+
69+
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 blobs and restoring deleted blobs use the following REST API operations:
70+
71+
- [Delete Blob](/rest/api/storageservices/delete-blob) (REST API)
72+
- [Undelete Blob](/rest/api/storageservices/undelete-blob) (REST API)
73+
74+
### Code samples
75+
76+
- [View code samples from this article (GitHub)](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/python/blob-devguide-py/blob-devguide-blobs.py)
77+
78+
[!INCLUDE [storage-dev-guide-resources-python](../../../includes/storage-dev-guides/storage-dev-guide-resources-python.md)]
79+
80+
### See also
81+
82+
- [Soft delete for blobs](soft-delete-blob-overview.md)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
title: Download a blob with Python
3+
titleSuffix: Azure Storage
4+
description: Learn how to download a blob in Azure Storage by using the Python client library.
5+
services: storage
6+
author: pauljewellmsft
7+
8+
ms.author: pauljewell
9+
ms.date: 01/24/2023
10+
ms.service: storage
11+
ms.subservice: blobs
12+
ms.topic: how-to
13+
ms.devlang: python
14+
ms.custom: devx-track-python, devguide-python
15+
---
16+
17+
# Download a blob with Python
18+
19+
This article shows how to download a blob using the [Azure Storage client library for Python](/python/api/overview/azure/storage). You can download a blob by using the following method:
20+
21+
- [BlobClient.download_blob](/python/api/azure-storage-blob/azure.storage.blob.blobclient#azure-storage-blob-blobclient-download-blob)
22+
23+
The `download_blob` method returns a [StorageStreamDownloader](/python/api/azure-storage-blob/azure.storage.blob.storagestreamdownloader) object.
24+
25+
## Download to a file path
26+
27+
The following example downloads a blob to a file path:
28+
29+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-blobs.py" id="Snippet_download_blob_file":::
30+
31+
## Download to a stream
32+
33+
The following example downloads a blob to a stream. In this example, [StorageStreamDownloader.read_into](/python/api/azure-storage-blob/azure.storage.blob.storagestreamdownloader#azure-storage-blob-storagestreamdownloader-readinto) downloads the blob contents to a stream and returns the number of bytes read:
34+
35+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-blobs.py" id="Snippet_download_blob_stream":::
36+
37+
## Download a blob in chunks
38+
39+
The following example downloads a blob and iterates over chunks in the download stream. In this example, [StorageStreamDownloader.chunks](/python/api/azure-storage-blob/azure.storage.blob.storagestreamdownloader#azure-storage-blob-storagestreamdownloader-chunks) returns an iterator, which allows you to read the blob content in chunks:
40+
41+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-blobs.py" id="Snippet_download_blob_chunks":::
42+
43+
## Download to a string
44+
45+
The following example downloads blob contents as text. In this example, the `encoding` parameter is necessary for `readall()` to return a string, otherwise it returns bytes:
46+
47+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-blobs.py" id="Snippet_download_blob_text":::
48+
49+
## Resources
50+
51+
To learn more about how to download blobs using the Azure Blob Storage client library for Python, see the following resources.
52+
53+
### REST API operations
54+
55+
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 downloading blobs use the following REST API operation:
56+
57+
- [Get Blob](/rest/api/storageservices/get-blob) (REST API)
58+
59+
### Code samples
60+
61+
- [View code samples from this article (GitHub)](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/python/blob-devguide-py/blob-devguide-blobs.py)
62+
63+
[!INCLUDE [storage-dev-guide-resources-python](../../../includes/storage-dev-guides/storage-dev-guide-resources-python.md)]

0 commit comments

Comments
 (0)