|  | 
|  | 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | 
|  | 2 | +// SPDX - License - Identifier: Apache - 2.0 | 
|  | 3 | + | 
|  | 4 | +#include <awsdoc/s3-crt/s3-crt-demo.h> | 
|  | 5 | +// snippet-start:[s3-crt.cpp.bucket_operations.list_create_delete] | 
|  | 6 | +#include <iostream> | 
|  | 7 | +#include <fstream> | 
|  | 8 | +#include <aws/core/Aws.h> | 
|  | 9 | +#include <aws/core/utils/memory/stl/AWSStringStream.h> | 
|  | 10 | +#include <aws/core/utils/logging/CRTLogSystem.h> | 
|  | 11 | +#include <aws/s3-crt/S3CrtClient.h> | 
|  | 12 | +#include <aws/s3-crt/model/CreateBucketRequest.h> | 
|  | 13 | +#include <aws/s3-crt/model/DeleteBucketRequest.h> | 
|  | 14 | +#include <aws/s3-crt/model/PutObjectRequest.h> | 
|  | 15 | +#include <aws/s3-crt/model/GetObjectRequest.h> | 
|  | 16 | +#include <aws/s3-crt/model/DeleteObjectRequest.h> | 
|  | 17 | + | 
|  | 18 | +static const char ALLOCATION_TAG[] = "s3-crt-demo"; | 
|  | 19 | + | 
|  | 20 | +// List all Amazon Simple Storage Service (Amazon S3) buckets under the account. | 
|  | 21 | +bool ListBuckets(const Aws::S3Crt::S3CrtClient& s3CrtClient, const Aws::String& bucketName) { | 
|  | 22 | + | 
|  | 23 | + Aws::S3Crt::Model::ListBucketsOutcome outcome = s3CrtClient.ListBuckets(); | 
|  | 24 | + | 
|  | 25 | + if (outcome.IsSuccess()) { | 
|  | 26 | + std::cout << "All buckets under my account:" << std::endl; | 
|  | 27 | + | 
|  | 28 | + for (auto const& bucket : outcome.GetResult().GetBuckets()) | 
|  | 29 | + { | 
|  | 30 | + std::cout << " * " << bucket.GetName() << std::endl; | 
|  | 31 | + } | 
|  | 32 | + std::cout << std::endl; | 
|  | 33 | + | 
|  | 34 | + return true; | 
|  | 35 | + } | 
|  | 36 | + else { | 
|  | 37 | + std::cout << "ListBuckets error:\n"<< outcome.GetError() << std::endl << std::endl; | 
|  | 38 | + | 
|  | 39 | + return false; | 
|  | 40 | + } | 
|  | 41 | +} | 
|  | 42 | + | 
|  | 43 | +// Create an Amazon Simple Storage Service (Amazon S3) bucket. | 
|  | 44 | +bool CreateBucket(const Aws::S3Crt::S3CrtClient& s3CrtClient, const Aws::String& bucketName) { | 
|  | 45 | + | 
|  | 46 | + std::cout << "Creating bucket: \"" << bucketName << "\" ..." << std::endl; | 
|  | 47 | + | 
|  | 48 | + Aws::S3Crt::Model::CreateBucketRequest request; | 
|  | 49 | + request.SetBucket(bucketName); | 
|  | 50 | + | 
|  | 51 | + Aws::S3Crt::Model::CreateBucketOutcome outcome = s3CrtClient.CreateBucket(request); | 
|  | 52 | + | 
|  | 53 | + if (outcome.IsSuccess()) { | 
|  | 54 | + std::cout << "Bucket created." << std::endl << std::endl; | 
|  | 55 | + | 
|  | 56 | + return true; | 
|  | 57 | + } | 
|  | 58 | + else { | 
|  | 59 | + std::cout << "CreateBucket error:\n" << outcome.GetError() << std::endl << std::endl; | 
|  | 60 | + | 
|  | 61 | + return false; | 
|  | 62 | + } | 
|  | 63 | +} | 
|  | 64 | + | 
|  | 65 | +// Delete an existing Amazon S3 bucket. | 
|  | 66 | +bool DeleteBucket(const Aws::S3Crt::S3CrtClient& s3CrtClient, const Aws::String& bucketName) { | 
|  | 67 | + | 
|  | 68 | + std::cout << "Deleting bucket: \"" << bucketName << "\" ..." << std::endl; | 
|  | 69 | + | 
|  | 70 | + Aws::S3Crt::Model::DeleteBucketRequest request; | 
|  | 71 | + request.SetBucket(bucketName); | 
|  | 72 | + | 
|  | 73 | + Aws::S3Crt::Model::DeleteBucketOutcome outcome = s3CrtClient.DeleteBucket(request); | 
|  | 74 | + | 
|  | 75 | + if (outcome.IsSuccess()) { | 
|  | 76 | + std::cout << "Bucket deleted." << std::endl << std::endl; | 
|  | 77 | + | 
|  | 78 | + return true; | 
|  | 79 | + } | 
|  | 80 | + else { | 
|  | 81 | + std::cout << "DeleteBucket error:\n" << outcome.GetError() << std::endl << std::endl; | 
|  | 82 | + | 
|  | 83 | + return false; | 
|  | 84 | + } | 
|  | 85 | +} | 
|  | 86 | + | 
|  | 87 | +// Put an Amazon S3 object to the bucket. | 
|  | 88 | +bool PutObject(const Aws::S3Crt::S3CrtClient& s3CrtClient, const Aws::String& bucketName, const Aws::String& objectKey, const Aws::String& fileName) { | 
|  | 89 | + | 
|  | 90 | + std::cout << "Putting object: \"" << objectKey << "\" to bucket: \"" << bucketName << "\" ..." << std::endl; | 
|  | 91 | + | 
|  | 92 | + Aws::S3Crt::Model::PutObjectRequest request; | 
|  | 93 | + request.SetBucket(bucketName); | 
|  | 94 | + request.SetKey(objectKey); | 
|  | 95 | + std::shared_ptr<Aws::IOStream> bodyStream = Aws::MakeShared<Aws::FStream>(ALLOCATION_TAG, fileName.c_str(), std::ios_base::in | std::ios_base::binary); | 
|  | 96 | + if (!bodyStream->good()) { | 
|  | 97 | + std::cout << "Failed to open file: \"" << fileName << "\"." << std::endl << std::endl; | 
|  | 98 | + return false; | 
|  | 99 | + } | 
|  | 100 | + request.SetBody(bodyStream); | 
|  | 101 | + | 
|  | 102 | + Aws::S3Crt::Model::PutObjectOutcome outcome = s3CrtClient.PutObject(request); | 
|  | 103 | + | 
|  | 104 | + if (outcome.IsSuccess()) { | 
|  | 105 | + std::cout << "Object added." << std::endl << std::endl; | 
|  | 106 | + | 
|  | 107 | + return true; | 
|  | 108 | + } | 
|  | 109 | + else { | 
|  | 110 | + std::cout << "PutObject error:\n" << outcome.GetError() << std::endl << std::endl; | 
|  | 111 | + | 
|  | 112 | + return false; | 
|  | 113 | + } | 
|  | 114 | +} | 
|  | 115 | + | 
|  | 116 | +// Get the Amazon S3 object from the bucket. | 
|  | 117 | +bool GetObject(const Aws::S3Crt::S3CrtClient& s3CrtClient, const Aws::String& bucketName, const Aws::String& objectKey) { | 
|  | 118 | + | 
|  | 119 | + std::cout << "Getting object: \"" << objectKey << "\" from bucket: \"" << bucketName << "\" ..." << std::endl; | 
|  | 120 | + | 
|  | 121 | + Aws::S3Crt::Model::GetObjectRequest request; | 
|  | 122 | + request.SetBucket(bucketName); | 
|  | 123 | + request.SetKey(objectKey); | 
|  | 124 | + | 
|  | 125 | + Aws::S3Crt::Model::GetObjectOutcome outcome = s3CrtClient.GetObject(request); | 
|  | 126 | + | 
|  | 127 | + if (outcome.IsSuccess()) { | 
|  | 128 | + std::cout << "Object content: " << outcome.GetResult().GetBody().rdbuf() << std::endl << std::endl; | 
|  | 129 | + | 
|  | 130 | + return true; | 
|  | 131 | + } | 
|  | 132 | + else { | 
|  | 133 | + std::cout << "GetObject error:\n" << outcome.GetError() << std::endl << std::endl; | 
|  | 134 | + | 
|  | 135 | + return false; | 
|  | 136 | + } | 
|  | 137 | +} | 
|  | 138 | + | 
|  | 139 | +// Delete the Amazon S3 object from the bucket. | 
|  | 140 | +bool DeleteObject(const Aws::S3Crt::S3CrtClient& s3CrtClient, const Aws::String& bucketName, const Aws::String& objectKey) { | 
|  | 141 | + | 
|  | 142 | + std::cout << "Deleting object: \"" << objectKey << "\" from bucket: \"" << bucketName << "\" ..." << std::endl; | 
|  | 143 | + | 
|  | 144 | + Aws::S3Crt::Model::DeleteObjectRequest request; | 
|  | 145 | + request.SetBucket(bucketName); | 
|  | 146 | + request.SetKey(objectKey); | 
|  | 147 | + | 
|  | 148 | + Aws::S3Crt::Model::DeleteObjectOutcome outcome = s3CrtClient.DeleteObject(request); | 
|  | 149 | + | 
|  | 150 | + if (outcome.IsSuccess()) { | 
|  | 151 | + std::cout << "Object deleted." << std::endl << std::endl; | 
|  | 152 | + | 
|  | 153 | + return true; | 
|  | 154 | + } | 
|  | 155 | + else { | 
|  | 156 | + std::cout << "DeleteObject error:\n" << outcome.GetError() << std::endl << std::endl; | 
|  | 157 | + | 
|  | 158 | + return false; | 
|  | 159 | + } | 
|  | 160 | +} | 
|  | 161 | + | 
|  | 162 | +// 1. List all buckets under the account | 
|  | 163 | +// 2. Create an Amazon S3 bucket | 
|  | 164 | +// 3. Put an object to the bucket | 
|  | 165 | +// 4. Get the object | 
|  | 166 | +// 5. Delete the object | 
|  | 167 | +// 6. Delete the bucket | 
|  | 168 | +int main(int argc, char* argv[]) { | 
|  | 169 | + | 
|  | 170 | + Aws::SDKOptions options; | 
|  | 171 | + | 
|  | 172 | + // Override default log level for AWS common runtime libraries to prevent from being overwhelmed by logs from them. | 
|  | 173 | + options.loggingOptions.crt_logger_create_fn = []() { | 
|  | 174 | + return Aws::MakeShared<Aws::Utils::Logging::DefaultCRTLogSystem>(ALLOCATION_TAG, Aws::Utils::Logging::LogLevel::Warn); | 
|  | 175 | + }; | 
|  | 176 | + | 
|  | 177 | + // Uncomment the following code to override default global client bootstrap for AWS common runtime libraries. | 
|  | 178 | + // options.ioOptions.clientBootstrap_create_fn = []() { | 
|  | 179 | + // Aws::Crt::Io::EventLoopGroup eventLoopGroup(0 /* cpuGroup */, 18 /* threadCount */); | 
|  | 180 | + // Aws::Crt::Io::DefaultHostResolver defaultHostResolver(eventLoopGroup, 8 /* maxHosts */, 300 /* maxTTL */); | 
|  | 181 | + // auto clientBootstrap = Aws::MakeShared<Aws::Crt::Io::ClientBootstrap>(ALLOCATION_TAG, eventLoopGroup, defaultHostResolver); | 
|  | 182 | + // clientBootstrap->EnableBlockingShutdown(); | 
|  | 183 | + // return clientBootstrap; | 
|  | 184 | + // }; | 
|  | 185 | + | 
|  | 186 | + // Uncomment the following code to override default global TLS connection options for AWS common runtime libraries. | 
|  | 187 | + // options.ioOptions.tlsConnectionOptions_create_fn = []() { | 
|  | 188 | + // Aws::Crt::Io::TlsContextOptions tlsCtxOptions = Aws::Crt::Io::TlsContextOptions::InitDefaultClient(); | 
|  | 189 | + // Aws::Crt::Io::TlsContext tlsContext(tlsCtxOptions, Aws::Crt::Io::TlsMode::CLIENT); | 
|  | 190 | + // return Aws::MakeShared<Aws::Crt::Io::TlsConnectionOptions>(ALLOCATION_TAG, tlsContext.NewConnectionOptions()); | 
|  | 191 | + // }; | 
|  | 192 | + | 
|  | 193 | + Aws::InitAPI(options); | 
|  | 194 | + { | 
|  | 195 | + Aws::String bucket_name = "my-bucket"; | 
|  | 196 | + Aws::String object_key = "my-object"; | 
|  | 197 | + Aws::String file_name = "my-file"; | 
|  | 198 | + Aws::String region = Aws::Region::US_EAST_1; | 
|  | 199 | + const double throughput_target_gbps = 5; | 
|  | 200 | + const uint64_t part_size = 8 * 1024 * 1024; // 8 MB. | 
|  | 201 | + | 
|  | 202 | + Aws::S3Crt::ClientConfiguration config; | 
|  | 203 | + config.region = region; | 
|  | 204 | + config.throughputTargetGbps = throughput_target_gbps; | 
|  | 205 | + config.partSize = part_size; | 
|  | 206 | + | 
|  | 207 | + Aws::S3Crt::S3CrtClient s3_crt_client(config); | 
|  | 208 | + | 
|  | 209 | + ListBuckets(s3_crt_client, bucket_name); | 
|  | 210 | + | 
|  | 211 | + CreateBucket(s3_crt_client, bucket_name); | 
|  | 212 | + | 
|  | 213 | + PutObject(s3_crt_client, bucket_name, object_key, file_name); | 
|  | 214 | + | 
|  | 215 | + GetObject(s3_crt_client, bucket_name, object_key); | 
|  | 216 | + | 
|  | 217 | + DeleteObject(s3_crt_client, bucket_name, object_key); | 
|  | 218 | + | 
|  | 219 | + DeleteBucket(s3_crt_client, bucket_name); | 
|  | 220 | + } | 
|  | 221 | + Aws::ShutdownAPI(options); | 
|  | 222 | + | 
|  | 223 | + return 0; | 
|  | 224 | +} | 
|  | 225 | +// snippet-end:[s3-crt.cpp.bucket_operations.list_create_delete] | 
0 commit comments