// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 /** * Before running this C++ code example, set up your development environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started.html * * Purpose * * Demonstrates using the AWS SDK for C++ to create an S3 bucket and upload objects to S3 buckets. * * 1. Create a bucket. * 2. Upload a local file to the bucket. * 3. Download the object to a local file. * 4. Copy the object to a different "folder" in the bucket. * 5. List objects in the bucket. * 6. Delete all objects in the bucket. * 7. Delete the bucket. * */ // snippet-start:[cpp.example_code.s3.Scenario_GettingStarted] #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "s3_examples.h" namespace AwsDoc { namespace S3 { //! Delete an S3 bucket. /*! \param bucketName: The S3 bucket's name. \param client: An S3 client. \return bool: Function succeeded. */ static bool deleteBucket(const Aws::String &bucketName, Aws::S3::S3Client &client); //! Delete an object in an S3 bucket. /*! \param bucketName: The S3 bucket's name. \param key: The key for the object in the S3 bucket. \param client: An S3 client. \return bool: Function succeeded. */ static bool deleteObjectFromBucket(const Aws::String &bucketName, const Aws::String &key, Aws::S3::S3Client &client); } } //! Scenario to create, copy, and delete S3 buckets and objects. /*! \param bucketNamePrefix: A prefix for a bucket name. \param uploadFilePath: Path to file to upload to an Amazon S3 bucket. \param saveFilePath: Path for saving a downloaded S3 object. \param clientConfig: Aws client configuration. \return bool: Function succeeded. */ bool AwsDoc::S3::S3_GettingStartedScenario(const Aws::String &bucketNamePrefix, const Aws::String &uploadFilePath, const Aws::String &saveFilePath, const Aws::Client::ClientConfiguration &clientConfig) { Aws::S3::S3Client client(clientConfig); // Create a unique bucket name which is only temporary and will be deleted. // Format: + "-" + lowercase UUID. Aws::String uuid = Aws::Utils::UUID::RandomUUID(); Aws::String bucketName = bucketNamePrefix + Aws::Utils::StringUtils::ToLower(uuid.c_str()); // 1. Create a bucket. { Aws::S3::Model::CreateBucketRequest request; request.SetBucket(bucketName); if (clientConfig.region != Aws::Region::US_EAST_1) { Aws::S3::Model::CreateBucketConfiguration createBucketConfiguration; createBucketConfiguration.WithLocationConstraint( Aws::S3::Model::BucketLocationConstraintMapper::GetBucketLocationConstraintForName( clientConfig.region)); request.WithCreateBucketConfiguration(createBucketConfiguration); } Aws::S3::Model::CreateBucketOutcome outcome = client.CreateBucket(request); if (!outcome.IsSuccess()) { const Aws::S3::S3Error &err = outcome.GetError(); std::cerr << "Error: createBucket: " << err.GetExceptionName() << ": " << err.GetMessage() << std::endl; return false; } else { std::cout << "Created the bucket, '" << bucketName << "', in the region, '" << clientConfig.region << "'." << std::endl; } } // 2. Upload a local file to the bucket. Aws::String key = "key-for-test"; { Aws::S3::Model::PutObjectRequest request; request.SetBucket(bucketName); request.SetKey(key); std::shared_ptr input_data = Aws::MakeShared("SampleAllocationTag", uploadFilePath, std::ios_base::in | std::ios_base::binary); if (!input_data->is_open()) { std::cerr << "Error: unable to open file, '" << uploadFilePath << "'." << std::endl; AwsDoc::S3::deleteBucket(bucketName, client); return false; } request.SetBody(input_data); Aws::S3::Model::PutObjectOutcome outcome = client.PutObject(request); if (!outcome.IsSuccess()) { std::cerr << "Error: putObject: " << outcome.GetError().GetMessage() << std::endl; AwsDoc::S3::deleteObjectFromBucket(bucketName, key, client); AwsDoc::S3::deleteBucket(bucketName, client); return false; } else { std::cout << "Added the object with the key, '" << key << "', to the bucket, '" << bucketName << "'." << std::endl; } } // 3. Download the object to a local file. { Aws::S3::Model::GetObjectRequest request; request.SetBucket(bucketName); request.SetKey(key); Aws::S3::Model::GetObjectOutcome outcome = client.GetObject(request); if (!outcome.IsSuccess()) { const Aws::S3::S3Error &err = outcome.GetError(); std::cerr << "Error: getObject: " << err.GetExceptionName() << ": " << err.GetMessage() << std::endl; } else { std::cout << "Downloaded the object with the key, '" << key << "', in the bucket, '" << bucketName << "'." << std::endl; Aws::IOStream &ioStream = outcome.GetResultWithOwnership(). GetBody(); Aws::OFStream outStream(saveFilePath, std::ios_base::out | std::ios_base::binary); if (!outStream.is_open()) { std::cout << "Error: unable to open file, '" << saveFilePath << "'." << std::endl; } else { outStream << ioStream.rdbuf(); std::cout << "Wrote the downloaded object to the file '" << saveFilePath << "'." << std::endl; } } } // 4. Copy the object to a different "folder" in the bucket. Aws::String copiedToKey = "test-folder/" + key; { Aws::S3::Model::CopyObjectRequest request; request.WithBucket(bucketName) .WithKey(copiedToKey) .WithCopySource(bucketName + "/" + key); Aws::S3::Model::CopyObjectOutcome outcome = client.CopyObject(request); if (!outcome.IsSuccess()) { std::cerr << "Error: copyObject: " << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Copied the object with the key, '" << key << "', to the key, '" << copiedToKey << ", in the bucket, '" << bucketName << "'." << std::endl; } } // 5. List objects in the bucket. { Aws::S3::Model::ListObjectsV2Request request; request.WithBucket(bucketName); Aws::String continuationToken; Aws::Vector allObjects; do { if (!continuationToken.empty()) { request.SetContinuationToken(continuationToken); } Aws::S3::Model::ListObjectsV2Outcome outcome = client.ListObjectsV2( request); if (!outcome.IsSuccess()) { std::cerr << "Error: ListObjects: " << outcome.GetError().GetMessage() << std::endl; break; } else { Aws::Vector objects = outcome.GetResult().GetContents(); allObjects.insert(allObjects.end(), objects.begin(), objects.end()); continuationToken = outcome.GetResult().GetContinuationToken(); } } while (!continuationToken.empty()); std::cout << allObjects.size() << " objects in the bucket, '" << bucketName << "':" << std::endl; for (Aws::S3::Model::Object &object: allObjects) { std::cout << " '" << object.GetKey() << "'" << std::endl; } } // 6. Delete all objects in the bucket. // All objects in the bucket must be deleted before deleting the bucket. AwsDoc::S3::deleteObjectFromBucket(bucketName, copiedToKey, client); AwsDoc::S3::deleteObjectFromBucket(bucketName, key, client); // 7. Delete the bucket. return AwsDoc::S3::deleteBucket(bucketName, client); } bool AwsDoc::S3::deleteObjectFromBucket(const Aws::String &bucketName, const Aws::String &key, Aws::S3::S3Client &client) { Aws::S3::Model::DeleteObjectRequest request; request.SetBucket(bucketName); request.SetKey(key); Aws::S3::Model::DeleteObjectOutcome outcome = client.DeleteObject(request); if (!outcome.IsSuccess()) { std::cerr << "Error: deleteObject: " << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Deleted the object with the key, '" << key << "', from the bucket, '" << bucketName << "'." << std::endl; } return outcome.IsSuccess(); } bool AwsDoc::S3::deleteBucket(const Aws::String &bucketName, Aws::S3::S3Client &client) { Aws::S3::Model::DeleteBucketRequest request; request.SetBucket(bucketName); Aws::S3::Model::DeleteBucketOutcome outcome = client.DeleteBucket(request); if (!outcome.IsSuccess()) { const Aws::S3::S3Error &err = outcome.GetError(); std::cerr << "Error: deleteBucket: " << err.GetExceptionName() << ": " << err.GetMessage() << std::endl; } else { std::cout << "Deleted the bucket, '" << bucketName << "'." << std::endl; } return outcome.IsSuccess(); } // snippet-end:[cpp.example_code.s3.Scenario_GettingStarted] #ifndef EXCLUDE_MAIN_FUNCTION int main(int argc, const char *argv[]) { if (argc != 4) { std::cout << "Usage:\n" << " \n\n" << "Where:\n" << " bucketNamePrefix - A prefix for a bucket name..\n" " uploadFilePath - The path where the file is located (for example, C:/AWS/book2.pdf).\n" << " saveFilePath - The path where the file is saved after it's " << "downloaded (for example, C:/AWS/book2.pdf). " << std::endl; return 1; } Aws::String bucketNamePrefix = argv[1]; Aws::String objectPath = argv[2]; Aws::String savePath = argv[3]; Aws::SDKOptions options; InitAPI(options); { Aws::Client::ClientConfiguration clientConfig; AwsDoc::S3::S3_GettingStartedScenario(bucketNamePrefix, objectPath, savePath, clientConfig); } ShutdownAPI(options); return 0; } #endif // EXCLUDE_MAIN_FUNCTION