From ac30c252b20642f9dd0cf830382ae607f4723ce6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Meny?= Date: Thu, 19 May 2022 11:35:23 +0100 Subject: [PATCH] Attempt at fixing S3 error when saving Zip file (#590) --- app/services/exports/case_log_export_service.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/app/services/exports/case_log_export_service.rb b/app/services/exports/case_log_export_service.rb index 4844e85cb..397a7cd94 100644 --- a/app/services/exports/case_log_export_service.rb +++ b/app/services/exports/case_log_export_service.rb @@ -95,18 +95,21 @@ module Exports archive_datetimes = {} case_logs_per_archive.each do |archive, case_logs_to_export| manifest_xml = build_manifest_xml(case_logs_to_export.count) - zip_io = Zip::File.open_buffer(StringIO.new) - zip_io.add("manifest.xml", manifest_xml) + zip_file = Zip::File.open_buffer(StringIO.new) + zip_file.add("manifest.xml", manifest_xml) part_number = 1 case_logs_to_export.each_slice(MAX_XML_RECORDS) do |case_logs_slice| data_xml = build_export_xml(case_logs_slice) part_number_str = "pt#{part_number.to_s.rjust(3, '0')}" - zip_io.add("#{archive}_#{part_number_str}.xml", data_xml) + zip_file.add("#{archive}_#{part_number_str}.xml", data_xml) part_number += 1 end - @storage_service.write_file("#{archive}.zip", zip_io.write_buffer) + # Required by S3 to avoid Aws::S3::Errors::BadDigest + zip_io = zip_file.write_buffer + zip_io.rewind + @storage_service.write_file("#{archive}.zip", zip_io) archive_datetimes[archive] = Time.zone.now end