Repository navigation
Expand file tree
/
Copy pathAwsBucket.cpp
More file actions
55 lines (42 loc) · 1.52 KB
/
Copy pathAwsBucket.cpp
File metadata and controls
55 lines (42 loc) · 1.52 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
#include "AwsBucket.h"
#pragma comment(lib, "aws-cpp-sdk-core.lib")
#pragma comment(lib, "aws-cpp-sdk-s3.lib")
CAwsBucket::CAwsBucket() {
// constructor implementation
Aws::InitAPI(options);
config.region = "us-east-1";
}
CAwsBucket::~CAwsBucket() {
// destructor implementation
Aws::ShutdownAPI(options);
}
int CAwsBucket::Send(const Aws::String bucket_name, const Aws::String file_name, const Aws::String object_key) {
Aws::S3::S3Client s3_client(config);
std::cout << "Uploading " << file_name << " to S3 bucket " <<
bucket_name << " with key " << object_key << std::endl;
std::ifstream file_stream(file_name.c_str(), std::ios::in | std::ios::binary);
if (!file_stream)
{
std::cout << "Failed to open file: " << file_name << std::endl;
return 1;
}
Aws::S3::Model::PutObjectRequest object_request;
object_request.SetBucket(bucket_name);
object_request.SetKey(object_key);
// Binary files must also have the std::ios_base::bin flag or'ed in
auto input_data = Aws::MakeShared<Aws::FStream>("SampleAllocationTag",
file_name.c_str(), std::ios_base::in | std::ios_base::binary);
object_request.SetBody(input_data);
auto put_object_outcome = s3_client.PutObject(object_request);
if (put_object_outcome.IsSuccess())
{
std::cout << "Done!" << std::endl;
}
else
{
std::cout << "PutObject error: "
<< put_object_outcome.GetError().GetExceptionName() << std::endl;
return 1;
}
return 0;
}