Skip to content

Commit

Permalink
Fix part uploading in S3
Browse files Browse the repository at this point in the history
  • Loading branch information
bautisflow committed Jun 6, 2024
1 parent b8f2596 commit bd03c66
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/thinger/utils/aws/s3.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,31 +167,31 @@ class S3 {

bool initiate_upload() {

LOG_INFO(fmt::format("[____AWS] Inititiating upload of {0}", file_path_));
LOG_INFO(std::format("[____AWS] Inititiating upload of {0}", file_path_));

std::string payload;
auto res = request("POST", "/"+filename,"uploads=",payload, content_type);

if ( res.error() != httplib::Error::Success || !HttpStatus::isSuccessful(res->status) ) {
LOG_ERROR(fmt::format("[____AWS] Failed initiating upload {0}", filename));
LOG_ERROR(std::format("[____AWS] Failed initiating upload {0}", filename));
return HttpStatus::isSuccessful(res->status);
}

upload_id = XML::get_element_value(res->body, "UploadId");

LOG_INFO(fmt::format("[____AWS] Upload initiated with upload id {0}", upload_id));
LOG_INFO(std::format("[____AWS] Upload initiated with upload id {0}", upload_id));
return HttpStatus::isSuccessful(res->status);
}

bool abort_upload() {

LOG_WARNING(fmt::format("[____AWS] Aborting multipart upload for file: {0}; and upload id: {1}", file_path_, upload_id));
LOG_WARNING(std::format("[____AWS] Aborting multipart upload for file: {0}; and upload id: {1}", file_path_, upload_id));

std::string payload;
auto res = request("DELETE", "/"+filename,"uploadId="+upload_id,payload,"text/plain");

if ( res.error() != httplib::Error::Success || !HttpStatus::isSuccessful(res->status) ) {
LOG_ERROR(fmt::format("[____AWS] Failed aborting upload id {0}", upload_id));
LOG_ERROR(std::format("[____AWS] Failed aborting upload id {0}", upload_id));
}

return HttpStatus::isSuccessful(res->status);
Expand All @@ -208,18 +208,18 @@ class S3 {
// Open and calculate part size
std::ifstream file(file_path_, std::ios::binary);

for (unsigned int i = 0; i < parts; i++) { // send part by part
for (unsigned int i = 1; i <= parts; i++) { // send part by part

file.read(buffer.data(), buffer_size);
std::streamsize bytes_read = file.gcount();

LOG_INFO(fmt::format("[____AWS] Multipart upload of {0}; Uploading part {1} out of {2}", filename, std::to_string(i), std::to_string(parts)));
LOG_INFO(std::format("[____AWS] Multipart upload of {0}; Uploading part {1} out of {2}", filename, std::to_string(i), std::to_string(parts)));

auto res = request("PUT","/"+filename,
"partNumber="+std::to_string(i)+"&uploadId="+upload_id,buffer.data(),bytes_read,content_type);

if ( res.error() != httplib::Error::Success || !HttpStatus::isSuccessful(res->status) ) {
LOG_ERROR(fmt::format("[____AWS] Failed Multipart upload {0}; Part {1} out of {2}", filename, std::to_string(i), std::to_string(parts)));
LOG_ERROR(std::format("[____AWS] Failed Multipart upload {0}; Part {1} out of {2}", filename, std::to_string(i), std::to_string(parts)));
return HttpStatus::isSuccessful(res->status);
}

Expand All @@ -237,7 +237,7 @@ class S3 {

std::string complete_xml = generate_xml_complete_mpu();

LOG_INFO(fmt::format("[____AWS] Finishing upload of {0}", file_path_));
LOG_INFO(std::format("[____AWS] Finishing upload of {0}", file_path_));

return request("POST","/"+filename,"uploadId="+upload_id,complete_xml, "text/xml");
}
Expand Down

0 comments on commit bd03c66

Please sign in to comment.