-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathS3 access.py
494 lines (232 loc) · 7.53 KB
/
S3 access.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#!/usr/bin/env python
# coding: utf-8
# In[1]:
get_ipython().system('pip install smart_open[s3] ')
# In[190]:
import boto3
# ## Connection
# In[192]:
session = boto3.Session(
region_name='gra',
aws_access_key_id='74f3176ddbea46d88a3fa092a5a64b7b',
aws_secret_access_key='e0a24e4db96f4141ad67d3598f59f9b7')
#Then use the session to get the resource
s3 = session.resource('s3')
# my_bucket = s3.Bucket('stackvidhya')
# for my_bucket_object in my_bucket.objects.all():
# print(my_bucket_object.key)
# In[193]:
s3
# In[194]:
s3 = boto3.resource(
endpoint_url='https://s3.gra.cloud.ovh.net',
service_name='s3',
region_name='gra',
aws_access_key_id='74f3176ddbea46d88a3fa092a5a64b7b',
aws_secret_access_key='e0a24e4db96f4141ad67d3598f59f9b7'
)
s3cli = boto3.client(
endpoint_url='https://s3.gra.cloud.ovh.net',
service_name='s3',
region_name='gra',
aws_access_key_id='74f3176ddbea46d88a3fa092a5a64b7b',
aws_secret_access_key='e0a24e4db96f4141ad67d3598f59f9b7'
)
# endpoint-url
# region = gra
# s3 signature_version=s3v4
#https://s3.gra.cloud.ovh.net
# gn.gonzalez
# ## listing objects
# In[195]:
s3.buckets.all()
# In[196]:
for bucket in s3.buckets.all():
print(bucket)
# In[219]:
objects_lig = s3cli.list_objects(Bucket='lig')
# In[220]:
objects_lig = s3cli.list_objects(Bucket='lig')
get_folders_to_create(objects_lig['Contents'])
# In[199]:
objects = s3cli.list_objects_v2(Bucket='kodicare')
# In[ ]:
s3.download_file(Bucket='lig')
# In[215]:
folders = get_folders_to_create(objects_lig['Contents'])
# In[221]:
objects_lig['Contents']
# In[28]:
'2022-09_fr'
'2022-10_fr'
# In[225]:
print(objects_lig)
# In[224]:
prefix = '2022-07_fr/'
objects_lig = s3cli.list_objects_v2(Bucket='lig', Prefix=prefix)
print(len(objects_lig['Contents']))
## if len < 1000, else: run with paginator.
get_objects(s3cli, page['Contents'])
# In[214]:
page['Contents']
# In[213]:
prefix = '2022-07_fr/'
paginator = s3cli.get_paginator('list_objects')
pages = paginator.paginate(Bucket='lig', Prefix=prefix)
for page in pages:
folders = get_folders_to_create(page['Contents'])
post_create_folders(folders)
get_objects(s3cli, page['Contents'])
# ## Download doc by doc.
# 1. create the required folders.
# 2. run the following code:
#
# Functions :
# ### 1. create the required folders.
# In[204]:
import os
# In[205]:
path_qwant_data = '/data1/home/mrim/gonzagab/Kodicare/qwant-data/'
# In[206]:
get_ipython().system('ls ')
# In[207]:
import numpy as np
# In[208]:
['/'.join(doc['Key'].split('/')[:-1]) for doc in page['Contents']]
# In[209]:
def get_folders_to_create(objects_list):
all_keys = ['/'.join(doc['Key'].split('/')[:-1]) for doc in objects_list]
folders_to_create = np.unique(all_keys)
folders_to_create = np.sort(folders_to_create)
new_folders = []
for folder in folders_to_create:
sub_folders = folder.split('/')
sub_folders = [fd for fd in ['/'.join(sub_folders[:-n]) for n in range(len(sub_folders))] if len(fd)> 0]
for fd_i in sub_folders:
new_folders.append(fd_i)
new_folders = np.sort(np.unique(new_folders))
folders_to_create = np.sort(np.unique(np.append(folders_to_create, new_folders)))
return folders_to_create
# In[210]:
## Create directories
def post_create_folders(folders_to_create):
for folder in folders_to_create:
if not os.path.isdir(folder):
try:
os.makedirs(path_qwant_data + folder)
print("created:" + folder)
except:
print("error " + folder)
else:
print("exists: "+ folder)
# ### 2. run the following code to download
# In[211]:
def get_objects(s3cli, s3_objects):
for obj in s3_objects:
try:
s3cli.download_file('kodicare',obj['Key'], obj['Key'])
except:
print(obj['Key']) ##
# ## Upload file
# In[182]:
def get_all_files(folder):
f = []
fullpath_f = []
for (dirpath, dirnames, filenames) in walk(data_to_upload):
f.extend(filenames)
fullpath_f.extend([dirpath+ '/'+ x for x in filenames])
return fullpath_f
# In[178]:
page['Contents']
# In[93]:
data_to_upload = '/home/mrim/galuscap/projects/qwant-data/2022-06_fr/collection'
data_to_upload1 = '/home/mrim/galuscap/projects/qwant-data/2022-06_fr/split-clean-collection'
# In[94]:
get_ipython().system('ls /home/mrim/galuscap/projects/qwant-data/2022-06_fr/split-clean-collection')
# In[95]:
get_ipython().system('ls /home/mrim/galuscap/projects/qwant-data/2022-06_fr/collection')
# In[76]:
import os
from os import listdir
from os.path import isfile, join
from os import walk
# In[183]:
list_docs_1 = get_all_files(data_to_upload)
list_docs_2 = get_all_files(data_to_upload1)
# In[184]:
list_docs_1
# In[ ]:
(filename, 'lig', filename.replace('/home/mrim/galuscap/projects/qwant-data/', ''))
# In[ ]:
get_ipython().run_cell_magic('time', '', "for filename in list_docs_1:\n res = s3cli.upload_file(filename, 'lig', filename.replace('/home/mrim/galuscap/projects/qwant-data/', ''))\nres")
# In[ ]:
get_ipython().run_cell_magic('time', '', "for filename in list_docs_2:\n res = s3cli.upload_file(filename, 'lig', filename.replace('/home/mrim/galuscap/projects/qwant-data/', ''))\nres")
# In[176]:
## To check !
prefix = '2022-06_fr/split-clean-collection'
paginator = s3cli.get_paginator('list_objects_v2')
pages = paginator.paginate(Bucket='lig', Prefix=prefix)
for page in pages:
folders = get_folders_to_create(page['Contents'])
print(folders)
# get_objects(s3cli, page['Contents'])
# In[128]:
# Upload the file
s3_client = boto3.client('s3')
try:
response = s3cli.upload_file(file_name, bucket, object_name)
except ClientError as e:
logging.error(e)
return False
return True
# ## Done :)
# ### testing code --> to be ignored
# In[42]:
get_ipython().system("cat '2022-09_fr/collection/LOG_kodicare.log'")
# In[ ]:
# In[172]:
len(objects['Contents'])
# In[13]:
get_ipython().system('pwd')
# In[35]:
#to download a file:
s3cli.download_file('kodicare', '2022-04_fr/urls.json', '2022-04_fr/urls.json')
# In[234]:
s3cli.download_file('kodicare', '2022-07_fr/id2query.json', '2022-07_fr/id2query.json')
# In[15]:
get_ipython().run_cell_magic('time', '', "s3cli.download_file('kodicare','2022-04_fr/clickmodel/data/part-00000-cc612722-57a8-44ef-bb31-f4ff9679536e-c000.json', '2022-04_fr/clickmodel/data/part-00000-cc612722-57a8-44ef-bb31-f4ff9679536e-c000.json')")
# In[9]:
bucket.objects.all()
# In[20]:
for bucket in s3.buckets.all():
print(bucket.name)
# In[21]:
bucket
# In[10]:
get_ipython().run_cell_magic('time', '', '# Print out bucket names\nnames = []\nfor bucket in s3.buckets.all():\n print(bucket.name)\n for my_bucket_object in bucket.objects.all():\n names.append(my_bucket_object.key)\n print(my_bucket_object.key)')
# In[17]:
names[-10:]
# In[ ]:
for filename in names:
s3cli.download_file('kodicare',filename, filename)
# In[12]:
names[:10]
# In[43]:
# Load csv file directly into python
obj = s3.Bucket('kodicare').Object('2022-04_fr/collection/collector_kodicare_1.txt').get()
obj
# In[47]:
import json
# In[45]:
import pandas as pd
# In[49]:
#
file_content = obj['Body'].read().decode('utf-8')
json_content = json.loads(file_content)
# foo = json.loads(obj['Body'])
json_content
# In[51]:
json_content
# In[46]:
foo = pd.read_csv(obj['Body'], index_col=0)
# In[ ]: