Search code examples
c++amazon-s3ibm-cloud-storage

How can I create a smart-tier bucket?


I need to create a smart-tier bucket in IBM Cloud Storage using S3 in C++. I configured this function:

inline bool createBucket(const Aws::S3::S3Client& client, const std::string& bucket) {
    try {
        Aws::S3::Model::CreateBucketRequest crtRequest;
        crtRequest.SetBucket(bucket);
        crtRequest.SetACL(Aws::S3::Model::BucketCannedACL::private_);
        Aws::S3::Model::IntelligentTieringConfiguration configuration;
        configuration.SetId(Aws::String("SmartTiering"));//Setting ID
        configuration.SetStatus(Aws::S3::Model::IntelligentTieringStatus::Enabled);
        Aws::S3::Model::PutBucketIntelligentTieringConfigurationRequest request;
        request.SetBucket(bucket);
        request.SetIntelligentTieringConfiguration(configuration);

        const auto crtOutcome = client.CreateBucket(crtRequest);
        if (crtOutcome.IsSuccess()) {
            std::cout << "created bucket" << std::endl;
        }

        const auto cfgOutcome = client.PutBucketIntelligentTieringConfiguration(request);
        if (!cfgOutcome.IsSuccess()) {
            std::cerr << cfgOutcome.GetError().GetMessage() << std::endl;
            std::cerr << "Configuration ID: " << configuration.GetId() << std::endl;
            return false;
        }
        return true;
    }catch (const Aws::Client::AWSError<Aws::S3::S3Errors>& e) {
        if (e.GetErrorType() == Aws::S3::S3Errors::NETWORK_CONNECTION) {
            std::cerr << "no internet" << std::endl;
        }
        return false;
    }
}

Output:

created bucket
Missing required field [Id]
Configuration ID: SmartTiering

Output is contradictive and bucket gets created with a standard-tier storage type. In Python it's done with a locationConstraint but there is no Aws::S3::Model::LocationConstraint type containing "smart". Python implementation (using ibm-cos-sdk):

def create_bucket(self, bucket_name):
        self.client.create_bucket(Bucket=bucket_name, ACL='private', CreateBucketConfiguration={'LocationConstraint': 'us-south-smart'})

Solution

    1. Set your region parameter for Aws::S3::S3Client to containg smart in the end, e.g. us-south-smart.

    2. Set the location constraint using Aws::S3::Model::BucketLocationConstraintMapper to match the region. Corrected code:

      Aws::S3::Model::CreateBucketRequest request;
      request.SetBucket(bucket);
      Aws::S3::Model::CreateBucketConfiguration bucketConfig;
      bucketConfig.SetLocationConstraint(
          Aws::S3::Model::BucketLocationConstraintMapper::
              GetBucketLocationConstraintForName(region));  // this is the key
      request.SetCreateBucketConfiguration(bucketConfig);
      const auto crtOutcome = client.CreateBucket(request);
      if (crtOutcome.IsSuccess()) {
          std::cout << "created bucket" << std::endl;
      } else {
          std::cout << crtOutcome.GetError().GetMessage() << std::endl;
      }