· 6 years ago · Mar 06, 2019, 10:18 AM
1#include <iostream>
2#include <aws/core/Aws.h>
3#include <aws/s3/S3Client.h>
4#include <aws/s3/model/PutObjectRequest.h>
5#include <aws/core/auth/AWSCredentialsProvider.h>
6
7#include <iostream>
8#include <fstream>
9#include <sys/stat.h>
10
11#include <curl/curl.h>
12
13using namespace Aws::Auth;
14using namespace Aws::Http;
15using namespace Aws::Client;
16using namespace Aws::S3;
17using namespace Aws::S3::Model;
18using namespace Aws::Utils;
19
20int main(int argc, const char * argv[]) {
21
22 Aws::SDKOptions options;
23 Aws::InitAPI(options);
24
25 ClientConfiguration clientConfig;
26 //config.region = "us-west-2";
27 clientConfig.scheme = Aws::Http::Scheme::HTTPS;
28 clientConfig.region = Aws::Region::US_EAST_2;
29 clientConfig.connectTimeoutMs = 30000;
30 clientConfig.requestTimeoutMs = 600000;
31 clientConfig.enableHostPrefixInjection = true;
32
33 static const char * ACCESS_KEY_ID = "myKey";
34 static const char * SECRET_KEY = "mySecretKey";
35 static const char * ALLOCATION_TAG = "SampleAllocationTag";
36
37 static const char * filename = "myFilename";
38
39 std::shared_ptr<S3Client> client = Aws::MakeShared<S3Client>(
40 ALLOCATION_TAG,
41 AWSCredentials(Aws::String(ACCESS_KEY_ID), Aws::String(SECRET_KEY)),
42 clientConfig
43 );
44
45 Aws::S3::Model::PutObjectRequest object_request;
46
47 object_request.SetBucket("myBucketName");
48 //object_request.SetKey(s3_object_name);
49 const std::shared_ptr<Aws::IOStream> input_data =
50 Aws::MakeShared<Aws::FStream>(ALLOCATION_TAG,
51 filename,
52 std::ios_base::in | std::ios_base::binary);
53 object_request.SetBody(input_data);
54
55 // Put the object
56 auto put_object_outcome = client->PutObject(object_request);
57 if (!put_object_outcome.IsSuccess()) {
58 auto error = put_object_outcome.GetError();
59 std::cout << "ERROR: " << error.GetExceptionName() << ": "
60 << error.GetMessage() << std::endl;
61 }
62
63 Aws::ShutdownAPI(options);
64
65 return 0;
66}