-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathblobstoragedeleteobject.py
81 lines (63 loc) · 2.39 KB
/
blobstoragedeleteobject.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# blobstoragedeleteobject.py
# It is an example that handles Blob Storage containers on Microsoft Azure.
# Delete a Blob in a Blob Storage container.
# You must provide 2 parameters:
# CONTAINER_NAME = Name of the container
# BLOB_NAME = Name of the blob
import sys
import os
import configparser
from azure.storage.blob import 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 delete_blob(storage_account_conn_str, container_name, blob_name):
"""
Delete a blob in a blob storage container in a storage account.
"""
try:
# Create the container object
container_client = ContainerClient.from_connection_string(conn_str=storage_account_conn_str,
container_name=container_name)
# Delete the blob in the container
print('Deleting blob ... ')
container_client.delete_blob(blob_name)
print('\nDeleted')
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) < 2:
print('Not enough parameters.\n'\
'Proper Usage is: python blobstoragedeleteobject.py '\
'<CONTAINER_NAME> <BLOB_NAME>')
sys.exit(1)
container_name = args[0]
blob_name = args[1]
print('Container name: ' + container_name)
print('Blob name: ' + blob_name)
# Read storage account authentication information
config_dict = load_cfg()
cfg_storage_account_conn_str = config_dict['storageaccountconnectionstring']
# Delete a blob in a blob storage container in a storage account
delete_blob(cfg_storage_account_conn_str, container_name, blob_name)
return
# This is the standard boilerplate that calls the main() function.
if __name__ == '__main__':
main()