2021-12-16 12:18:11 -05:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
2021-07-15 14:56:24 -04:00
# include <iostream>
# include <aws/core/Aws.h>
# include <aws/s3/S3Client.h>
2024-04-24 14:30:38 -04:00
# include <aws/s3/model/ListObjectsV2Request.h>
2021-07-15 14:56:24 -04:00
# include <aws/s3/model/Object.h>
2024-07-03 09:52:38 -04:00
# include "s3_examples.h"
2021-07-15 14:56:24 -04:00
2022-09-15 09:33:04 -04:00
/**
* Before running this C++ code example, set up your development environment, including your credentials.
2021-07-15 14:56:24 -04:00
*
2022-09-15 09:33:04 -04:00
* For more information, see the following documentation topic:
2021-07-15 14:56:24 -04:00
*
2022-09-26 14:24:21 -04:00
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started.html
2022-09-15 09:33:04 -04:00
*
* Purpose
*
* Demonstrates using the AWS SDK for C++ to list the objects in an S3 bucket.
2021-07-15 14:56:24 -04:00
*
2022-09-15 09:33:04 -04:00
*/
2021-07-15 14:56:24 -04:00
2022-09-15 09:33:04 -04:00
//! Routine which demonstrates listing the objects in an S3 bucket.
/*!
2024-07-03 09:52:38 -04:00
\param bucketName: Name of the S3 bucket.
\param clientConfig: Aws client configuration.
2024-07-24 10:52:59 -04:00
\param[out] keysResult: Vector to receive the keys.
2024-07-03 09:52:38 -04:00
\return bool: Function succeeded.
2022-09-15 09:33:04 -04:00
*/
2021-07-15 14:56:24 -04:00
2022-09-15 09:33:04 -04:00
// snippet-start:[s3.cpp.list_objects.code]
2024-07-03 09:52:38 -04:00
bool AwsDoc : : S3 : : listObjects ( const Aws : : String & bucketName ,
2024-07-24 10:52:59 -04:00
Aws : : Vector < Aws : : String > & keysResult ,
2024-07-03 09:52:38 -04:00
const Aws : : S3 : : S3ClientConfiguration & clientConfig ) {
Aws : : S3 : : S3Client s3Client ( clientConfig ) ;
2021-07-15 14:56:24 -04:00
2024-04-24 14:30:38 -04:00
Aws : : S3 : : Model : : ListObjectsV2Request request ;
2021-07-15 14:56:24 -04:00
request . WithBucket ( bucketName ) ;
2024-04-24 14:30:38 -04:00
Aws : : String continuationToken ; // Used for pagination.
Aws : : Vector < Aws : : S3 : : Model : : Object > allObjects ;
2021-07-15 14:56:24 -04:00
2024-04-24 14:30:38 -04:00
do {
if ( ! continuationToken . empty ( ) ) {
request . SetContinuationToken ( continuationToken ) ;
}
2024-07-03 09:52:38 -04:00
auto outcome = s3Client . ListObjectsV2 ( request ) ;
2024-04-24 14:30:38 -04:00
if ( ! outcome . IsSuccess ( ) ) {
2024-07-03 09:52:38 -04:00
std : : cerr < < " Error: listObjects: " < <
2024-04-24 14:30:38 -04:00
outcome . GetError ( ) . GetMessage ( ) < < std : : endl ;
return false ;
2024-07-03 09:52:38 -04:00
} else {
2024-04-24 14:30:38 -04:00
Aws : : Vector < Aws : : S3 : : Model : : Object > objects =
outcome . GetResult ( ) . GetContents ( ) ;
2021-07-15 14:56:24 -04:00
2024-04-24 14:30:38 -04:00
allObjects . insert ( allObjects . end ( ) , objects . begin ( ) , objects . end ( ) ) ;
continuationToken = outcome . GetResult ( ) . GetNextContinuationToken ( ) ;
2021-07-15 14:56:24 -04:00
}
2024-04-24 14:30:38 -04:00
} while ( ! continuationToken . empty ( ) ) ;
std : : cout < < allObjects . size ( ) < < " object(s) found: " < < std : : endl ;
for ( const auto & object : allObjects ) {
std : : cout < < " " < < object . GetKey ( ) < < std : : endl ;
2024-07-24 10:52:59 -04:00
keysResult . push_back ( object . GetKey ( ) ) ;
2021-07-15 14:56:24 -04:00
}
2024-04-24 14:30:38 -04:00
return true ;
2021-07-15 14:56:24 -04:00
}
2022-09-15 09:33:04 -04:00
// snippet-end:[s3.cpp.list_objects.code]
/*
*
* main function
*
2022-09-26 11:16:08 -04:00
* Prerequisites: Create a bucket containing at least one object.
2022-09-15 09:33:04 -04:00
*
2024-07-03 09:52:38 -04:00
* usage: run_list_objects <bucket_name>
2022-09-15 09:33:04 -04:00
*
2024-07-03 09:52:38 -04:00
* Where:
* bucket_name - The name of the bucket that contains the objects.
*.
2022-09-15 09:33:04 -04:00
*/
2024-07-24 10:52:59 -04:00
# ifndef EXCLUDE_MAIN_FUNCTION
2021-07-15 14:56:24 -04:00
2024-07-03 09:52:38 -04:00
int main ( int argc , char * argv [ ] )
{
if ( argc ! = 2 )
{
std : : cout < < R " (
Usage :
run_list_objects < bucket_name >
Where :
bucket_name - The name of the bucket that contains the objects .
) " << std::endl;
return 1 ;
}
2021-07-15 14:56:24 -04:00
Aws : : SDKOptions options ;
Aws : : InitAPI ( options ) ;
{
2024-07-03 09:52:38 -04:00
const Aws : : String bucketName = argv [ 1 ] ;
2021-07-15 14:56:24 -04:00
2024-07-03 09:52:38 -04:00
Aws : : S3 : : S3ClientConfiguration clientConfig ;
2022-09-15 09:33:04 -04:00
// Optional: Set to the AWS Region in which the bucket was created (overrides config file).
// clientConfig.region = "us-east-1";
2024-07-24 10:52:59 -04:00
Aws : : Vector < Aws : : String > keysResult ;
AwsDoc : : S3 : : listObjects ( bucketName , keysResult , clientConfig ) ;
2024-04-24 14:30:38 -04:00
}
2021-07-15 14:56:24 -04:00
Aws : : ShutdownAPI ( options ) ;
return 0 ;
}
2022-09-15 09:33:04 -04:00
2024-07-24 10:52:59 -04:00
# endif // EXCLUDE_MAIN_FUNCTION
2022-09-15 09:33:04 -04:00