Skip to content

Commit 59e8d44

Browse files
committed
Updated Azure SDK version and refactoring
1 parent e51b532 commit 59e8d44

File tree

4 files changed

+49
-39
lines changed

4 files changed

+49
-39
lines changed

azureblobstoragelistall/README.md

+12-14
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This folder contains a Python application example that handles Blob storage on Microsoft Azure.
44

5-
List the blobs in all Blob Storage containers in an Azure storage account.
5+
List information about all Blob Storage containers and the blobs they contain in an Azure storage account.
66

77
## Requirements
88

@@ -14,12 +14,12 @@ List the blobs in all Blob Storage containers in an Azure storage account.
1414
* Python 3
1515
* Azure SDKs for Python
1616

17-
* Install the Azure SDKs for Python.
17+
* You install individual Azure library packages on a per-project basis depending on your needs. It is recommended using Python virtual environments for each project. There is no standalone "SDK" installer for Python.
1818

19-
Install the latest stable version (supports Python 2.7 and 3.x) via pip:
19+
* Install the specified python packages.
2020

2121
```bash
22-
pip install azure
22+
pip install -r requirements.txt
2323
```
2424

2525
## Using the code
@@ -95,26 +95,24 @@ List the blobs in all Blob Storage containers in an Azure storage account.
9595
9696
1. Navigate to `Storage Account`.
9797
2. Select your storage account.
98-
3. Select `Access keys` and you can see your Storage account name, connection strings and account keys.
98+
3. Select `Access keys` and you can see your Storage account connection string.
9999
100100
The connection string looks like this:
101101
102-
```bash
103-
DefaultEndpointsProtocol=https;AccountName=<ACCOUNT_NAME>;AccountKey=<ACCOUNT_KEY>;EndpointSuffix=core.windows.net
104-
```
102+
```bash
103+
DefaultEndpointsProtocol=https;AccountName=<ACCOUNT_NAME>;AccountKey=<ACCOUNT_KEY>;EndpointSuffix=core.windows.net
104+
```
105105
106106
The application configuration is stored in the `app.cfg` file. The file content is:
107107
108108
```bash
109-
[StorageAuthentication]
110-
AccountName=<ACCOUNT_NAME>
111-
AccountKey=<ACCOUNT_KEY>
109+
[Configuration]
110+
StorageAccountConnectionString=<STORAGE_ACCOUNT_CONNECTION_STRING>
112111
```
113112
114-
You must edit the `app.cfg` file and replace the values of:
113+
You must edit the `app.cfg` file and replace the value of:
115114
116-
* `<ACCOUNT_NAME>` by the account name of your storage account.
117-
* `<ACCOUNT_KEY>` by the account key of your storage account.
115+
* `<STORAGE_ACCOUNT_CONNECTION_STRING>` by the connection string of your storage account.
118116
119117
The application uses this information for accessing your Azure storage account.
120118

azureblobstoragelistall/app.cfg

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
[StorageAuthentication]
2-
AccountName=<ACCOUNT_NAME>
3-
AccountKey=<ACCOUNT_KEY>
1+
[Configuration]
2+
StorageAccountConnectionString=<STORAGE_ACCOUNT_CONNECTION_STRING>

azureblobstoragelistall/blobstoragelistall.py

+34-22
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
# -*- coding: utf-8 -*-
33
# blobstoragelistall.py
44
# It is an example that handles Blob Storage containers on Microsoft Azure.
5-
# List the blobs in all Blob Storage containers.
5+
# List information about all Blob Storage containers and the blobs they contain in an Azure storage account.
66

77
import sys
88
import os
99
import configparser
10-
from azure.storage.blob import BlockBlobService, PublicAccess
10+
from azure.storage.blob import BlobServiceClient
1111

1212

13-
def loadcfg():
13+
def load_cfg():
1414
"""
15-
Read storage authentication information from a config file
15+
Read storage account authentication information from a config file
1616
and return the values in a dictionary.
1717
"""
1818
config_file = 'app.cfg'
@@ -23,37 +23,49 @@ def loadcfg():
2323
print('Config file "' + config_file + '" does not exist')
2424
sys.exit(1)
2525

26-
return dict(config.items('StorageAuthentication'))
26+
return dict(config.items('Configuration'))
2727

2828

29-
def main():
30-
31-
# Read storage authentication information
32-
config_dict = loadcfg()
33-
cfg_account_name = config_dict['accountname']
34-
cfg_account_key = config_dict['accountkey']
35-
36-
# Create the BlockBlockService that is used to call the Blob service for the storage account
37-
block_blob_service = BlockBlobService(account_name=cfg_account_name, account_key=cfg_account_key)
29+
def list_containers(storage_account_conn_str):
30+
"""
31+
List the containers and the blobs they contain in a storage account
32+
"""
33+
# Create the BlobServiceClient object which will be used to create a container client
34+
blob_service_client = BlobServiceClient.from_connection_string(storage_account_conn_str)
3835

3936
try:
37+
print('Listing Blob Storage containers ...')
4038
# List the containers
41-
containers_list = block_blob_service.list_containers()
42-
for container in containers_list:
39+
container_list = blob_service_client.list_containers()
40+
for container in container_list:
41+
# Get the container object
42+
container_client = blob_service_client.get_container_client(container)
4343
# List the blobs in the container
44-
print('List of blobs in Blob Storage container "'+ container.name + '":')
45-
blobs_list = block_blob_service.list_blobs(container.name)
46-
for blob in blobs_list:
47-
props = blob.properties
48-
print('- Blob name: ' + blob.name)
49-
print(' size: ', props.content_length)
44+
print('* Blob container "' + container.name + '", list of blobs:')
45+
blob_list = container_client.list_blobs()
46+
for blob in blob_list:
47+
print(' - Blob name: ' + blob.name)
48+
print(' size: ', blob.size)
49+
5050
except Exception as e:
5151
print("\nError:")
5252
print(e)
5353

5454
return
5555

5656

57+
def main():
58+
59+
# Read storage account authentication information
60+
config_dict = load_cfg()
61+
cfg_storage_account_conn_str = config_dict['storageaccountconnectionstring']
62+
63+
# List the containers in the storage account
64+
list_containers(cfg_storage_account_conn_str)
65+
66+
return
67+
68+
5769
# This is the standard boilerplate that calls the main() function.
5870
if __name__ == '__main__':
5971
main()
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
azure-storage-blob

0 commit comments

Comments
 (0)