-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathput_object.cpp
116 lines (96 loc) · 3.24 KB
/
put_object.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
#include <iostream>
#include <fstream>
#include <sys/stat.h>
#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/PutObjectRequest.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.
*
*/
//! Routine which demonstrates putting an object in an S3 bucket.
/*!
\param bucketName: Name of the bucket.
\param fileName: Name of the file to put in the bucket.
\param clientConfig: Aws client configuration.
\return bool: Function succeeded.
*/
// snippet-start:[s3.cpp.put_object.code]
bool AwsDoc::S3::putObject(const Aws::String &bucketName,
const Aws::String &fileName,
const Aws::S3::S3ClientConfiguration &clientConfig) {
Aws::S3::S3Client s3Client(clientConfig);
Aws::S3::Model::PutObjectRequest request;
request.SetBucket(bucketName);
//We are using the name of the file as the key for the object in the bucket.
//However, this is just a string and can be set according to your retrieval needs.
request.SetKey(fileName);
std::shared_ptr<Aws::IOStream> inputData =
Aws::MakeShared<Aws::FStream>("SampleAllocationTag",
fileName.c_str(),
std::ios_base::in | std::ios_base::binary);
if (!*inputData) {
std::cerr << "Error unable to read file " << fileName << std::endl;
return false;
}
request.SetBody(inputData);
Aws::S3::Model::PutObjectOutcome outcome =
s3Client.PutObject(request);
if (!outcome.IsSuccess()) {
std::cerr << "Error: putObject: " <<
outcome.GetError().GetMessage() << std::endl;
} else {
std::cout << "Added object '" << fileName << "' to bucket '"
<< bucketName << "'.";
}
return outcome.IsSuccess();
}
// snippet-end:[s3.cpp.put_object.code]
/*
*
* main function
*
* Prerequisites: S3 bucket for the object.
*
* usage: run_put_object <object_name> <bucket_name>
*
*/
#ifndef EXCLUDE_MAIN_FUNCTION
int main(int argc, char* argv[])
{
if (argc != 3)
{
std::cout << R"(
Usage:
run_put_object <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;
}
Aws::SDKOptions options;
Aws::InitAPI(options);
{
const Aws::String fileName = argv[1];
const Aws::String bucketName = argv[2];
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::putObject(bucketName, fileName, clientConfig);
}
Aws::ShutdownAPI(options);
return 0;
}
#endif // EXCLUDE_MAIN_FUNCTION