-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathblobstoragemove.py
128 lines (99 loc) · 4.91 KB
/
blobstoragemove.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/python
# -*- coding: utf-8 -*-
# blobstoragemove.py
# It is an example that handles Blob Storage containers on Microsoft Azure.
# Copy a blob from a Blob Storage container to another Blob Storage container.
# You must provide 3 parameters:
# SOURCE_CONTAINER = Source container name
# SOURCE_BLOB = Source blob name
# DESTINATION_CONTAINER = Destination container name
import sys
import os
import time
import configparser
from azure.storage.blob import BlobClient, BlobLeaseClient, ContainerClient
def load_cfg():
"""
Read storage account authentication information from a config file
and return the values in a dictionary.
"""
config_file = 'app.cfg'
if os.path.exists(config_file):
config = configparser.RawConfigParser()
config.read(config_file)
else:
print('Config file "' + config_file + '" does not exist')
sys.exit(1)
return dict(config.items('Configuration'))
def move_blob(storage_account_conn_str, source_container_name, source_blob_name, dest_container_name):
"""
Move a blob in a blob storage container to another blob storage container in a storage account.
"""
try:
# Create the blob object representing the source
source_blob_client = BlobClient.from_connection_string(conn_str=storage_account_conn_str,
container_name=source_container_name,
blob_name=source_blob_name)
# Create the blob object representing the destination
dest_blob_client = BlobClient.from_connection_string(conn_str=storage_account_conn_str,
container_name=dest_container_name,
blob_name=source_blob_name)
print('Moving a Blob from a Blob container to another one ... ')
# Lease the source blob for the copy operation
# to prevent another client from modifying it.
lease = BlobLeaseClient(source_blob_client)
lease.acquire()
# Get the source blob's properties and display the lease state.
source_props = source_blob_client.get_blob_properties()
print("Lease state: " + source_props.lease.state)
# Start the copy operation.
dest_blob_client.start_copy_from_url(source_blob_client.url)
# Get the destination blob's properties to check the copy status.
properties = dest_blob_client.get_blob_properties()
copy_props = properties.copy
# Display the copy status.
print("Copy status: " + copy_props["status"])
print("Copy progress: " + copy_props["progress"])
print("Completion time: " + str(copy_props["completion_time"]))
print("Total bytes: " + str(properties.size))
if (source_props.lease.state == "leased"):
# Break the lease on the source blob.
lease.break_lease()
# Update the destination blob's properties to check the lease state.
source_props = source_blob_client.get_blob_properties()
print("Lease state: " + source_props.lease.state)
# Create the container object representing the source
source_container_client = ContainerClient.from_connection_string(conn_str=storage_account_conn_str,
container_name=source_container_name)
# Delete the blob in the source container
source_container_client.delete_blob(source_blob_name)
print('\nMoved')
except Exception as e:
print("\nError:")
print(e)
return
def main():
# Make a list of command line arguments, omitting the [0] element
# which is the script itself.
args = sys.argv[1:]
if len(args) < 3:
print('Not enough parameters.\n'\
'Proper Usage is: python blobstoragemove.py '\
'<SOURCE_CONTAINER> <SOURCE_BLOB> <DESTINATION_CONTAINER>')
sys.exit(1)
source_container_name = args[0]
print('Source container name: ' + source_container_name)
source_blob_name = args[1]
print('Source blob name: ' + source_blob_name)
dest_container_name = args[2]
print('Destination container name: ' + dest_container_name)
dest_blob_name = source_blob_name
# Read storage account authentication information
config_dict = load_cfg()
cfg_storage_account_conn_str = config_dict['storageaccountconnectionstring']
# Move a blob in a blob storage container to another blob storage container in a storage account
move_blob(cfg_storage_account_conn_str, source_container_name, source_blob_name, dest_container_name)
return
# This is the standard boilerplate that calls the main() function.
if __name__ == '__main__':
main()