-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblob.py
More file actions
149 lines (117 loc) · 4.52 KB
/
Copy pathblob.py
File metadata and controls
149 lines (117 loc) · 4.52 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from azure.storage.blob import BlobServiceClient
import config
import uuid
def list_blobs_in_container():
print('\nListing Blobs In Container')
try:
blob_service_client = BlobServiceClient.from_connection_string(config.string)
container_client = blob_service_client.get_container_client(config.container_name)
blob_list = container_client.list_blobs()
for blob in blob_list:
print("*\t" + blob.name)
except Exception as e:
print(e)
exit()
def upload_blob_to_container():
print('\nUpload Blob To Container')
try:
blob_service_client = BlobServiceClient.from_connection_string(config.string)
blob_client = blob_service_client.get_blob_client(container=config.container_name, blob=str(uuid.uuid4())[0:8]+'.jpg')
with open('demo_img.jpg', "rb") as data:
blob_client.upload_blob(data)
except Exception as e:
print(e)
exit()
def download_blob_from_container():
print('\nDownload Blob From Container')
try:
blob_name = input('Enter File Name To Download: ')
blob_service_client = BlobServiceClient.from_connection_string(config.string)
blob_client = blob_service_client.get_blob_client(container=config.container_name, blob=blob_name)
file = open('downloaded.jpg', 'wb')
file.write(blob_client.download_blob().readall())
file.close()
except Exception as e:
print(e)
exit()
def remove_blob_from_container():
print('\nRemove Blob From Container')
try:
print('Under Development')
except Exception as e:
print(e)
exit()
def create_new_blob_container():
from azure.core.exceptions import ResourceExistsError
print('\nCreate Blob Container')
container_name = input('Enter New Container Name: ')
try:
blob_service_client = BlobServiceClient.from_connection_string(config.string)
new_container = blob_service_client.create_container(container_name)
properties = new_container.get_container_properties()
except ResourceExistsError:
print("Container already exists.")
except Exception as e:
print(e)
exit()
def varify_container_existence():
from azure.core.exceptions import ResourceNotFoundError
container_name = input('Enter Container Name: ')
try:
blob_service_client = BlobServiceClient.from_connection_string(config.string)
container_client = blob_service_client.get_container_client(container_name)
for blob in container_client.list_blobs():
print("Found blob: ", blob.name)
except ResourceNotFoundError:
print("\nContainer not found.")
exit()
def delete_container():
from azure.core.exceptions import ResourceNotFoundError
container_name = input('Enter Container Name To Be Removed: ')
try:
blob_service_client = BlobServiceClient.from_connection_string(config.string)
blob_service_client.delete_container(container_name)
except ResourceNotFoundError:
print("Container already deleted.")
def list_all_containers():
try:
blob_service_client = BlobServiceClient.from_connection_string(config.string)
all_containers = blob_service_client.list_containers(include_metadata=True)
for container in all_containers:
print("*\t" + container['name'])
except Exception as e:
print(e)
exit()
def restore_deleted_container():
print('Still Under Dev\n')
exit()
def init():
options = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
msg_1 = '1: List Blobs In Container\n' \
'2: Upload Blob To Container\n' \
'3: Remove Blob From Container\n' \
'4: Download Blob From Container\n' \
'5: Create New Blob Container\n' \
'6: Check If Container Exists\n' \
'7: Remove Container\n' \
'8: List All Containers\n' \
'9: Restore Deleted Container\n' \
print(msg_1)
option = input('Enter Option: ')
if option not in options:
print('Incorrect Option Entered!')
exit()
function_library = {
'1': list_blobs_in_container,
'2': upload_blob_to_container,
'3': remove_blob_from_container,
'4': download_blob_from_container,
'5': create_new_blob_container,
'6': varify_container_existence,
'7': delete_container,
'8': list_all_containers,
'9': restore_deleted_container
}
function = function_library[option]
function()
init()