Repository navigation
Expand file tree
/
Copy pathlist_objects.cpp
More file actions
120 lines (99 loc) · 3.35 KB
/
Copy pathlist_objects.cpp
File metadata and controls
120 lines (99 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
#include <iostream>
#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/ListObjectsV2Request.h>
#include <aws/s3/model/Object.h>
#include "s3_examples.h"
/**
* 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 objects in an S3 bucket.
*
*/
//! Routine which demonstrates listing the objects in an S3 bucket.
/*!
\param bucketName: Name of the S3 bucket.
\param clientConfig: Aws client configuration.
\param[out] keysResult: Vector to receive the keys.
\return bool: Function succeeded.
*/
// snippet-start:[s3.cpp.list_objects.code]
bool AwsDoc::S3::listObjects(const Aws::String &bucketName,
Aws::Vector<Aws::String> &keysResult,
const Aws::S3::S3ClientConfiguration &clientConfig) {
Aws::S3::S3Client s3Client(clientConfig);
Aws::S3::Model::ListObjectsV2Request request;
request.WithBucket(bucketName);
Aws::String continuationToken; // Used for pagination.
Aws::Vector<Aws::S3::Model::Object> allObjects;
do {
if (!continuationToken.empty()) {
request.SetContinuationToken(continuationToken);
}
auto outcome = s3Client.ListObjectsV2(request);
if (!outcome.IsSuccess()) {
std::cerr << "Error: listObjects: " <<
outcome.GetError().GetMessage() << std::endl;
return false;
} else {
Aws::Vector<Aws::S3::Model::Object> objects =
outcome.GetResult().GetContents();
allObjects.insert(allObjects.end(), objects.begin(), objects.end());
continuationToken = outcome.GetResult().GetNextContinuationToken();
}
} while (!continuationToken.empty());
std::cout << allObjects.size() << " object(s) found:" << std::endl;
for (const auto &object: allObjects) {
std::cout << " " << object.GetKey() << std::endl;
keysResult.push_back(object.GetKey());
}
return true;
}
// snippet-end:[s3.cpp.list_objects.code]
/*
*
* main function
*
* Prerequisites: Create a bucket containing at least one object.
*
* usage: run_list_objects <bucket_name>
*
* Where:
* bucket_name - The name of the bucket that contains the objects.
*.
*/
#ifndef EXCLUDE_MAIN_FUNCTION
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;
}
Aws::SDKOptions options;
Aws::InitAPI(options);
{
const Aws::String bucketName = 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";
Aws::Vector<Aws::String> keysResult;
AwsDoc::S3::listObjects(bucketName, keysResult, clientConfig);
}
Aws::ShutdownAPI(options);
return 0;
}
#endif // EXCLUDE_MAIN_FUNCTION