|
1 | | -from azure.storage.blob import BlobServiceClient, ContainerClient |
| 1 | +from azure.core.exceptions import ResourceExistsError |
| 2 | +from azure.storage.blob import BlobServiceClient |
2 | 3 | import os |
3 | 4 |
|
4 | 5 | """ |
|
13 | 14 |
|
14 | 15 | SOURCE_FOLDER = "./sample-blobs/" |
15 | 16 |
|
| 17 | + |
16 | 18 | def batch_delete_blobs_sample(local_path): |
17 | | - # Set the connection string and container name values to initialize the Container Client |
| 19 | + # Set the connection string and container name values to initialize the Container Client |
18 | 20 | connection_string = os.getenv('AZURE_STORAGE_CONNECTION_STRING') |
19 | 21 |
|
20 | 22 | blob_service_client = BlobServiceClient.from_connection_string(conn_str=connection_string) |
21 | | - # Create a ContainerClient to use the batch_delete function on a Blob Container |
| 23 | + # Create a ContainerClient to use the batch_delete function on a Blob Container |
22 | 24 | container_client = blob_service_client.get_container_client("mycontainername") |
23 | 25 | try: |
24 | 26 | container_client.create_container() |
25 | 27 | except ResourceExistsError: |
26 | 28 | pass |
27 | | - # Upload blobs |
| 29 | + # Upload blobs |
28 | 30 | for filename in os.listdir(local_path): |
29 | 31 | with open(local_path+filename, "rb") as data: |
30 | 32 | container_client.upload_blob(name=filename, data=data, blob_type="BlockBlob") |
31 | 33 |
|
32 | | - # List blobs in storage account |
| 34 | + # List blobs in storage account |
33 | 35 | blob_list = [b.name for b in list(container_client.list_blobs())] |
34 | | - |
35 | | - # Delete blobs |
| 36 | + |
| 37 | + # Delete blobs |
36 | 38 | container_client.delete_blobs(*blob_list) |
37 | 39 |
|
38 | | -if __name__ == '__main__': |
| 40 | +if __name__ == '__main__': |
39 | 41 | batch_delete_blobs_sample(SOURCE_FOLDER) |
| 42 | + |
0 commit comments