-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathactions.cpp
514 lines (407 loc) · 16.8 KB
/
actions.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#include "qs3client.h"
#include "actions.h"
#include <aws/s3/model/ListObjectsRequest.h>
#include <aws/s3/model/Object.h>
#include <aws/s3/model/HeadObjectRequest.h>
#include <aws/s3/model/GetObjectRequest.h>
#include <aws/s3/model/PutObjectRequest.h>
#include <aws/s3/model/UploadPartRequest.h>
#include <aws/s3/model/UploadPartResult.h>
#include <aws/s3/model/CreateMultipartUploadRequest.h>
#include <fstream>
#include <iostream>
#include <QtConcurrent>
#include <aws/core/utils/memory/stl/AWSStreamFwd.h>
#include <aws/core/utils/stream/PreallocatedStreamBuf.h>
#include <aws/core/utils/memory/stl/AWSStringStream.h>
#include <aws/core/utils/HashingUtils.h>
#include <aws/s3/model/CompleteMultipartUploadRequest.h>
#include <aws/s3/model/AbortMultipartUploadRequest.h>
namespace qlibs3 {
void CommandAction::waitForFinished()
{
future.waitForFinished();
}
bool CommandAction::isFinished()
{
return future.isFinished();
}
void CommandAction::setFuture(const QFuture<void> f)
{
future = f;
}
UploadObjectHandler::UploadObjectHandler(QObject *parent, std::shared_ptr<S3Client> client,
QString bucketName,
QString keyName, QString readFile, QString contentType):
ObjectHandlerInterface(parent), m_client(client), m_uploadId("")
{
m_status.store(static_cast<long>(TransferStatus::NOT_STARTED));
m_bucketName = QString2AwsString(bucketName);
m_keyName = QString2AwsString(keyName);
m_readFile = readFile;
//missing contenttype will crush this SDK;
if (contentType.isEmpty()) {
m_contenttype = "application/octet-stream";
} else {
m_contenttype = QString2AwsString(contentType);
}
m_totalTransfered = 0;
}
void UploadObjectHandler::stop()
{
m_cancel.store(true);
}
int UploadObjectHandler::start()
{
std::function<void()> f = [this]() {
this->doUpload();
};
m_cancel.store(false);
future = QtConcurrent::run(f);
// this->doUpload();
return 1;
}
void UploadObjectHandler::waitForFinish()
{
future.waitForFinished();
}
void UploadObjectHandler::doUpload()
{
//should run in thread pool;
qDebug() << "Uploading" << m_readFile;
auto fileStream = Aws::MakeShared<Aws::FStream>("LOCALSTREAM", m_readFile.toLocal8Bit().constData(),
std::ios_base::in | std::ios_base::binary);
fileStream->seekg(0, std::ios_base::end);
m_totalSize = static_cast<size_t>(fileStream->tellg());
fileStream->seekg(0, std::ios_base::beg);
if (!fileStream->good()) {
std::cout << "enter no good\n";
m_status.store(static_cast<long>(TransferStatus::FAILED));
//not retriable error
s3error err(Aws::S3::S3Errors::NO_SUCH_UPLOAD, false);
err.SetMessage(QString2AwsString("Not such File :" + m_readFile));
emit updateStatus(TransferStatus::FAILED);
emit finished(false, err);
qDebug() << "no such file " << m_readFile;
return;
}
emit this->updateStatus(TransferStatus::IN_PROGRESS);
if (m_totalSize > BUFFERSIZE) //5M
doMultipartUpload(fileStream);
else
doSinglePartUpload(fileStream);
return;
}
bool UploadObjectHandler::shouldContinue()
{
return !this->m_cancel.load();
}
void UploadObjectHandler::doMultipartUpload(const std::shared_ptr<IOStream> &fileStream)
{
qDebug() << "DOING MULTIPART";
//should run in thread pool;
Aws::S3::Model::CreateMultipartUploadRequest createMultipartRequest;
createMultipartRequest.WithBucket(m_bucketName)
.WithKey(m_keyName);
uint64_t partCount;
auto createMultipartResponse = m_client->CreateMultipartUpload(createMultipartRequest);
if (createMultipartResponse.IsSuccess()) {
qDebug() << "create multipart success";
m_uploadId = createMultipartResponse.GetResult().GetUploadId();
partCount = ( m_totalSize + BUFFERSIZE - 1 ) / BUFFERSIZE;
for (int i = 0 ; i < partCount ; i++) {
PartState *p = new PartState;
p->partID = i + 1;
p->rangeBegin = i * BUFFERSIZE;
//the size of the last part is different
uint64_t partSize = (i + 1 < partCount ) ? BUFFERSIZE : (m_totalSize - BUFFERSIZE *
(partCount - 1));
p->sizeInBytes = partSize;
p->success = false;
m_queueMap.insert(p->partID, p);
qDebug() << p->partID << " " << p->rangeBegin << " " << p->sizeInBytes;
}
} else {
qDebug() << "create Multipart ID failed";
m_status.store((static_cast<long>(TransferStatus::FAILED)));
emit updateStatus(TransferStatus::FAILED);
s3error err(Aws::S3::S3Errors::SERVICE_UNAVAILABLE, false);
emit finished(false, err);
return;
}
//REALLY UPLOAD echo queued parts;
uint64_t sentBytes = 0;
int partNum = 1;
//auto buf = Aws::MakeShared<Aws::Utils::Array<uint64_t>>("LOCALSTREAM", BUFFERSIZE);
Aws::Utils::Array<uint8_t> buffer(BUFFERSIZE);
while (sentBytes < m_totalSize && this->shouldContinue() && partNum <= partCount) {
//auto buffer = qobject_cast<QS3Client*>(parent())->m_bufferManager.Acquire();
PartState *p = m_queueMap[partNum];
uint64_t offset = p->rangeBegin;
uint64_t lengthToWrite = p->sizeInBytes;
fileStream->seekg(offset);
fileStream->read((char *)buffer.GetUnderlyingData(), lengthToWrite);
//
auto streamBuf = Aws::New<Aws::Utils::Stream::PreallocatedStreamBuf>("LOCALSTREAM", reinterpret_cast<unsigned char*>(buffer.GetUnderlyingData()), static_cast<size_t>(lengthToWrite));
auto preallocatedStreamReader = Aws::MakeShared<Aws::IOStream>("LOCALSTREAM", streamBuf);
Aws::S3::Model::UploadPartRequest uploadPartRequest;
//
uploadPartRequest.SetPartNumber(partNum);
uploadPartRequest.SetBucket(m_bucketName);
uploadPartRequest.SetKey(m_keyName);
uploadPartRequest.SetContentLength(lengthToWrite);
uploadPartRequest.SetUploadId(m_uploadId);
uploadPartRequest.SetContentType(m_contenttype);
//
uploadPartRequest.SetContinueRequestHandler([this](const Aws::Http::HttpRequest *) {
return this->shouldContinue();
});
uploadPartRequest.SetDataSentEventHandler([this, partNum](const Aws::Http::HttpRequest *,
long long amount) {
m_totalTransfered += amount;
emit updateProgress(m_totalTransfered, m_totalSize);
});
uploadPartRequest.SetBody(preallocatedStreamReader);
qDebug() << "uploading " << "PartNum :" << p->partID;
auto uploadPartOutcome = m_client->UploadPart(uploadPartRequest);
//anyway, release the buffer;
//qobject_cast<QS3Client*>(parent())->m_bufferManager.Release(buffer);
Aws::Delete(streamBuf);
if (uploadPartOutcome.IsSuccess()) {
emit updateStatus(TransferStatus::IN_PROGRESS);
p->etag = uploadPartOutcome.GetResult().GetETag();
p->success = true;
qDebug() << "\nPartNum :" << p->partID << "Size: " << p->sizeInBytes << "completed";
} else {
emit updateStatus(TransferStatus::FAILED);
s3error err = uploadPartOutcome.GetError();
m_status.store((static_cast<long>(TransferStatus::FAILED)));
emit finished(false, err);
return;
}
sentBytes += lengthToWrite;
partNum ++;
}
Aws::S3::Model::CompletedMultipartUpload completedUpload;
for (int i = 1; i <= partCount; i++) {
//if not all the part is uploaded
PartState *p = m_queueMap[i];
if (!p->success) {
s3error err(Aws::S3::S3Errors::NETWORK_CONNECTION, false);
emit updateStatus(TransferStatus::FAILED);
emit finished(false, err);
return;
}
Aws::S3::Model::CompletedPart completedPart;
completedPart.WithPartNumber(p->partID).WithETag(p->etag);
completedUpload.AddParts(completedPart);
}
//clean up m_queueMap
for (auto &p : m_queueMap) {
delete p;
}
//ready to commit the upload
Aws::S3::Model::CompleteMultipartUploadRequest completeMultipartUploadRequest;
completeMultipartUploadRequest.SetContinueRequestHandler([this](const Aws::Http::HttpRequest * p) {
return this->shouldContinue();
});
completeMultipartUploadRequest.WithBucket(m_bucketName)
.WithKey(m_keyName)
.WithUploadId(m_uploadId)
.WithMultipartUpload(completedUpload);
auto completeUploadOutcome = m_client->CompleteMultipartUpload(completeMultipartUploadRequest);
if (completeUploadOutcome.IsSuccess()) {
qDebug() << "commit upload success";
m_status.store((static_cast<long>(TransferStatus::COMPLETED)));
emit updateStatus(TransferStatus::COMPLETED);
emit finished(true, s3error());
return;
} else {
if (m_cancel.load() == true) {
m_status.store((static_cast<long>(TransferStatus::CANCELED)));
emit updateStatus(TransferStatus::CANCELED);
} else {
m_status.store((static_cast<long>(TransferStatus::FAILED)));
emit updateStatus(TransferStatus::FAILED);
}
emit finished(false, s3error());
}
}
void UploadObjectHandler::doSinglePartUpload(const std::shared_ptr<Aws::IOStream> &fileStream)
{
//should run in thread pool;
Aws::S3::Model::PutObjectRequest putObjectRequest;
putObjectRequest.SetContinueRequestHandler([this](const Aws::Http::HttpRequest *) {
return this->shouldContinue();
});
putObjectRequest.WithBucket(m_bucketName).WithKey(m_keyName);
qDebug() << "content type" << m_contenttype.c_str();
putObjectRequest.SetContentType(m_contenttype);
putObjectRequest.SetBody(fileStream);
putObjectRequest.SetDataSentEventHandler([this](const Aws::Http::HttpRequest *,
long long progress) {
m_totalTransfered += static_cast<uint64_t>(progress);
emit updateProgress(m_totalTransfered, m_totalSize);
});
//TODO
//putObjectRequest.SetRequestRetryHandler();
qDebug() << "UPLOADING to" << "BUCKETNAME" << m_bucketName.c_str() << "OBJECTNAME" <<
m_keyName.c_str();
auto putObjectOutcome = m_client->PutObject(putObjectRequest);
qDebug() << "do upload truely";
s3error err;
if (putObjectOutcome.IsSuccess()) {
m_status.store((static_cast<long>(TransferStatus::COMPLETED)));
emit updateStatus(TransferStatus::COMPLETED);
emit finished(true, err);
return;
} else {
if (m_cancel.load() == true) {
m_status.store(static_cast<long>(TransferStatus::CANCELED));
emit updateStatus(TransferStatus::CANCELED);
} else {
m_status.store(static_cast<long>(TransferStatus::FAILED));
emit updateStatus(TransferStatus::FAILED);
}
emit finished(false, putObjectOutcome.GetError());
return;
}
}
DownloadObjectHandler::DownloadObjectHandler(QObject *parent, std::shared_ptr<S3Client> client,
const QString &bucketName,
const QString &keyName, const QString &writeToFile):
ObjectHandlerInterface(parent), m_client(client)
{
m_status.store(static_cast<long>(TransferStatus::NOT_STARTED));
m_bucketName = QString2AwsString(bucketName);
m_keyName = QString2AwsString(keyName);
m_writeToFile = writeToFile;
m_cancel.store(true);
m_totalTransfered = 0;
}
int DownloadObjectHandler::start()
{
if (m_status.load() == static_cast<long>(TransferStatus::IN_PROGRESS))
return -1;
m_cancel.store(false);
//TODO put into threadpool
//set your self DO NOT AUTODELETE by threadpool
std::function<void()> f = [this]() {
this->doDownload();
};
future = QtConcurrent::run(f);
m_status.store(static_cast<long>(TransferStatus::IN_PROGRESS));
emit updateStatus(TransferStatus::IN_PROGRESS);
}
void DownloadObjectHandler::stop()
{
qDebug() << "DownloadObjectHandler set to stop";
m_cancel.store(true);
//MAY trigger something
}
void DownloadObjectHandler::waitForFinish()
{
future.waitForFinished();
}
void DownloadObjectHandler::doDownload()
{
Aws::S3::Model::HeadObjectRequest headObjectRequest;
//qDebug() << m_bucketName;
headObjectRequest.WithBucket(m_bucketName).WithKey(m_keyName);
auto headObjectOutcome = m_client->HeadObject(headObjectRequest);
//no such file in S3
if (!headObjectOutcome.IsSuccess()) {
std::cout << headObjectOutcome.GetError().GetExceptionName() << " " <<
headObjectOutcome.GetError().GetMessage() << std::endl;
std::cout << headObjectOutcome.GetError().GetMessage();
std::cout << headObjectOutcome.GetResult().GetContentLength();
std::cout << int(headObjectOutcome.GetError().GetErrorType());
qDebug() << "enter do download11";
m_status.store(static_cast<long>(TransferStatus::FAILED));
emit updateStatus(TransferStatus::FAILED);
emit finished(false, headObjectOutcome.GetError());
return;
} else {
if (m_cancel.load() == true) {
m_status.store(static_cast<long>(TransferStatus::CANCELED));
emit updateStatus(TransferStatus::CANCELED);
} else {
m_totalSize = headObjectOutcome.GetResult().GetContentLength();
m_totalTransfered = 0;
}
}
Aws::S3::Model::GetObjectRequest request;
request.WithBucket(m_bucketName).WithKey(m_keyName);
request.SetContinueRequestHandler([this](const Aws::Http::HttpRequest *) {
return !this->m_cancel.load();
});
//read localfile,
//always try to append this
// this fstream was used to implement continuing downloading via "bytes=pos-"
// will be freed soon
Aws::FStream *fstream = Aws::New<Aws::FStream>("LOCALSTREAM",
m_writeToFile.toLocal8Bit().constData(),
std::ios_base::out | std::ios_base::app | std::ios_base::binary | std::ios_base::ate);
//errno??
if (!fstream->good()) {
m_status.store(static_cast<long>(TransferStatus::FAILED));
s3error err(Aws::S3::S3Errors::NO_SUCH_UPLOAD, false);
err.SetMessage(QString2AwsString("Not such File :" + m_writeToFile));
emit updateStatus(TransferStatus::FAILED);
emit finished(false, err);
qDebug() << "Not such file :" << m_writeToFile;
return;
}
auto pos = fstream->tellp();
// fstream is useless. first close(), then free()
fstream->close();
Aws::Free(fstream);
Aws::StringStream ss;
ss << "bytes=" << pos << "-";
if (m_totalSize != 0)
request.SetRange(ss.str());
if (m_totalSize != 0 && m_totalSize == pos) {
m_status.store(static_cast<long>(TransferStatus::EXACT_OBJECT_ALREADY_EXISTS));
emit updateStatus(TransferStatus::EXACT_OBJECT_ALREADY_EXISTS);
emit finished(false, s3error());
return;
}
m_totalTransfered += pos;
request.SetDataReceivedEventHandler([this](const Aws::Http::HttpRequest *,
Aws::Http::HttpResponse *, long long progress) {
m_totalTransfered += static_cast<uint64_t>(progress);
emit updateProgress(m_totalTransfered, m_totalSize);
});
/* Never pass outer fstream into SetResponseStreamFactory(), because aws-lib will free fstream,
* and continue to invoke GetResponseStreamFactory(), thus crash...
*/
//request is responsible to close and delete the fstream;
auto responseFactory = [ = ]() {
//return fstream;
Aws::FStream *fstream = Aws::New< Aws::FStream >("LOCALSTREAM",
m_writeToFile.toLocal8Bit().constData(),
std::ios_base::out | std::ios_base::app | std::ios_base::binary | std::ios_base::ate);
return fstream;
};
request.SetResponseStreamFactory(responseFactory);
emit updateStatus(TransferStatus::IN_PROGRESS);
auto getObjectOutcome = m_client->GetObject(request);
if (getObjectOutcome.IsSuccess()) {
m_status.store(static_cast<long>(TransferStatus::COMPLETED));
emit updateStatus(TransferStatus::COMPLETED);
emit finished(true, s3error());
return;
} else {
//could be canceled or failed
if (m_cancel.load() == true) {
m_status.store(static_cast<long>(TransferStatus::CANCELED));
emit updateStatus(TransferStatus::CANCELED);
} else {
m_status.store(static_cast<long>(TransferStatus::FAILED));
emit updateStatus(TransferStatus::FAILED);
}
emit finished(true, getObjectOutcome.GetError());
return;
}
}
}//namespace qlibs3