11
11
12
12
import boto3
13
13
14
- from .base import InputFiles , InputFilesWithSession
14
+ from .base import InputFiles , InputFilesWithSession , s3_client
15
15
16
16
17
17
mod_logger = logging .getLogger (__name__ )
@@ -108,12 +108,10 @@ def get_s3_keys(prefix, site, bucket='fcp-indi'):
108
108
list
109
109
All the keys matching the prefix and site in the S3 bucket
110
110
"""
111
- s3 = boto3 .client ('s3' )
112
-
113
111
# Avoid duplicate trailing slash in prefix
114
112
prefix = prefix .rstrip ('/' )
115
113
116
- response = s3 .list_objects_v2 (
114
+ response = s3_client .list_objects_v2 (
117
115
Bucket = bucket ,
118
116
Prefix = prefix + '/' + site + '/' ,
119
117
)
@@ -127,7 +125,7 @@ def get_s3_keys(prefix, site, bucket='fcp-indi'):
127
125
)
128
126
129
127
while response ['IsTruncated' ]:
130
- response = s3 .list_objects_v2 (
128
+ response = s3_client .list_objects_v2 (
131
129
Bucket = bucket ,
132
130
Prefix = prefix + '/' + site + '/' ,
133
131
ContinuationToken = response ['NextContinuationToken' ]
@@ -230,7 +228,8 @@ def get_subject_id(key):
230
228
return s3_registers
231
229
232
230
233
- def download_register (subject_keys , bucket = 'fcp-indi' , directory = './input' ):
231
+ def download_register (subject_keys , bucket = 'fcp-indi' , directory = './input' ,
232
+ overwrite = False ):
234
233
"""
235
234
Parameters
236
235
----------
@@ -246,6 +245,9 @@ def download_register(subject_keys, bucket='fcp-indi', directory='./input'):
246
245
directory : string
247
246
Local directory to which to save files
248
247
248
+ overwrite : bool
249
+ Flag to overwrite existing files
250
+
249
251
Returns
250
252
-------
251
253
files : InputFiles namedtuple
@@ -256,7 +258,6 @@ def download_register(subject_keys, bucket='fcp-indi', directory='./input'):
256
258
'files' : local file paths,
257
259
'file_type' : 'local',
258
260
"""
259
- s3 = boto3 .client ('s3' )
260
261
subject = subject_keys .subject
261
262
site = subject_keys .site
262
263
@@ -275,10 +276,13 @@ def download_register(subject_keys, bucket='fcp-indi', directory='./input'):
275
276
def download_from_s3 (fname_ , bucket_ , key_ ):
276
277
# Create the directory and file if necessary
277
278
Path (op .dirname (fname_ )).mkdir (parents = True , exist_ok = True )
278
- Path (fname_ ).touch (exist_ok = True )
279
+ try :
280
+ Path (fname_ ).touch (exist_ok = overwrite )
279
281
280
- # Download the file
281
- s3 .download_file (Bucket = bucket_ , Key = key_ , Filename = fname_ )
282
+ # Download the file
283
+ s3_client .download_file (Bucket = bucket_ , Key = key_ , Filename = fname_ )
284
+ except FileExistsError :
285
+ mod_logger .info ('File {fname:s} already exists. Continuing...' )
282
286
283
287
s3keys = subject_keys .files
284
288
files = input_files .files
@@ -366,14 +370,12 @@ def determine_directions(input_files,
366
370
'' .format (files = required_json - set (json_files ))
367
371
)
368
372
369
- s3 = boto3 .client ('s3' )
370
-
371
373
def get_json (json_file ):
372
374
if input_type == 'local' :
373
375
with open (json_file , 'r' ) as fp :
374
376
meta = json .load (fp )
375
377
else :
376
- response = s3 .get_object (
378
+ response = s3_client .get_object (
377
379
Bucket = bucket ,
378
380
Key = json_file ,
379
381
)
0 commit comments