Skip to content

Commit

Permalink
C++,Bash: Multi-service, update bucket names. (#6944)
Browse files Browse the repository at this point in the history
  • Loading branch information
meyertst-aws authored Oct 1, 2024
1 parent a370cb2 commit 4c5489e
Show file tree
Hide file tree
Showing 24 changed files with 146 additions and 52 deletions.
30 changes: 28 additions & 2 deletions .doc_gen/metadata/dynamodb_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ dynamodb_CreateTable:
- description:
snippet_tags:
- dynamodb.cpp.create_table.code
- description: Code that waits for the table to become active.
snippet_tags:
- cpp.example_code.dynamodb.scenario.waitTableActive

Go:
versions:
- sdk_version: 2
Expand Down Expand Up @@ -623,11 +627,13 @@ dynamodb_PutItem:
versions:
- sdk_version: 1
github: cpp/example_code/dynamodb
sdkguide:
excerpts:
- description:
snippet_tags:
- dynamodb.cpp.put_item.code
- description: Code that waits for the table to become active.
snippet_tags:
- cpp.example_code.dynamodb.scenario.waitTableActive
Go:
versions:
- sdk_version: 2
Expand Down Expand Up @@ -890,6 +896,9 @@ dynamodb_UpdateItem:
- description:
snippet_tags:
- dynamodb.cpp.update_item.code
- description: Code that waits for the table to become active.
snippet_tags:
- cpp.example_code.dynamodb.scenario.waitTableActive
Go:
versions:
- sdk_version: 2
Expand Down Expand Up @@ -1109,11 +1118,13 @@ dynamodb_DeleteItem:
versions:
- sdk_version: 1
github: cpp/example_code/dynamodb
sdkguide:
excerpts:
- description:
snippet_tags:
- cpp.example_code.dynamodb.delete_item
- description: Code that waits for the table to become active.
snippet_tags:
- cpp.example_code.dynamodb.scenario.waitTableActive
Swift:
versions:
- sdk_version: 1
Expand Down Expand Up @@ -1774,6 +1785,21 @@ dynamodb_BatchExecuteStatement:
- cpp.example_code.dynamodb.BatchExecuteStatement.Delete
services:
dynamodb: {BatchExecuteStatement}
dynamodb_UpdateTable:
languages:
C++:
versions:
- sdk_version: 1
github: cpp/example_code/dynamodb
excerpts:
- description:
snippet_tags:
- dynamodb.cpp.update_table.code
- description: Code that waits for the table to become active.
snippet_tags:
- cpp.example_code.dynamodb.scenario.waitTableActive
services:
dynamodb: {UpdateTable}
dynamodb_Usage_DaxDemo:
title: Accelerate &DDB; reads with &DAX; using an &AWS; SDK
title_abbrev: Accelerate reads with &DAX;
Expand Down
7 changes: 5 additions & 2 deletions aws-cli/bash-linux/s3/s3_getting_started.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@ function s3_getting_started() {
echo_repeat "*" 88
echo "Welcome to the Amazon S3 getting started demo."
echo_repeat "*" 88

echo "A unique bucket will be created by appending a Universally Unique Identifier to a bucket name prefix."
echo -n "Enter a prefix for the S3 bucket that will be used in this demo: "
get_input
bucket_name_prefix=$get_input_result
local bucket_name
bucket_name=$(generate_random_name "amzn-s3-demo-bucket")
bucket_name=$(generate_random_name "$bucket_name_prefix")

local region_code
region_code=$(aws configure get region)
Expand Down
1 change: 1 addition & 0 deletions cpp/example_code/dynamodb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Code excerpts that show you how to call individual service functions.
- [Query](query_items.cpp#L22)
- [Scan](scan_table.cpp#L23)
- [UpdateItem](update_item.cpp#L24)
- [UpdateTable](update_table.cpp#L33)

### Scenarios

Expand Down
3 changes: 2 additions & 1 deletion cpp/example_code/dynamodb/batch_write_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,10 @@ bool AwsDoc::DynamoDB::batchWriteItem(const Aws::String &jsonFilePath,
std::cerr << "Error with DynamoDB::BatchWriteItem. "
<< outcome.GetError().GetMessage()
<< std::endl;
return false;
}

return true;
return outcome.IsSuccess();
}

//! Convert requests in JSON format to a vector of WriteRequest objects.
Expand Down
3 changes: 2 additions & 1 deletion cpp/example_code/dynamodb/create_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ bool AwsDoc::DynamoDB::createTable(const Aws::String &tableName,
else {
std::cerr << "Failed to create table: " << outcome.GetError().GetMessage()
<< std::endl;
return false;
}

return outcome.IsSuccess();
return waitTableActive(tableName, dynamoClient);
}
// snippet-end:[dynamodb.cpp.create_table.code]

Expand Down
3 changes: 2 additions & 1 deletion cpp/example_code/dynamodb/create_table_composite_key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ bool AwsDoc::DynamoDB::createTableWithCompositeKey(const Aws::String &tableName,
else {
std::cerr << "Failed to create table:" << outcome.GetError().GetMessage()
<< std::endl;
return false;
}

return outcome.IsSuccess();
return waitTableActive(tableName, dynamoClient);
}
// snippet-end:[dynamodb.cpp.create_table_composite_key.code]

Expand Down
3 changes: 2 additions & 1 deletion cpp/example_code/dynamodb/delete_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ bool AwsDoc::DynamoDB::deleteItem(const Aws::String &tableName,
else {
std::cerr << "Failed to delete item: " << outcome.GetError().GetMessage()
<< std::endl;
return false;
}

return outcome.IsSuccess();
return waitTableActive(tableName, dynamoClient);
}
// snippet-end:[cpp.example_code.dynamodb.delete_item]

Expand Down
5 changes: 3 additions & 2 deletions cpp/example_code/dynamodb/dynamodb_samples.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <aws/core/client/ClientConfiguration.h>
#include <aws/dynamodb/model/AttributeValue.h>
#include <aws/dynamodb/DynamoDBClient.h>

namespace AwsDoc {
namespace DynamoDB {
Expand Down Expand Up @@ -262,11 +263,11 @@ namespace AwsDoc {
/*!
\sa waitTableActive()
\param waitTableActive: The DynamoDB table's name.
\param clientConfiguration: AWS client configuration.
\param dynamoClient: A DynamoDB client.
\return bool: Function succeeded.
*/
bool waitTableActive(const Aws::String &tableName,
const Aws::Client::ClientConfiguration &clientConfiguration);
const Aws::DynamoDB::DynamoDBClient &dynamoClient);
//! Command line prompt/response utility function.
/*!
\\sa askQuestion()
Expand Down
6 changes: 3 additions & 3 deletions cpp/example_code/dynamodb/dynamodb_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,12 @@ bool AwsDoc::DynamoDB::deleteMoviesDynamoDBTable(
/*!
\sa waitTableActive()
\param waitTableActive: The DynamoDB table's name.
\param clientConfiguration: AWS client configuration.
\param dynamoClient: A DynamoDB client.
\return bool: Function succeeded.
*/
bool AwsDoc::DynamoDB::waitTableActive(const Aws::String &tableName,
const Aws::Client::ClientConfiguration &clientConfiguration) {
Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration);
const Aws::DynamoDB::DynamoDBClient &dynamoClient) {

// Repeatedly call DescribeTable until table is ACTIVE.
const int MAX_QUERIES = 20;
Aws::DynamoDB::Model::DescribeTableRequest request;
Expand Down
3 changes: 2 additions & 1 deletion cpp/example_code/dynamodb/put_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ bool AwsDoc::DynamoDB::putItem(const Aws::String &tableName,
}
else {
std::cerr << outcome.GetError().GetMessage() << std::endl;
return false;
}

return outcome.IsSuccess();
return waitTableActive(tableName, dynamoClient);
}

// snippet-end:[dynamodb.cpp.put_item.code]
Expand Down
6 changes: 3 additions & 3 deletions cpp/example_code/dynamodb/update_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ bool AwsDoc::DynamoDB::updateItem(const Aws::String &tableName,
request);
if (outcome.IsSuccess()) {
std::cout << "Item was updated" << std::endl;
}
else {
} else {
std::cerr << outcome.GetError().GetMessage() << std::endl;
return false;
}

return outcome.IsSuccess();
return waitTableActive(tableName, dynamoClient);
}
// snippet-end:[dynamodb.cpp.update_item.code]

Expand Down
14 changes: 10 additions & 4 deletions cpp/example_code/dynamodb/update_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,18 @@ bool AwsDoc::DynamoDB::updateTable(const Aws::String &tableName,
request);
if (outcome.IsSuccess()) {
std::cout << "Successfully updated the table." << std::endl;
}
else {
std::cerr << outcome.GetError().GetMessage() << std::endl;
} else {
const Aws::DynamoDB::DynamoDBError &error = outcome.GetError();
if (error.GetErrorType() == Aws::DynamoDB::DynamoDBErrors::VALIDATION &&
error.GetMessage().find("The provisioned throughput for the table will not change") != std::string::npos) {
std::cout << "The provisioned throughput for the table will not change." << std::endl;
} else {
std::cerr << outcome.GetError().GetMessage() << std::endl;
return false;
}
}

return outcome.IsSuccess();
return waitTableActive(tableName, dynamoClient);
}
// snippet-end:[dynamodb.cpp.update_table.code]

Expand Down
22 changes: 17 additions & 5 deletions cpp/example_code/s3/create_bucket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,39 @@ bool AwsDoc::S3::createBucket(const Aws::String &bucketName,
*
* main function
*
* Usage: 'run_create_bucket>'
* Usage: 'run_create_bucket <bucket_name_prefix>
*
*/

#ifndef EXCLUDE_MAIN_FUNCTION

int main() {
int main(int argc, char* argv[]) {
Aws::SDKOptions options;
InitAPI(options);

if (argc != 2) {
std::cout << R"(
Usage:
run_create_bucket <bucket_name_prefix>
Where:
bucket_name - A bucket name prefix which will be made unique by appending a UUID.
)" << std::endl;
return 1;
}

Aws::String bucketNamePrefix = argv[1];

{
Aws::S3::S3ClientConfiguration clientConfig;
// Optional: Set to the AWS Region in which the bucket was created (overrides config file).
// clientConfig.region = "us-east-1";

// Create a unique bucket name to increase the chance of success
// when trying to create the bucket.
// Format: "amzn-s3-demo-bucket-" + lowercase UUID.
// Format: "<bucketNamePrefix> + "-" + lowercase UUID.
Aws::String uuid = Aws::Utils::UUID::RandomUUID();
Aws::String bucketName = "amzn-s3-demo-bucket-" +
Aws::Utils::StringUtils::ToLower(uuid.c_str());
Aws::String bucketName = bucketNamePrefix + "-" +
Aws::Utils::StringUtils::ToLower(uuid.c_str());

AwsDoc::S3::createBucket(bucketName, clientConfig);
}
Expand Down
26 changes: 20 additions & 6 deletions cpp/example_code/s3/list_objects_with_aws_global_region.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <thread>
#include <iostream>
#include <aws/core/Aws.h>
#include <aws/core/utils/logging/LogLevel.h>
#include <aws/core/client/ClientConfiguration.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/CreateBucketRequest.h>
Expand Down Expand Up @@ -42,10 +41,10 @@
*/
static const int MAX_TIMEOUT_RETRIES = 20;

static Aws::String createOneBucket(const Aws::S3::S3Client &s3Client) {
static Aws::String createOneBucket(const Aws::String &bucketNamePrefix, const Aws::S3::S3Client &s3Client) {
// Create an S3 bucket within the us-west-2 AWS Region.
Aws::String uuid = Aws::Utils::UUID::RandomUUID();
Aws::String bucketName = "amzn-s3-demo-bucket-" +
Aws::String bucketName = bucketNamePrefix +
Aws::Utils::StringUtils::ToLower(uuid.c_str());

Aws::S3::Model::CreateBucketRequest createBucketRequest;
Expand Down Expand Up @@ -160,13 +159,14 @@ bool deleteABucket(const Aws::S3::S3Client &s3Client, const Aws::String &bucketN
*/

bool AwsDoc::S3::listObjectsWithAwsGlobalRegion(
const Aws::String &bucketNamePrefix,
const Aws::S3::S3ClientConfiguration &clientConfig) {
Aws::S3::S3ClientConfiguration config(clientConfig);
config.region = Aws::Region::AWS_GLOBAL;

Aws::S3::S3Client s3Client(config);

Aws::String bucketName = createOneBucket(s3Client);
Aws::String bucketName = createOneBucket(bucketNamePrefix, s3Client);
if (bucketName.empty()) {
return false;
}
Expand All @@ -186,17 +186,31 @@ bool AwsDoc::S3::listObjectsWithAwsGlobalRegion(
*
* main function
*
* Usage: ' run_list_objects_with_aws_global_region_bucket <bucket_name_prefix>'
*
*/

#ifndef EXCLUDE_MAIN_FUNCTION

int main() {
int main(int argc, char *argv[]) {
if (argc != 2) {
std::cout << R"(
Usage:
run_list_objects_with_aws_global_region_bucket <bucket_name_prefix>
Where:
bucket_name - A bucket name prefix which will be made unique by appending a UUID.
)" << std::endl;
return 1;
}

Aws::SDKOptions options;

InitAPI(options);

Aws::String bucketNamePrefix = argv[1];
{
Aws::S3::S3ClientConfiguration config;
AwsDoc::S3::listObjectsWithAwsGlobalRegion(config);
AwsDoc::S3::listObjectsWithAwsGlobalRegion(bucketNamePrefix, config);
}
ShutdownAPI(options);

Expand Down
4 changes: 3 additions & 1 deletion cpp/example_code/s3/s3_examples.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ namespace AwsDoc {
const Aws::S3::S3ClientConfiguration &clientConfig);

bool listObjectsWithAwsGlobalRegion(
const Aws::String &bucketNamePrefix,
const Aws::S3::S3ClientConfiguration &clientConfig);

Aws::String generatePreSignedPutObjectUrl(const Aws::String &bucketName,
Expand Down Expand Up @@ -107,7 +108,8 @@ namespace AwsDoc {
const Aws::String &errorPage,
const Aws::S3::S3ClientConfiguration &clientConfig);

bool S3_GettingStartedScenario(const Aws::String &uploadFilePath,
bool S3_GettingStartedScenario(const Aws::String &bucketNamePrefix,
const Aws::String &uploadFilePath,
const Aws::String &saveFilePath,
const Aws::Client::ClientConfiguration &clientConfig);

Expand Down
Loading

0 comments on commit 4c5489e

Please sign in to comment.