I currently have two Python generator functions (i.e. using "yield" to read the data) :
- row_generator - reads data from a Salesforce data entity
- data_writer - writes the data to a GZIPPED NDJSON file locally
Currently, after step 2, the file is uploaded to AWS S3. I would like to remove step 2 and write directly to S3.
I'm not sure the best way to do that - I guess it may be to create the GZIPPED NDJSON file in memory, and then to upload this file, but I'm not sure how to do that with generator.
Here is my current "data_writer" function, which I'm looking to replace with something that will write directly to S3:
def data_writer(row_generator):
dt = datetime.now()
formatted_date = datetime.strftime(dt, '%Y%m%d%H%M%S')
filename = f'Contact_{formatted_date}.json.gz'
save_directory = f'C:/SalesforceExtract/'
save_filepath = f'C:/SalesforceExtract/{filename}'
with gzip.open(save_filepath, mode='wt', encoding='UTF8', newline='') as f:
for idx, row_values in enumerate(row_generator, 1):
json.dump(json.dumps(row_values, default=str), f)
f.write("\n")
return filename, save_filepath, idx, save_directory