Skip to content

Commit 43260f7

Browse files
authored
Upload JSON data to bucket key argument (#10)
1 parent 16f8345 commit 43260f7

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

combine_data.py

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
{ "oc" : [5] },
4949
{ "sa" : [6] }
5050
]
51-
51+
5252
def create_args():
5353
"""Create and return argparser with arguments."""
5454

@@ -66,6 +66,10 @@ def create_args():
6666
"--uploadbucket",
6767
type=str,
6868
help="Name of S3 bucket to upload JSON files to.")
69+
arg_parser.add_argument("-k",
70+
"--bucketkey",
71+
type=str,
72+
help="Name of prefix to upload JSON files to.")
6973
arg_parser.add_argument("-s",
7074
"--sword_version",
7175
type=str,
@@ -104,7 +108,7 @@ def get_logger():
104108

105109
def combine_continents(continents, data_dir, sword_version,expanded, logger):
106110
"""Combine continent-level data in to global data.
107-
111+
108112
Parameters
109113
----------
110114
continents: list
@@ -115,7 +119,7 @@ def combine_continents(continents, data_dir, sword_version,expanded, logger):
115119
Dictionary of global data lists
116120
logger: logger
117121
Logger instance to use for logging statements
118-
122+
119123
Returns
120124
-------
121125
dict
@@ -190,29 +194,28 @@ def create_basin_data(data_dir, basin_id, base_reaches, sword_version):
190194
"sos": f"{continent_codes[str(basin_id)[0]]}_sword_v{sword_version}_SOS_priors.nc"
191195
}
192196

193-
def upload(json_file_list, upload_bucket, input_dir, expanded, logger):
197+
def upload(json_file_list, upload_bucket, bucket_key, input_dir, expanded, logger):
194198
"""Upload JSON files to S3 bucket."""
195-
199+
196200
s3 = boto3.client("s3")
197-
date_prefix = datetime.datetime.now().strftime("%Y%m%dT%H%M%S")
198201
try:
199202
for json_file in json_file_list:
200203
json_file = pathlib.Path(json_file)
201204
if json_file.name == "expanded_reaches_of_interest.json": continue
202-
203-
# Upload under date prefix
205+
206+
# Upload under bucket key
204207
s3.upload_file(str(json_file),
205208
upload_bucket,
206-
f"{date_prefix}/{json_file.name}",
209+
f"{bucket_key}/{json_file.name}",
207210
ExtraArgs={"ServerSideEncryption": "aws:kms"})
208-
logger.info(f"Uploaded {json_file} to {upload_bucket}.")
211+
logger.info(f"Uploaded {json_file} to {upload_bucket}/{bucket_key}.")
209212
# Upload to root of bucket
210213
s3.upload_file(str(json_file),
211214
upload_bucket,
212215
json_file.name,
213216
ExtraArgs={"ServerSideEncryption": "aws:kms"})
214-
logger.info(f"Uploaded {json_file} to {upload_bucket}/{date_prefix}.")
215-
217+
logger.info(f"Uploaded {json_file} to {upload_bucket}.")
218+
216219
# Upload expanded reaches of interest
217220
expanded_roi = pathlib.Path(input_dir).joinpath("expanded_reaches_of_interest.json")
218221
if expanded_roi.exists():
@@ -224,28 +227,28 @@ def upload(json_file_list, upload_bucket, input_dir, expanded, logger):
224227
if not expanded and expanded_roi.exists():
225228
s3.upload_file(str(expanded_roi),
226229
upload_bucket,
227-
f"{date_prefix}/{expanded_roi.name}",
230+
f"{bucket_key}/{expanded_roi.name}",
228231
ExtraArgs={"ServerSideEncryption": "aws:kms"})
229-
logger.info(f"Uploaded {expanded_roi} to {upload_bucket}/{date_prefix}.")
230-
232+
logger.info(f"Uploaded {expanded_roi} to {upload_bucket}/{bucket_key}.")
233+
231234
except botocore.exceptions.ClientError as e:
232235
raise e
233236

234237
def combine_data():
235238
"""Combine continent-level JSON files into global files."""
236-
239+
237240
start = datetime.datetime.now()
238-
241+
239242
# Get logger
240243
logger = get_logger()
241-
244+
242245
# Command line arguments
243246
arg_parser = create_args()
244247
args = arg_parser.parse_args()
245-
248+
246249
for arg in vars(args):
247250
logger.info("%s: %s", arg, getattr(args, arg))
248-
251+
249252
# Load continents
250253
continents = [
251254
"af",
@@ -258,19 +261,19 @@ def combine_data():
258261

259262
# Combine continent-level data
260263
json_file_list = combine_continents(continents, args.datadir, args.sword_version, args.expanded, logger)
261-
264+
262265
# Upload JSON files to S3
263266
if args.uploadbucket:
264267
try:
265-
upload(json_file_list, args.uploadbucket, args.datadir, args.expanded, logger)
268+
upload(json_file_list, args.uploadbucket, args.bucketkey, args.datadir, args.expanded, logger)
266269
except botocore.exceptions.ClientError as e:
267270
logger.error(e)
268271
logger.info("System exiting.")
269272
sys.exit(1)
270-
273+
271274
end = datetime.datetime.now()
272275
logger.info(f"Execution time: {end - start}")
273-
274-
276+
277+
275278
if __name__ == "__main__":
276279
combine_data()

0 commit comments

Comments
 (0)