/
put_object_async.cpp
171 lines (143 loc) · 6.31 KB
/
put_object_async.cpp
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/PutObjectRequest.h>
#include <chrono>
#include <condition_variable>
#include <fstream>
#include <iostream>
#include <mutex>
#include <sys/stat.h>
#include <awsdoc/s3/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 put an object in an S3 bucket using the async API.
*
*/
// snippet-start:[s3.cpp.put_object_async.mutex_vars]
// A mutex is a synchronization primitive that can be used to protect shared
// data from being simultaneously accessed by multiple threads.
std::mutex AwsDoc::S3::upload_mutex;
// A condition_variable is a synchronization primitive that can be used to
// block a thread, or to block multiple threads at the same time.
// The thread is blocked until another thread both modifies a shared
// variable (the condition) and notifies the condition_variable.
std::condition_variable AwsDoc::S3::upload_variable;
// snippet-end:[s3.cpp.put_object_async.mutex_vars]
//! Routine which implements an async task finished callback.
/*!
\fn PutObjectAsyncFinished()
\param s3Client Instance of the caller's Amazon S3 client object.
\param request Instance of the caller's put object request.
\param outcome Instance of the caller's put object outcome.
\param context Instance of the caller's put object call context.
*/
// snippet-start:[s3.cpp.put_object_async_finished.code]
void PutObjectAsyncFinished(const Aws::S3::S3Client *s3Client,
const Aws::S3::Model::PutObjectRequest &request,
const Aws::S3::Model::PutObjectOutcome &outcome,
const std::shared_ptr<const Aws::Client::AsyncCallerContext> &context) {
if (outcome.IsSuccess()) {
std::cout << "Success: PutObjectAsyncFinished: Finished uploading '"
<< context->GetUUID() << "'." << std::endl;
}
else {
std::cerr << "Error: PutObjectAsyncFinished: " <<
outcome.GetError().GetMessage() << std::endl;
}
// Unblock the thread that is waiting for this function to complete.
AwsDoc::S3::upload_variable.notify_one();
}
// snippet-end:[s3.cpp.put_object_async_finished.code]
//! Routine which demonstrates adding an object to an Amazon S3 bucket, asynchronously.
/*!
\fn GetObjectAcl()
\param s3Client Instance of the S3 Client.
\param bucketName Name of the bucket.
\param fileName Name of the file to put in the bucket.
*/
// snippet-start:[s3.cpp.put_object_async.code]
bool AwsDoc::S3::PutObjectAsync(const Aws::S3::S3Client &s3Client,
const Aws::String &bucketName,
const Aws::String &fileName) {
// Create and configure the asynchronous put object request.
Aws::S3::Model::PutObjectRequest request;
request.SetBucket(bucketName);
request.SetKey(fileName);
const std::shared_ptr<Aws::IOStream> input_data =
Aws::MakeShared<Aws::FStream>("SampleAllocationTag",
fileName.c_str(),
std::ios_base::in | std::ios_base::binary);
if (!*input_data) {
std::cerr << "Error: unable to open file " << fileName << std::endl;
return false;
}
request.SetBody(input_data);
// Create and configure the context for the asynchronous put object request.
std::shared_ptr<Aws::Client::AsyncCallerContext> context =
Aws::MakeShared<Aws::Client::AsyncCallerContext>("PutObjectAllocationTag");
context->SetUUID(fileName);
// Make the asynchronous put object call. Queue the request into a
// thread executor and call the PutObjectAsyncFinished function when the
// operation has finished.
s3Client.PutObjectAsync(request, PutObjectAsyncFinished, context);
return true;
}
// snippet-end:[s3.cpp.put_object_async.code]
/**
*
* main function
*
* Prerequisites: The bucket and the object to get the ACL information about:
*
* TODO(user): items: Set the following variables
* - bucketName: The name of the bucket to get the ACL information about.
* - object_name: Name of object already in bucket.
*
*/
#ifndef TESTING_BUILD
// snippet-start:[s3.cpp.put_object_async.invoke.code]
int main() {
Aws::SDKOptions options;
Aws::InitAPI(options);
{
// TODO(user): Change bucket_name to the name of a bucket in your account.
const Aws::String bucket_name = "<Enter a bucket name>";
//TODO(user): Create a file called "my-file.txt" in the local folder where your executables are built to.
const Aws::String object_name = "my-file.txt";
// A unique_lock is a general-purpose mutex ownership wrapper allowing
// deferred locking, time-constrained attempts at locking, recursive
// locking, transfer of lock ownership, and use with
// condition variables.
std::unique_lock<std::mutex> lock(AwsDoc::S3::upload_mutex);
// Create and configure the Amazon S3 client.
// This client must be declared here, as this client must exist
// until the put object operation finishes.
Aws::Client::ClientConfiguration config;
// Optional: Set to the AWS Region in which the bucket was created (overrides config file).
// config.region = "us-east-1";
Aws::S3::S3Client s3_client(config);
AwsDoc::S3::PutObjectAsync(s3_client, bucket_name, object_name);
std::cout << "main: Waiting for file upload attempt..." <<
std::endl << std::endl;
// While the put object operation attempt is in progress,
// you can perform other tasks.
// This example simply blocks until the put object operation
// attempt finishes.
AwsDoc::S3::upload_variable.wait(lock);
std::cout << std::endl << "main: File upload attempt completed."
<< std::endl;
}
Aws::ShutdownAPI(options);
return 0;
}
// snippet-end:[s3.cpp.put_object_async.invoke.code]
#endif // TESTING_BUILD