forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathput_object_async.cpp
More file actions
185 lines (154 loc) · 6.55 KB
/
put_object_async.cpp
File metadata and controls
185 lines (154 loc) · 6.55 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// 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 "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 uploadFileAsyncFinished(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: uploadFileAsyncFinished: Finished uploading '"
<< context->GetUUID() << "'." << std::endl;
} else {
std::cerr << "Error: uploadFileAsyncFinished: " <<
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.
/*!
\param s3Client: Instance of the S3 Client.
\param request: Instance of the put object request.
\param bucketName: Name of the bucket.
\param fileName: Name of the file to put in the bucket.
\return bool: Function succeeded.
*/
// snippet-start:[s3.cpp.put_object_async.code]
bool AwsDoc::S3::uploadFileAsync(const Aws::S3::S3Client &s3Client,
Aws::S3::Model::PutObjectRequest &request,
const Aws::String &bucketName,
const Aws::String &fileName) {
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 uploadFileAsyncFinished function when the
// operation has finished.
s3Client.PutObjectAsync(request, uploadFileAsyncFinished, 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:
*
* Usage: run_put_object_async <file_name> <bucket_name>
*
*/
#ifndef EXCLUDE_MAIN_FUNCTION
// snippet-start:[s3.cpp.put_object_async.invoke.code]
int main(int argc, char* argv[])
{
if (argc != 3)
{
std::cout << R"(
Usage:
run_put_object_async <file_name> <bucket_name>
Where:
file_name - The name of the file to upload.
bucket_name - The name of the bucket to upload the object to.
)" << std::endl;
return 1;
}
const Aws::SDKOptions options;
Aws::InitAPI(options);
{
const Aws::String fileName = argv[1];
const Aws::String bucketName = argv[2];
// 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.
const Aws::S3::S3ClientConfiguration config;
// Optional: Set to the AWS Region in which the bucket was created (overrides config file).
// config.region = "us-east-1";
const Aws::S3::S3Client s3Client(config);
// Create the request object.
// This request object must be declared here, because the object must exist
// until the put object operation finishes.
Aws::S3::Model::PutObjectRequest request;
AwsDoc::S3::uploadFileAsync(s3Client, request, bucketName, fileName);
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 // EXCLUDE_MAIN_FUNCTION