-
Notifications
You must be signed in to change notification settings - Fork 32
/
parallel_enumerate_containers.py
283 lines (200 loc) · 8.57 KB
/
parallel_enumerate_containers.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#
# parallel_enumerate_containers.py
#
# Enumerate all blobs in all containers in a storage account, using one
# thread/process per container.
#
# Creates one output file per container, with each row formatted as [name] \t [size] \t [access-tier].
#
#%% Constants and imports
import os
import sys
import time
import argparse
import multiprocessing
from azure.storage.blob import BlobServiceClient
# Assumes that the parent folder of the ai4eutils repo is on the PYTHONPATH
#
# import sys; sys.path.append('/home/dmorris/git/ai4eutils')
# export PYTHONPATH="$PYTHONPATH:/home/dmorris/git/ai4eutils"
import path_utils
n_blobs_per_page = 5000
n_print = 10000
# Toggles between threads (True) and processes (False)
use_threads = False
verbose = False
# This is a bit of a hack, but it has a *massive* impact on performance and on
# minimizing storage-account-level throttling. So... don't set this to zero.
sleep_time_per_page = 0.001
# Limit the number of files to enumerate per thread; used only for debugging
debug_max_files = -1
get_sizes = True
get_access_tiers = True
#%% List containers in a storage account
def list_containers(account_name,sas_token,required_string=None):
# Break URL into URL and token
if not sas_token.startswith('?'):
sas_token = '?' + sas_token
storage_account_url_blob = 'https://' + account_name + '.blob.core.windows.net'
blob_service_client = BlobServiceClient(account_url=storage_account_url_blob,
credential=sas_token)
container_iter = blob_service_client.list_containers(include_metadata=False)
containers = []
for container in container_iter:
name = container['name']
if required_string is None or required_string in name:
containers.append(name)
elif required_string is not None:
print('Skipping container {}'.format(name))
print('Enumerated {} containers:'.format(len(containers)))
print(containers)
return containers
#%% Multiprocessing init
def pinit(c):
global cnt
cnt = c
class Counter(object):
def __init__(self, total):
# 'i' means integer
self.val = multiprocessing.Value('i', 0)
self.total = multiprocessing.Value('i', total)
self.last_print = multiprocessing.Value('i', 0)
def increment(self, n=1):
b_print = False
with self.val.get_lock():
self.val.value += n
if ((self.val.value - self.last_print.value) >= n_print):
self.last_print.value = self.val.value
b_print = True
if b_print:
total_string = ''
if self.total.value > 0:
total_string = ' of {}'.format(self.total.value)
print('{}: iteration {}{}'.format(time.strftime("%Y-%m-%d %H:%M:%S"),
self.val.value,total_string),flush=True)
@property
def value(self):
return self.val.value
def last_print_value(self):
return self.last_print.value
pinit(Counter(-1))
#%% Enumeration function
def list_blobs_in_container(container_name,account_name,sas_token,output_folder,prefix=None):
if not sas_token.startswith('?'):
sas_token = '?' + sas_token
storage_account_url_blob = 'https://' + account_name + '.blob.core.windows.net'
# prefix = prefixes[0]; print(prefix)
print('Starting enumeration for container {}'.format(container_name))
# Open the output file
fn = path_utils.clean_filename(container_name) + '.log'
output_file = os.path.join(output_folder,fn)
# Create the container
blob_service_client = BlobServiceClient(
account_url=storage_account_url_blob,
credential=sas_token)
container_client = blob_service_client.get_container_client(container_name)
# Enumerate
with open(output_file,'w') as output_f:
continuation_token = ''
hit_debug_limit = False
i_blob = 0
while (continuation_token is not None) and (not hit_debug_limit):
blobs_iter = container_client.list_blobs(
name_starts_with=prefix,
results_per_page=n_blobs_per_page).by_page(
continuation_token=continuation_token)
blobs = next(blobs_iter)
n_blobs_this_page = 0
for blob in blobs:
i_blob += 1
n_blobs_this_page += 1
if (debug_max_files > 0) and (i_blob > debug_max_files):
print('Hit debug path limit for prefix {}'.format(prefix))
i_blob -= 1
hit_debug_limit = True
break
else:
size_string = ''
if get_sizes:
size_string = '\t' + str(blob.size)
tier_string = ''
if get_access_tiers:
s = blob.blob_tier
# This typically indicates a GPv1 Storage Account, with no tiering support
if s is None:
s = 'Unknown'
tier_string = '\t' + s
output_f.write(blob.name + size_string + tier_string + '\n')
# print('Enumerated {} blobs'.format(n_blobs_this_page))
cnt.increment(n=n_blobs_this_page)
continuation_token = blobs_iter.continuation_token
if sleep_time_per_page > 0:
time.sleep(sleep_time_per_page)
# ...while we're enumerating
# ...with open(output_file)
print('Finished enumerating {} blobs for container {}'.format(
i_blob,container_name))
#%% Thread-based implementation
from threading import Thread
def list_blobs_threads(account_name,sas_token,containers,output_folder):
all_threads = []
for container_name in containers:
# print('Starting thread for prefix {}'.format(s))
t = Thread(name=container_name,target=list_blobs_in_container,
args=(container_name,account_name,sas_token,output_folder,))
t.daemon = False
t.start()
all_threads.append(t)
for t in all_threads:
t.join()
# print('Thread {} finished'.format(t.name))
#%% Process-based implementation
from multiprocessing import Process
def list_blobs_processes(account_name,sas_token,containers,output_folder):
all_processes = []
for container_name in containers:
# print('Starting process for prefix {}'.format(s))
p = Process(name=container_name,target=list_blobs_in_container,
args=(container_name,account_name,sas_token,output_folder,))
p.daemon = False
p.start()
all_processes.append(p)
for p in all_processes:
p.join()
# print('Process {} finished'.format(p.name))
#%% Main function
def list_blobs_in_all_containers(account_name,sas_token,output_folder):
containers = list_containers(account_name,sas_token)
os.makedirs(output_folder,exist_ok=True)
pinit(Counter(-1))
if use_threads:
list_blobs_threads(account_name,sas_token,containers,output_folder)
else:
list_blobs_processes(account_name,sas_token,containers,output_folder)
#%% Command-line driver
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser = argparse.ArgumentParser(
description='Enumerate blobs in all containers in a storage account, using one thread/process per container.')
parser.add_argument(
'account_name',
help='Name of the target storage account')
parser.add_argument(
'sas_token',
help='Read-/list-capable, account-level SAS token to the target storage account')
parser.add_argument(
'output_folder',
help='Output folder; one flat file per container will be written to this folder')
if len(sys.argv[1:]) == 0:
parser.print_help()
parser.exit()
args = parser.parse_args()
list_blobs_in_all_containers(args.account_name,args.sas_token,args.output_folder)
#%% Interactive driver
if False:
pass
#%%
account_name = ''
sas_token = '?sv='
output_folder = r'c:\temp\enumeration-test'
list_blobs_in_all_containers(args.account_name,args.sas_token,args.output_folder)