Repository navigation
Expand file tree
/
Copy pathdelete_objects.cpp
More file actions
119 lines (100 loc) · 3.54 KB
/
Copy pathdelete_objects.cpp
File metadata and controls
119 lines (100 loc) · 3.54 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
/**
* 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
*
* For information on the structure of the code examples and how to build and run the examples, see
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started-code-examples.html.
*
* Purpose
*
* Demonstrates using the AWS SDK for C++ to delete multiple objects in an Amazon Simple Storage
* Service (Amazon S3) bucket.
*
*/
#include <iostream>
#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/DeleteObjectsRequest.h>
#include "s3_examples.h"
//! Routine which demonstrates deleting multiple objects in an Amazon S3 bucket.
/*!
\param objectKeys: Vector of object keys.
\param fromBucket: Name of a bucket with an object to delete.
\param clientConfig: AWS client configuration.
\return bool: Function succeeded.
*/
// snippet-start:[cpp.example_code.s3.delete_objects]
bool AwsDoc::S3::deleteObjects(const std::vector<Aws::String> &objectKeys,
const Aws::String &fromBucket,
const Aws::S3::S3ClientConfiguration &clientConfig) {
Aws::S3::S3Client client(clientConfig);
Aws::S3::Model::DeleteObjectsRequest request;
Aws::S3::Model::Delete deleteObject;
for (const Aws::String &objectKey: objectKeys) {
deleteObject.AddObjects(Aws::S3::Model::ObjectIdentifier().WithKey(objectKey));
}
request.SetDelete(deleteObject);
request.SetBucket(fromBucket);
Aws::S3::Model::DeleteObjectsOutcome outcome =
client.DeleteObjects(request);
if (!outcome.IsSuccess()) {
auto err = outcome.GetError();
std::cerr << "Error deleting objects. " <<
err.GetExceptionName() << ": " << err.GetMessage() << std::endl;
} else {
std::cout << "Successfully deleted the objects.";
for (size_t i = 0; i < objectKeys.size(); ++i) {
std::cout << objectKeys[i];
if (i < objectKeys.size() - 1) {
std::cout << ", ";
}
}
std::cout << " from bucket " << fromBucket << "." << std::endl;
}
return outcome.IsSuccess();
}
// snippet-end:[cpp.example_code.s3.delete_objects]
/*
*
* main function
*
* Prerequisites: The bucket containing the objects to delete.
*
* Usage: 'run_delete_objects <bucket_name> <object_key> ...'
*
*/
#ifndef EXCLUDE_MAIN_FUNCTION
int main(int argc, char **argv) {
if (argc < 3)
{
std::cout << R"(
Usage:
run_delete_objects <bucket_name> <object_key> ...
Where:
bucket_name - Name of S3 bucket.
<object_key> ... - One or more objects to delete.
)" << std::endl;
return 1;
}
Aws::SDKOptions options;
Aws::InitAPI(options);
{
Aws::String bucketName(argv[1]);
Aws::Vector<Aws::String> objectKeys;
for (int i = 2; i < argc; ++i)
{
objectKeys.push_back(Aws::String(argv[i]));
}
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::deleteObjects(objectKeys, bucketName, clientConfig);
}
ShutdownAPI(options);
return 0;
}
#endif // EXCLUDE_MAIN_FUNCTION