aws / aws-sdk-cpp

AWS SDK for C++
Apache License 2.0
1.96k stars 1.05k forks source link

The S3 protocol does not support retrieving multiple attributes through GetObjectAttributes. #2866

Closed wrjvszq closed 7 months ago

wrjvszq commented 7 months ago

Describe the bug

The following code does not allow for the simultaneous retrieval of Checksum and ObjectSize.

  Aws::S3::Model::GetObjectAttributesRequest attributesRequest;
  Aws::Vector<Aws::S3::Model::ObjectAttributes> objectAttributes;

  objectAttributes.push_back(Aws::S3::Model::ObjectAttributes::Checksum);
  objectAttributes.push_back(Aws::S3::Model::ObjectAttributes::ObjectSize);

  attributesRequest.WithBucket(bucketName).WithKey(objectName);
  attributesRequest.SetObjectAttributes(objectAttributes);
  auto attributesObjectOutcome = s3Client.GetObjectAttributes(attributesRequest);

  if(attributesObjectOutcome.IsSuccess()) {
    auto& attributes = attributesObjectOutcome.GetResult();
    auto crc32 = attributes.GetChecksum().GetChecksumCRC32();
    std::cout << "CRC32: " << crc32 << std::endl;
    long long fileSize = attributes.GetObjectSize();
    std::cout << "File Size: " << fileSize << " bytes" << std::endl;
  } else {
      std::cerr << "Failed to get the object : " << attributesObjectOutcome.GetError().GetExceptionName() << " " << attributesObjectOutcome.GetError().GetMessage() << std::endl;
  }

Expected Behavior

It is possible to simultaneously retrieve both Checksum and ObjectSize.

Current Behavior

  objectAttributes.push_back(Aws::S3::Model::ObjectAttributes::Checksum);
  objectAttributes.push_back(Aws::S3::Model::ObjectAttributes::ObjectSize);

Reproduction Steps

  Aws::S3::Model::GetObjectAttributesRequest attributesRequest;
  Aws::Vector<Aws::S3::Model::ObjectAttributes> objectAttributes;

  objectAttributes.push_back(Aws::S3::Model::ObjectAttributes::Checksum);
  objectAttributes.push_back(Aws::S3::Model::ObjectAttributes::ObjectSize);

  attributesRequest.WithBucket(bucketName).WithKey(objectName);
  attributesRequest.SetObjectAttributes(objectAttributes);
  auto attributesObjectOutcome = s3Client.GetObjectAttributes(attributesRequest);

  if(attributesObjectOutcome.IsSuccess()) {
    auto& attributes = attributesObjectOutcome.GetResult();
    auto crc32 = attributes.GetChecksum().GetChecksumCRC32();
    std::cout << "CRC32: " << crc32 << std::endl;
    long long fileSize = attributes.GetObjectSize();
    std::cout << "File Size: " << fileSize << " bytes" << std::endl;
  } else {
      std::cerr << "Failed to get the object : " << attributesObjectOutcome.GetError().GetExceptionName() << " " << attributesObjectOutcome.GetError().GetMessage() << std::endl;
  }

Possible Solution

Modify the code on line 123 of the GetObjectAttributesRequest.cpp file located at aws-sdk-cpp/generated/src/aws-cpp-sdk-s3/source/model

  if(m_objectAttributesHasBeenSet)
  {
    Aws::String temp;
    for(const auto& item : m_objectAttributes)
    {
      ss << ObjectAttributesMapper::GetNameForObjectAttributes(item);
      temp = ss.str();
      headers.emplace("x-amz-object-attributes", ss.str());
      ss.str("");
    }
  }
  if(m_objectAttributesHasBeenSet)
  {
    auto it = m_objectAttributes.begin();
    for (; it != m_objectAttributes.end(); ++it) {
      if (it != m_objectAttributes.begin()) {
          ss << ",";
      }
      ss << ObjectAttributesMapper::GetNameForObjectAttributes(*it);
    }

    headers.emplace("x-amz-object-attributes", ss.str());
    ss.str("");
  }

Additional Information/Context

No response

AWS CPP SDK version used

V1.11.x

Compiler and Version used

10

Operating System and version

20.04.1-Ubuntu

sbiscigl commented 7 months ago

Hey thanks for the clear and easy to repro issue, got a fix out for it, hopefuilly will be in soon

jmklix commented 7 months ago

Please update to the latest version of the sdk to get the above fix. Please let us know if you have any other problems with this sdk

github-actions[bot] commented 7 months ago

This issue is now closed. Comments on closed issues are hard for our team to see. If you need more assistance, please open a new issue that references this one.