This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
forked from SecurityForCloudBuilders/FileStorageDemoApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3-upload.cpp
79 lines (61 loc) · 1.88 KB
/
s3-upload.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
#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 <awsdoc/s3/s3_examples.h>
bool AwsDoc::S3::PutObject(const Aws::String& bucketName,
const Aws::String& objectName,
const Aws::String& region)
{
// Verify that the file exists.
struct stat buffer;
if (stat(objectName.c_str(), &buffer) == -1)
{
std::cout << "Error: PutObject: File '" <<
objectName << "' does not exist." << std::endl;
return false;
}
Aws::Client::ClientConfiguration config;
if (!region.empty())
{
config.region = region;
}
Aws::S3::S3Client s3_client(config);
Aws::S3::Model::PutObjectRequest request;
request.SetBucket(bucketName);
request.SetKey(objectName);
std::shared_ptr<Aws::IOStream> input_data =
Aws::MakeShared<Aws::FStream>("SampleAllocationTag",
objectName.c_str(),
std::ios_base::in | std::ios_base::binary);
request.SetBody(input_data);
Aws::S3::Model::PutObjectOutcome outcome =
s3_client.PutObject(request);
if (outcome.IsSuccess()) {
std::cout << "Added object '" << objectName << "' to bucket '"
<< bucketName << "'.";
return true;
}
else
{
std::cout << "Error: PutObject: " <<
outcome.GetError().GetMessage() << std::endl;
return false;
}
}
// int main(void) {
// Aws::SDKOptions options;
// Aws::InitAPI(options);
// {
// const Aws::String bucket_name = "my-bucket";
// const Aws::String object_name = "my-file.txt";
// const Aws::String region = "us-east-1";
// if (!AwsDoc::S3::PutObject(bucket_name, object_name, region)) {
// return 1;
// }
// }
// Aws::ShutdownAPI(options);
// return 0;
// }