-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage-solution.py
More file actions
52 lines (42 loc) · 1.91 KB
/
storage-solution.py
File metadata and controls
52 lines (42 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import os
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
# Storage connection string (NOT recommended, used for simplicity for this study)
STORAGE_CONNECTION_STR = os.environ["STORAGE_CONNECTION_STR"]
STORAGE_CONTAINER_NAME = os.environ["STORAGE_CONTAINER_NAME"]
BLOB_NAME = "2022-12-22-09-00.txt"
# Sample code explaining the various client objects and some example methods
def download_blob():
try:
# Create the BlobServiceClient (client for a specific Azure Storage resource)
blob_service_client = BlobServiceClient.from_connection_string(
STORAGE_CONNECTION_STR
)
# Using container name and BlobServiceClient, access a specific container via ContainerClient (client for a specific container)
container_client = blob_service_client.get_container_client(
container=STORAGE_CONTAINER_NAME
)
# Print existing blob from storage container with BlobClient (client for a specific blob)
blob_client = container_client.get_blob_client(blob=BLOB_NAME)
print(blob_client.download_blob().readall())
except Exception as ex:
print("Exception:")
print(ex)
# TODO: Using the Azure SDK reference documentation, complete the list_blobs_in_container() method.
# Hint: Note the relationship between BlobServiceClient, ContainerClient, and BlobClient.
def list_blobs_in_container():
try:
blob_service_client = BlobServiceClient.from_connection_string(
STORAGE_CONNECTION_STR
)
container_client = blob_service_client.get_container_client(
container=STORAGE_CONTAINER_NAME
)
blob_list = container_client.list_blobs()
for blob in blob_list:
print(blob.name)
except Exception as ex:
print("Exception:")
print(ex)
# Code to be executed
print("Trying to list blobs in container:")
list_blobs_in_container()