Skip to content

Commit 47fbde2

Browse files
committed
Updated Azure SDK version and refactoring
1 parent debb416 commit 47fbde2

File tree

4 files changed

+55
-47
lines changed

4 files changed

+55
-47
lines changed

azureblobstorageupload/README.md

+16-18
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ Upload a local file to 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
@@ -95,41 +95,39 @@ Upload a local file to 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
121119
* Run the code.
122120
123-
You must provide 3 parameters:
121+
You must provide 3 parameters, replace the values of:
124122
125-
* `<CONTAINER_NAME>` = Name of the container
126-
* `<BLOB_NAME>` = Blob name in the container
127-
* `<LOCAL_FILE_NAME>` = Local file name
123+
* `<CONTAINER_NAME>` by name of the container.
124+
* `<BLOB_NAME>` by blob name in the container.
125+
* `<LOCAL_FILE_NAME>` by local file name.
128126
129127
Run application:
130128
131129
```bash
132-
python blobstorageupload.py container-example blob-example local-file-example
130+
python blobstorageupload.py <CONTAINER_NAME> <BLOB_NAME> <LOCAL_FILE_NAME>
133131
```
134132
135133
* Test the application.

azureblobstorageupload/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>

azureblobstorageupload/blobstorageupload.py

+36-26
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@
88
# BLOB_NAME = Blob name in the container
99
# LOCAL_FILE_NAME = Local file name
1010

11-
1211
import sys
1312
import os
1413
import configparser
15-
from azure.storage.blob import BlockBlobService, PublicAccess
14+
from azure.storage.blob import BlobServiceClient
1615

1716

18-
def loadcfg():
17+
def load_cfg():
1918
"""
20-
Read storage authentication information from a config file
19+
Read storage account authentication information from a config file
2120
and return the values in a dictionary.
2221
"""
2322
config_file = 'app.cfg'
@@ -28,7 +27,34 @@ def loadcfg():
2827
print('Config file "' + config_file + '" does not exist')
2928
sys.exit(1)
3029

31-
return dict(config.items('StorageAuthentication'))
30+
return dict(config.items('Configuration'))
31+
32+
33+
def upload_blob(storage_account_conn_str, container_name, blob_name, local_file_name):
34+
"""
35+
Upload a Blob to a blob storage container.
36+
"""
37+
try:
38+
# Create the BlobServiceClient object which will be used to create a blob client
39+
blob_service_client = BlobServiceClient.from_connection_string(storage_account_conn_str)
40+
41+
# Create a blob client using the blob name
42+
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
43+
44+
if os.path.exists(local_file_name):
45+
# Upload the local file to the Blob container
46+
print('Uploading the local file to the Blob Storage container ...')
47+
with open(local_file_name, "rb") as data:
48+
blob_client.upload_blob(data)
49+
print("\nUploaded")
50+
else:
51+
print('\nError: Local file "' + local_file_name + '" does NOT exist.')
52+
53+
except Exception as e:
54+
print("\nError:")
55+
print(e)
56+
57+
return
3258

3359

3460
def main():
@@ -49,28 +75,12 @@ def main():
4975
local_file_name = args[2]
5076
print('Local file: ' + local_file_name)
5177

52-
# Read storage authentication information
53-
config_dict = loadcfg()
54-
cfg_account_name = config_dict['accountname']
55-
cfg_account_key = config_dict['accountkey']
56-
57-
# Create the BlockBlockService that is used to call the Blob service for the storage account
58-
block_blob_service = BlockBlobService(account_name=cfg_account_name, account_key=cfg_account_key)
78+
# Read storage account authentication information
79+
config_dict = load_cfg()
80+
cfg_storage_account_conn_str = config_dict['storageaccountconnectionstring']
5981

60-
try:
61-
if block_blob_service.exists(container_name):
62-
if os.path.exists(local_file_name):
63-
# Upload the local file to the Blob container
64-
print('Uploading a local file to a Blob Storage container ...')
65-
block_blob_service.create_blob_from_path(container_name, blob_name, local_file_name)
66-
print("\nUploaded")
67-
else:
68-
print('\nError: Local file "' + local_file_name + '" does NOT exist.')
69-
else:
70-
print('\nError: Blob Storage container "' + container_name + '" does NOT exist.')
71-
except Exception as e:
72-
print("\nError:")
73-
print(e)
82+
# Upload a Blob to a blob storage container
83+
upload_blob(cfg_storage_account_conn_str, container_name, blob_name, local_file_name)
7484

7585
return
7686

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

0 commit comments

Comments
 (0)