Skip to content

Commit f85b18d

Browse files
committed
Updated Azure SDK version and refactoring
1 parent 7f55979 commit f85b18d

File tree

4 files changed

+84
-55
lines changed

4 files changed

+84
-55
lines changed

azureblobstoragecopy/README.md

+16-18
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ Copy a Blob from a Blob Storage container to another Blob Storage container in a
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 @@ Copy a Blob from a Blob Storage container to another Blob Storage container in a
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-
* `<SOURCE_CONTAINER>` = Source container name
126-
* `<SOURCE_BLOB>` = Source blob name
127-
* `<DESTINATION_CONTAINER>` = Destination container name
123+
* `<SOURCE_CONTAINER>` by source container name.
124+
* `<SOURCE_BLOB>` by source blob name.
125+
* `<DESTINATION_CONTAINER>` by destination container name.
128126
129127
Run application:
130128
131129
```bash
132-
python blobstoragecopy.py source-container source-blob destination-container
130+
python blobstoragecopy.py <SOURCE_CONTAINER> <SOURCE_BLOB> <DESTINATION_CONTAINER>
133131
```
134132
135133
* Test the application.

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

azureblobstoragecopy/blobstoragecopy.py

+65-34
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
import os
1313
import time
1414
import configparser
15-
from azure.storage.blob import BlockBlobService, PublicAccess
15+
from azure.storage.blob import BlobClient, BlobLeaseClient
1616

1717

18-
def loadcfg():
18+
def load_cfg():
1919
"""
20-
Read storage authentication information from a config file
20+
Read storage account authentication information from a config file
2121
and return the values in a dictionary.
2222
"""
2323
config_file = 'app.cfg'
@@ -28,7 +28,63 @@ def loadcfg():
2828
print('Config file "' + config_file + '" does not exist')
2929
sys.exit(1)
3030

31-
return dict(config.items('StorageAuthentication'))
31+
return dict(config.items('Configuration'))
32+
33+
34+
def copy_blob(storage_account_conn_str, source_container_name, source_blob_name, dest_container_name):
35+
"""
36+
Copy a blob in a blob storage container to another blob storage container in a storage account.
37+
"""
38+
try:
39+
# Create the blob object representing the source
40+
source_blob_client = BlobClient.from_connection_string(conn_str=storage_account_conn_str,
41+
container_name=source_container_name,
42+
blob_name=source_blob_name)
43+
44+
# Create the blob object representing the destination
45+
dest_blob_client = BlobClient.from_connection_string(conn_str=storage_account_conn_str,
46+
container_name=dest_container_name,
47+
blob_name=source_blob_name)
48+
49+
print('Copying a Blob from a Blob container to another one ... ')
50+
51+
# Lease the source blob for the copy operation
52+
# to prevent another client from modifying it.
53+
lease = BlobLeaseClient(source_blob_client)
54+
lease.acquire()
55+
56+
# Get the source blob's properties and display the lease state.
57+
source_props = source_blob_client.get_blob_properties()
58+
print("Lease state: " + source_props.lease.state)
59+
60+
# Start the copy operation.
61+
dest_blob_client.start_copy_from_url(source_blob_client.url)
62+
63+
# Get the destination blob's properties to check the copy status.
64+
properties = dest_blob_client.get_blob_properties()
65+
copy_props = properties.copy
66+
67+
# Display the copy status.
68+
print("Copy status: " + copy_props["status"])
69+
print("Copy progress: " + copy_props["progress"])
70+
print("Completion time: " + str(copy_props["completion_time"]))
71+
print("Total bytes: " + str(properties.size))
72+
73+
if (source_props.lease.state == "leased"):
74+
# Break the lease on the source blob.
75+
lease.break_lease()
76+
77+
# Update the destination blob's properties to check the lease state.
78+
source_props = source_blob_client.get_blob_properties()
79+
print("Lease state: " + source_props.lease.state)
80+
81+
print('\nCopied')
82+
83+
except Exception as e:
84+
print("\nError:")
85+
print(e)
86+
87+
return
3288

3389

3490
def main():
@@ -50,37 +106,12 @@ def main():
50106
print('Destination container name: ' + dest_container_name)
51107
dest_blob_name = source_blob_name
52108

53-
# Read storage authentication information
54-
config_dict = loadcfg()
55-
cfg_account_name = config_dict['accountname']
56-
cfg_account_key = config_dict['accountkey']
109+
# Read storage account authentication information
110+
config_dict = load_cfg()
111+
cfg_storage_account_conn_str = config_dict['storageaccountconnectionstring']
57112

58-
# Create the BlockBlockService that is used to call the Blob service for the storage account
59-
block_blob_service = BlockBlobService(account_name=cfg_account_name, account_key=cfg_account_key)
60-
61-
try:
62-
if block_blob_service.exists(source_container_name):
63-
if block_blob_service.exists(source_container_name, source_blob_name):
64-
if block_blob_service.exists(dest_container_name):
65-
print('Copying a Blob from a Blob container to another one ...')
66-
# Get blob url: https://storageaccountname.blob.core.windows.net/containername/blobname
67-
source_blob_url = block_blob_service.make_blob_url(source_container_name, source_blob_name)
68-
# Copy Blob
69-
copy = block_blob_service.copy_blob(dest_container_name, dest_blob_name, source_blob_url)
70-
# Poll for copy completion
71-
while copy.status != 'success':
72-
time.sleep(30)
73-
copy = block_blob_service.get_blob_properties(dest_container_name, dest_blob_name).properties.copy
74-
print('\nCopied')
75-
else:
76-
print('\nError: Destination Blob Storage container "' + source_container_name + '" does NOT exist.')
77-
else:
78-
print('\nError: Blob "' + source_blob_name + '" does NOT exist.')
79-
else:
80-
print('\nError: Source Blob Storage container "' + source_container_name + '" does NOT exist.')
81-
except Exception as e:
82-
print("\nError:")
83-
print(e)
113+
# Copy a blob in a blob storage container to another blob storage container in a storage account
114+
copy_blob(cfg_storage_account_conn_str, source_container_name, source_blob_name, dest_container_name)
84115

85116
return
86117

azureblobstoragecopy/requirements.txt

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

0 commit comments

Comments
 (0)