/
FirehoseLibraryClient.cpp
197 lines (160 loc) · 5.87 KB
/
FirehoseLibraryClient.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
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
186
187
188
189
190
191
192
193
194
195
196
197
#include "FirehoseLibraryClient.h"
#include <aws/core/Aws.h>
#include <aws/core/client/CoreErrors.h>
#include <aws/core/auth/AWSCredentialsProviderChain.h>
#include <aws/core/utils/Outcome.h>
#include <aws/access-management/AccessManagementClient.h>
#include <aws/iam/IAMClient.h>
#include <aws/cognito-identity/CognitoIdentityClient.h>
#include <aws/core/client/ClientConfiguration.h>
#include <aws/core/utils/memory/stl/AWSStringStream.h>
#include <aws/firehose/FirehoseClient.h>
#include <aws/firehose/model/CreateDeliveryStreamRequest.h>
#include <aws/firehose/model/S3DestinationConfiguration.h>
#include <aws/firehose/model/PutRecordRequest.h>
#define DEBUG_INFO
using namespace std;
using namespace Aws;
using namespace Aws::Http;
using namespace Aws::Auth;
using namespace Aws::Client;
using namespace Aws::Firehose;
using namespace Aws::Firehose::Model;
using namespace Aws::Utils::Json;
//Constructor
FirehoseLibraryClient::FirehoseLibraryClient(string streamName, std::string bucketName) :
m_firehoseClient(nullptr),
m_config(new ClientConfiguration()),
m_streamName(streamName),
m_bucketName(bucketName),
m_options(new Aws::SDKOptions()),
m_initialized(false)
{
//m_options = new Aws::SDKOptions;
m_options->loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Trace;
Aws::InitAPI(*m_options);
m_config->scheme = Scheme::HTTPS;
m_config->region = Region::US_EAST_1;
m_firehoseClient = new FirehoseClient(*m_config);
}
//destructor
FirehoseLibraryClient::~FirehoseLibraryClient()
{
Aws::ShutdownAPI(*m_options);
}
bool FirehoseLibraryClient::initQueue(std::string bucketPrefix)
{
auto cognitoClient = Aws::MakeShared<Aws::CognitoIdentity::CognitoIdentityClient>("QueueOperationTest", *m_config);
auto iamClient = Aws::MakeShared<Aws::IAM::IAMClient>("QueueOperationTest", *m_config);
Aws::AccessManagement::AccessManagementClient accessManagementClient(iamClient, cognitoClient);
Aws::String accountId = accessManagementClient.GetAccountId();
//Aws::String user;
//Aws::IAM::Model::User data;
//accessManagementClient.GetUser(user, data);
#ifdef DEBUG_INFO
cout << "Account ID : " << accountId << /*user <<*/ endl;
#endif
CreateDeliveryStreamRequest request;
request.SetDeliveryStreamName(m_streamName.c_str());
S3DestinationConfiguration s3Config;
//TBD; role is fixed now...
Aws::String roleARN = "arn:aws:iam::" + accountId + ":role/firehose_delivery_role";
string bucketARN("arn:aws:s3:::" + m_bucketName);
Aws::String _bucketPrefix = bucketPrefix.c_str();
_bucketPrefix += "_";
s3Config.SetRoleARN(roleARN);
s3Config.SetBucketARN(bucketARN.c_str());
s3Config.SetPrefix(_bucketPrefix);
s3Config.SetBufferingHints(BufferingHints());
s3Config.SetCompressionFormat(CompressionFormat::UNCOMPRESSED);
s3Config.SetEncryptionConfiguration(EncryptionConfiguration());
request.SetS3DestinationConfiguration(s3Config);
CreateDeliveryStreamOutcome outcome = m_firehoseClient->CreateDeliveryStream(request);
if (outcome.IsSuccess())
{
cout << "Succesfull created new deliverystream [" << m_streamName << "]" << endl ;
m_initialized = true;
return true;
}else if (outcome.GetError().GetErrorType() == FirehoseErrors::RESOURCE_IN_USE){
#ifdef DEBUG_INFO
cout << "Resource in use, stream [" << m_streamName << "] exists already"<< endl;
#endif
m_initialized = true;
return true;
}else if (outcome.GetError().GetErrorType() == FirehoseErrors::LIMIT_EXCEEDED){
cout << "Limit Exceeded "<< endl;
}else if (outcome.GetError().GetErrorType() == FirehoseErrors::ACCESS_DENIED){
cout << "Access Denied "<< endl;
}else{
cout << "Other error.... [" << outcome.GetError().GetMessage() << "]" << endl;
}
return false;
}
bool FirehoseLibraryClient::sendMessage(const ifstream& data, int repetitions/* = 0*/)
{
if(!m_initialized)
{
cout << "Not Initialized." << endl;
return false;
}
PutRecordRequest request;
//set stream name;
Aws::String __streamName("TMP");
request.SetDeliveryStreamName(m_streamName.c_str());
Record record;
Aws::StringStream dataStream;
dataStream << data.rdbuf();
#ifdef DEBUG_INFO
cout << "Buff Size to transfer: [" << dataStream.str().length() << "]" << endl;
#endif
Aws::Utils::ByteBuffer buff((unsigned char*)dataStream.str().c_str(), dataStream.str().length());
//apply stream data to record buffer Data
record.SetData(buff);
//set record to request
request.SetRecord(record);
//for loop is for testing purposes only
for(int i = 0; i <= repetitions; i++)
{
//send request to cloud
Model::PutRecordOutcome outcome = m_firehoseClient->PutRecord(request);
if(!outcome.IsSuccess())
{
cout << "Error sending message " << i + 1 << "." << endl;
i = repetitions;
return false;
}
}
return true;
}
bool FirehoseLibraryClient::sendMessage(const std::vector<char>& dataVector)
{
if(!m_initialized)
{
cout << "Not Initialized." << endl;
return false;
}
PutRecordRequest request;
//set stream name;
Aws::String __streamName("TMP");
request.SetDeliveryStreamName(m_streamName.c_str());
Record record;
//Aws::StringStream dataStream;
//dataStream << data.rdbuf();
#ifdef DEBUG_INFO
cout << "Buff Size to transfer: [" << dataVector.size() << "]" << endl;
#endif
Aws::Utils::ByteBuffer buff((unsigned char*)&dataVector[0], dataVector.size());
//apply stream data to record buffer Data
record.SetData(buff);
//set record to request
request.SetRecord(record);
//for loop is for testing purposes only
//send request to cloud
Model::PutRecordOutcome outcome = m_firehoseClient->PutRecord(request);
if(!outcome.IsSuccess())
{
cout << "Error sending message ." << endl;
return false;
}
return true;
}