Skip to content

Commit e51b532

Browse files
committed
Updated Azure SDK version and refactoring
1 parent 99a7497 commit e51b532

File tree

4 files changed

+46
-45
lines changed

4 files changed

+46
-45
lines changed

Diff for: azureblobstoragelist/README.md

+13-15
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 a Blob Storage container in an Azure storage account.
5+
List information about the blobs in a Blob Storage container in an Azure storage account.
66

77
## Requirements
88

@@ -14,12 +14,12 @@ List the blobs in a Blob Storage container 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
@@ -80,7 +80,7 @@ List the blobs in a Blob Storage container in an Azure storage account.
8080
2. Select the `Subscription` in which you want to create the new storage account.
8181
3. Select the `Resource Group` for your storage account.
8282
4. Enter a `name` for your storage account.
83-
5. Select the `Region` for your storage account.
83+
5. Select the `Region` for your storage account.
8484
6. Select the `Performance` to be used.
8585
7. Select the `Redundancy` to be used.
8686
8. Click `Create` to create the storage account.
@@ -95,26 +95,24 @@ List the blobs in a Blob Storage container 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

Diff for: azureblobstoragelist/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>

Diff for: azureblobstoragelist/blobstoragelist.py

+30-27
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
# -*- coding: utf-8 -*-
33
# blobstoragelist.py
44
# It is an example that handles Blob Storage containers on Microsoft Azure.
5-
# List the blobs in a Blob Storage container.
5+
# List information about the blobs in a Blob Storage container in an Azure storage account.
66
# You must provide 1 parameter:
77
# CONTAINER_NAME = Name of the container
88

99
import sys
1010
import os
1111
import configparser
12-
from azure.storage.blob import BlockBlobService, PublicAccess
12+
from azure.storage.blob import ContainerClient
1313

1414

15-
def loadcfg():
15+
def load_cfg():
1616
"""
17-
Read storage authentication information from a config file
17+
Read storage account authentication information from a config file
1818
and return the values in a dictionary.
1919
"""
2020
config_file = 'app.cfg'
@@ -25,7 +25,27 @@ def loadcfg():
2525
print('Config file "' + config_file + '" does not exist')
2626
sys.exit(1)
2727

28-
return dict(config.items('StorageAuthentication'))
28+
return dict(config.items('Configuration'))
29+
30+
31+
def list_container_blobs(storage_account_conn_str, container_name):
32+
"""
33+
List the blobs in a container in a storage account
34+
"""
35+
try:
36+
# Create the container object
37+
container_client = ContainerClient.from_connection_string(conn_str=storage_account_conn_str,
38+
container_name=container_name)
39+
# List the blobs in the container
40+
print('List of blobs in Blob Storage container "'+ container_name + '":')
41+
blob_list = container_client.list_blobs()
42+
for blob in blob_list:
43+
print('- Blob name: ' + blob.name)
44+
print(' size: ', blob.size)
45+
46+
except Exception as e:
47+
print("\nError:")
48+
print(e)
2949

3050

3151
def main():
@@ -39,30 +59,13 @@ def main():
3959
sys.exit(1)
4060

4161
container_name = args[0]
42-
print('Container name: ' + container_name)
43-
44-
# Read storage authentication information
45-
config_dict = loadcfg()
46-
cfg_account_name = config_dict['accountname']
47-
cfg_account_key = config_dict['accountkey']
4862

49-
# Create the BlockBlockService that is used to call the Blob service for the storage account
50-
block_blob_service = BlockBlobService(account_name=cfg_account_name, account_key=cfg_account_key)
63+
# Read storage account authentication information
64+
config_dict = load_cfg()
65+
cfg_storage_account_conn_str = config_dict['storageaccountconnectionstring']
5166

52-
try:
53-
# List the blobs in the container
54-
if block_blob_service.exists(container_name):
55-
print('List of blobs in Blob Storage container "'+ container_name + '":')
56-
blobs_list = block_blob_service.list_blobs(container_name)
57-
for blob in blobs_list:
58-
props = blob.properties
59-
print('- Blob name: ' + blob.name)
60-
print(' size: ', props.content_length)
61-
else:
62-
print('\nError: Blob Storage container "' + container_name + '" does NOT exist.')
63-
except Exception as e:
64-
print("\nError:")
65-
print(e)
67+
# List the blobs in the container
68+
list_container_blobs(cfg_storage_account_conn_str, container_name)
6669

6770
return
6871

Diff for: azureblobstoragelist/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
azure-storage-blob

0 commit comments

Comments
 (0)