SIGN IN SIGN UP

Welcome to the AWS Code Examples Repository. This repo contains code examples used in the AWS documentation, AWS SDK Developer Guides, and more. For more information, see the Readme.md file below.

0 0 17 Java
2021-12-15 12:21:27 -05:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
2021-11-15 13:15:43 +00:00
#include <iostream>
#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/Bucket.h>
#include "s3_examples.h"
2021-11-15 13:15:43 +00:00
/**
* 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 list the buckets in the current account.
*
2021-12-15 12:21:27 -05:00
*/
2021-11-15 13:15:43 +00:00
//! Routine which demonstrates listing the buckets in the current account.
/*!
\param clientConfig: Aws client configuration.
\return bool: Function succeeded.
*/
2021-11-15 13:15:43 +00:00
2021-12-15 12:21:27 -05:00
// snippet-start:[s3.cpp.list_buckets.code]
bool AwsDoc::S3::listBuckets(const Aws::S3::S3ClientConfiguration &clientConfig) {
Aws::S3::S3Client client(clientConfig);
2022-07-28 18:11:43 -04:00
auto outcome = client.ListBuckets();
bool result = true;
if (!outcome.IsSuccess()) {
2022-09-15 11:03:36 -04:00
std::cerr << "Failed with error: " << outcome.GetError() << std::endl;
result = false;
} else {
2022-07-28 18:11:43 -04:00
std::cout << "Found " << outcome.GetResult().GetBuckets().size() << " buckets\n";
for (auto &&b: outcome.GetResult().GetBuckets()) {
2022-07-28 18:11:43 -04:00
std::cout << b.GetName() << std::endl;
}
}
return result;
2022-07-28 18:11:43 -04:00
}
// snippet-end:[s3.cpp.list_buckets.code]
/*
*
* main function
*
*/
#ifndef EXCLUDE_MAIN_FUNCTION
2021-11-15 13:15:43 +00:00
int main() {
2021-12-15 12:21:27 -05:00
//The Aws::SDKOptions struct contains SDK configuration options.
//An instance of Aws::SDKOptions is passed to the Aws::InitAPI and
2022-09-26 11:16:08 -04:00
//Aws::ShutdownAPI methods. The same instance should be sent to both methods.
2022-07-28 18:11:43 -04:00
Aws::SDKOptions options;
2021-12-15 12:21:27 -05:00
//The AWS SDK for C++ must be initialized by calling Aws::InitAPI.
InitAPI(options);
2022-07-28 18:11:43 -04:00
{
Aws::S3::S3ClientConfiguration clientConfig;
// Optional: Set to the AWS Region in which the bucket was created (overrides config file).
// clientConfig.region = "us-east-1";
AwsDoc::S3::listBuckets(clientConfig);
}
2021-11-15 13:15:43 +00:00
2021-12-15 12:21:27 -05:00
//Before the application terminates, the SDK must be shut down.
ShutdownAPI(options);
2021-11-15 13:15:43 +00:00
return 0;
}
#endif // EXCLUDE_MAIN_FUNCTION