Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/aws-cpp-sdk-transfer/source/transfer/TransferManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include <aws/core/platform/FileSystem.h>
#include <aws/core/utils/HashingUtils.h>
#include <aws/core/utils/logging/LogMacros.h>
#include <aws/core/utils/crypto/Hash.h>
#include <aws/core/utils/crypto/CRC32.h>
#include <aws/core/utils/crypto/CRC64.h>
#include <aws/core/utils/memory/AWSMemory.h>
#include <aws/core/utils/memory/stl/AWSStreamFwd.h>
#include <aws/core/utils/memory/stl/AWSStringStream.h>
Expand Down Expand Up @@ -399,6 +402,15 @@ namespace Aws
bool isRetry = !handle->GetMultiPartId().empty();
uint64_t sentBytes = 0;

std::shared_ptr<Aws::Utils::Crypto::Hash> fullObjectHashCalculator;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you can use a immdiately invoked function expression to avoid branching assignment like this i.e.

const auto fullObjectHashCalculator = [](S3::Model::ChecksumAlgorithm algorithm) -> std::shared_ptr<Aws::Utils::Crypto::Hash> {
    if (handle->GetChecksum().empty() && !isRetry) {
      if (m_transferConfig.checksumAlgorithm == S3::Model::ChecksumAlgorithm::CRC32C) {
        fullObjectHashCalculator = Aws::MakeShared<Aws::Utils::Crypto::CRC32C>("TransferManager");
      } else{
        fullObjectHashCalculator = Aws::MakeShared<Aws::Utils::Crypto::CRC64>("TransferManager");
      }
   }
  }(m_transferConfig.checksumAlgorithm);

you may have to add a few more parameters, but thats the idea

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated the initiation if fullObjectHashCalculator

if (handle->GetChecksum().empty() && !isRetry) {
if (m_transferConfig.checksumAlgorithm == S3::Model::ChecksumAlgorithm::CRC32C) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep this is right however we gotta do it for all entries in the S3::Model::ChecksumAlgorithm enum

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added other checksum algorithms

fullObjectHashCalculator = Aws::MakeShared<Aws::Utils::Crypto::CRC32C>("TransferManager");
} else{
fullObjectHashCalculator = Aws::MakeShared<Aws::Utils::Crypto::CRC64>("TransferManager");
}
}

if (!isRetry) {
Aws::S3::Model::CreateMultipartUploadRequest createMultipartRequest = m_transferConfig.createMultipartUploadTemplate;
createMultipartRequest.SetChecksumAlgorithm(m_transferConfig.checksumAlgorithm);
Expand Down Expand Up @@ -466,6 +478,10 @@ namespace Aws
streamToPut->seekg((partsIter->first - 1) * m_transferConfig.bufferSize);
streamToPut->read(reinterpret_cast<char*>(buffer), lengthToWrite);

if (fullObjectHashCalculator) {
fullObjectHashCalculator->Update(buffer, static_cast<size_t>(lengthToWrite));
}

auto streamBuf = Aws::New<Aws::Utils::Stream::PreallocatedStreamBuf>(CLASS_TAG, buffer, static_cast<size_t>(lengthToWrite));
auto preallocatedStreamReader = Aws::MakeShared<Aws::IOStream>(CLASS_TAG, streamBuf);

Expand Down Expand Up @@ -525,6 +541,13 @@ namespace Aws
handle->UpdateStatus(DetermineIfFailedOrCanceled(*handle));
TriggerTransferStatusUpdatedCallback(handle);
}
else if (fullObjectHashCalculator && handle->GetChecksum().empty()) {
// Finalize checksum calculation and set on handle
auto hashResult = fullObjectHashCalculator->GetHash();
if (hashResult.IsSuccess()) {
handle->SetChecksum(Aws::Utils::HashingUtils::Base64Encode(hashResult.GetResult()));
}
}
}

void TransferManager::DoSinglePartUpload(const std::shared_ptr<TransferHandle>& handle)
Expand Down Expand Up @@ -1508,4 +1531,4 @@ namespace Aws
}
}
}
}
}
16 changes: 16 additions & 0 deletions tests/aws-cpp-sdk-transfer-tests/TransferTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <aws/s3/S3Client.h>
#include <aws/s3/model/DeleteObjectsRequest.h>
#include <aws/s3/model/AbortMultipartUploadRequest.h>
#include <aws/s3/model/CompleteMultipartUploadRequest.h>
#include <aws/s3/model/ListObjectsRequest.h>
#include <aws/s3/model/ListMultipartUploadsRequest.h>
#include <aws/s3/model/GetObjectRequest.h>
Expand Down Expand Up @@ -160,6 +161,21 @@ class MockS3Client : public S3Client
return S3Client::ListObjectsV2(request);
}

// Override to verify checksum is being sent
Model::CompleteMultipartUploadOutcome CompleteMultipartUpload(const Model::CompleteMultipartUploadRequest& request) const override
{
std::cout << "=== CompleteMultipartUpload Request ===" << std::endl;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets not use std::cout here use a AWS_LOGSTREAM_* to log, not std out

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace std::cout with a logger

std::cout << "Available ChecksumAlgorithm enum values:" << std::endl;
std::cout << "ChecksumType: " << (int)request.GetChecksumType() << std::endl;
std::cout << "ChecksumCRC32: " << request.GetChecksumCRC32() << std::endl;
std::cout << "ChecksumCRC32C: " << request.GetChecksumCRC32C() << std::endl;
std::cout << "ChecksumCRC64NVME: " << request.GetChecksumCRC64NVME() << std::endl;
std::cout << "ChecksumSHA1: " << request.GetChecksumSHA1() << std::endl;
std::cout << "ChecksumSHA256: " << request.GetChecksumSHA256() << std::endl;
std::cout << "=======================================" << std::endl;
return S3Client::CompleteMultipartUpload(request);
}

// m_executor in Base class is private, we need our own one.
std::shared_ptr<Aws::Utils::Threading::Executor> executor;

Expand Down
Loading