-
Notifications
You must be signed in to change notification settings - Fork 0
/
awsxS3.cpp
281 lines (236 loc) · 9.79 KB
/
awsxS3.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
/***************************************************************************
* Copyright (C) 2022, Lanka Hsu, <lankahsu@gmail.com>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************/
#include <iostream>
//#include <aws/core/Aws.h>
//#include <aws/core/utils/Outcome.h>
#include <aws/s3/model/CopyObjectRequest.h>
#include <aws/s3/model/DeleteObjectRequest.h>
#include <aws/s3/model/GetObjectRequest.h>
#include <aws/s3/model/PutObjectRequest.h>
#include <fstream>
#include <memory>
#include "awsx9.h"
using namespace std;
static const char ALLOCATION_TAG[] = "awsxS3";
// bucket/key -> bucket/key
int s3_copy_object(S3_InfoX_t *s3_ctx)
{
int ret = 0;
if ((ret= S3_CTX_CHECK_CLI(s3_ctx)) == -1)
{
return ret;
}
if ((SAFE_STRLEN(s3_ctx->from.fileX.remote.bucket) <=0)
|| (SAFE_STRLEN(s3_ctx->from.fileX.remote.key) <=0)
|| (SAFE_STRLEN(s3_ctx->to.fileX.remote.bucket) <=0)
|| (SAFE_STRLEN(s3_ctx->to.fileX.remote.key) <=0))
{
size_t from_bucket_len = SAFE_STRLEN(s3_ctx->from.fileX.remote.bucket);
size_t from_key_len = SAFE_STRLEN(s3_ctx->from.fileX.remote.key);
size_t to_bucket_len = SAFE_STRLEN(s3_ctx->to.fileX.remote.bucket);
size_t to_key_len = SAFE_STRLEN(s3_ctx->to.fileX.remote.key);
DBG_ER_LN("Null Definition !!! (from_bucket_len: %zd, from_key_len: %zd, to_bucket_len: %zd, to_key_len: %zd)", from_bucket_len, from_key_len, to_bucket_len, to_key_len);
return -1;
}
DBG_IF_LN("(bucket: %s/%s -> bucket: %s/%s)", s3_ctx->from.fileX.remote.bucket, s3_ctx->from.fileX.remote.key, s3_ctx->to.fileX.remote.bucket, s3_ctx->to.fileX.remote.key);
Aws::S3::Model::CopyObjectRequest s3_copy_req;
// Set up the request.
Aws::String src_bucket_key(Aws::String(s3_ctx->from.fileX.remote.bucket) + "/" + Aws::String(s3_ctx->from.fileX.remote.key));
s3_copy_req.WithCopySource(src_bucket_key)
.WithKey(s3_ctx->to.fileX.remote.key)
.WithBucket(s3_ctx->to.fileX.remote.bucket);
Aws::S3::Model::CopyObjectOutcome s3_copy_res = s3_ctx->s3_cli->CopyObject(s3_copy_req);
if (s3_copy_res.IsSuccess())
{
DBG_IF_LN("CopyObject ok !!! (bucket: %s/%s -> bucket: %s/%s)", s3_ctx->from.fileX.remote.bucket, s3_ctx->from.fileX.remote.key, s3_ctx->to.fileX.remote.bucket, s3_ctx->to.fileX.remote.key);
}
else
{
DBG_ER_LN("CopyObject error - %s !!! (bucket: %s/%s -> bucket: %s/%s)", s3_copy_res.GetError().GetMessage().c_str(), s3_ctx->from.fileX.remote.bucket, s3_ctx->from.fileX.remote.key, s3_ctx->to.fileX.remote.bucket, s3_ctx->to.fileX.remote.key);
ret = -1;
}
return ret;
}
// bucket/key -> NULL
int s3_delete_object(S3_InfoX_t *s3_ctx)
{
int ret = 0;
if ((ret= S3_CTX_CHECK_CLI(s3_ctx)) == -1)
{
return ret;
}
if ((SAFE_STRLEN(s3_ctx->from.fileX.remote.bucket) <=0)
|| (SAFE_STRLEN(s3_ctx->from.fileX.remote.key) <=0))
{
size_t from_bucket_len = SAFE_STRLEN(s3_ctx->from.fileX.remote.bucket);
size_t from_key_len = SAFE_STRLEN(s3_ctx->from.fileX.remote.key);
DBG_ER_LN("Null Definition !!! (from_bucket_len: %zd, from_key_len: %zd)", from_bucket_len, from_key_len);
return -1;
}
DBG_IF_LN("(bucket: %s/%s -> NULL)", s3_ctx->from.fileX.remote.bucket, s3_ctx->from.fileX.remote.key);
Aws::S3::Model::DeleteObjectRequest s3_delete_req;
// Set up the request.
s3_delete_req.WithKey(s3_ctx->from.fileX.remote.key).WithBucket(s3_ctx->from.fileX.remote.bucket);
Aws::S3::Model::DeleteObjectOutcome s3_delete_res = s3_ctx->s3_cli->DeleteObject(s3_delete_req);
if (s3_delete_res.IsSuccess())
{
DBG_IF_LN("DeleteObject ok !!! (bucket: %s/%s -> NULL)", s3_ctx->from.fileX.remote.bucket, s3_ctx->from.fileX.remote.key);
}
else
{
DBG_ER_LN("DeleteObject error - %s !!! (bucket: %s/%s -> NULL)", s3_delete_res.GetError().GetMessage().c_str(), s3_ctx->from.fileX.remote.bucket, s3_ctx->from.fileX.remote.key);
ret = -1;
}
return ret;
}
// bucket/key -> local
// https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/configuring-iostreams.html
int s3_get_object(S3_InfoX_t *s3_ctx)
{
int ret = 0;
if ((ret= S3_CTX_CHECK_CLI(s3_ctx)) == -1)
{
return ret;
}
if ((SAFE_STRLEN(s3_ctx->from.fileX.remote.bucket) <=0)
|| (SAFE_STRLEN(s3_ctx->from.fileX.remote.key) <=0)
|| (SAFE_STRLEN(s3_ctx->to.fileX.localname) <=0))
{
size_t from_bucket_len = SAFE_STRLEN(s3_ctx->from.fileX.remote.bucket);
size_t from_key_len = SAFE_STRLEN(s3_ctx->from.fileX.remote.key);
size_t localname_len = SAFE_STRLEN(s3_ctx->to.fileX.localname);
DBG_ER_LN("Null Definition !!! (from_bucket_len: %zd, from_key_len: %zd, localname_len: %zd)", from_bucket_len, from_key_len, localname_len);
return -1;
}
DBG_IF_LN("(bucket: %s/%s -> localname: %s)", s3_ctx->from.fileX.remote.bucket, s3_ctx->from.fileX.remote.key, s3_ctx->to.fileX.localname);
Aws::S3::Model::GetObjectRequest s3_get_req;
// Set up the request.
s3_get_req.WithBucket(s3_ctx->from.fileX.remote.bucket).WithKey(s3_ctx->from.fileX.remote.key);
char *saveto = s3_ctx->to.fileX.localname;
s3_get_req.SetResponseStreamFactory([saveto] { return new std::fstream((const char*)saveto, std::ios_base::out); });
Aws::S3::Model::GetObjectOutcome s3_get_res = s3_ctx->s3_cli->GetObject(s3_get_req);
if (s3_get_res.IsSuccess())
{
DBG_IF_LN("GetObject ok !!! (bucket: %s/%s -> saveto: %s)", s3_ctx->from.fileX.remote.bucket, s3_ctx->from.fileX.remote.key, s3_ctx->to.fileX.localname);
}
else
{
DBG_ER_LN("GetObject error - %s !!! (bucket: %s/%s -> saveto: %s)", s3_get_res.GetError().GetMessage().c_str(), s3_ctx->from.fileX.remote.bucket, s3_ctx->from.fileX.remote.key, s3_ctx->to.fileX.localname);
ret = -1;
}
return ret;
}
// local -> bucket/key
int s3_put_object(S3_InfoX_t *s3_ctx)
{
int ret = 0;
if ((ret= S3_CTX_CHECK_CLI(s3_ctx)) == -1)
{
return ret;
}
if ((SAFE_STRLEN(s3_ctx->to.fileX.remote.bucket) <=0)
|| (SAFE_STRLEN(s3_ctx->to.fileX.remote.key) <=0)
|| (SAFE_STRLEN(s3_ctx->from.fileX.localname) <=0))
{
size_t to_bucket_len = SAFE_STRLEN(s3_ctx->to.fileX.remote.bucket);
size_t to_key_len = SAFE_STRLEN(s3_ctx->to.fileX.remote.key);
size_t localname_len = SAFE_STRLEN(s3_ctx->from.fileX.localname);
DBG_ER_LN("Null Definition !!! (localname_len: %zd, to_bucket_len: %zd, to_key_len: %zd)", localname_len, to_bucket_len, to_key_len);
return -1;
}
DBG_IF_LN("(localname: %s -> bucket: %s/%s)", s3_ctx->from.fileX.localname, s3_ctx->to.fileX.remote.bucket, s3_ctx->to.fileX.remote.key);
Aws::S3::Model::PutObjectRequest s3_put_req;
// Set up the request.
s3_put_req.WithBucket(s3_ctx->to.fileX.remote.bucket).WithKey(s3_ctx->to.fileX.remote.key);
char *fromfile = s3_ctx->from.fileX.localname;
std::shared_ptr<Aws::IOStream> inputData = Aws::MakeShared<Aws::FStream>(ALLOCATION_TAG, fromfile, std::ios_base::in | std::ios_base::binary);
if (!*inputData)
{
DBG_ER_LN("Read error !!! (fromfile: %s)", fromfile);
return -1;
}
s3_put_req.SetBody(inputData);
Aws::S3::Model::PutObjectOutcome s3_put_res = s3_ctx->s3_cli->PutObject(s3_put_req);
if (s3_put_res.IsSuccess())
{
DBG_IF_LN("PutObject ok !!! (localname: %s -> bucket: %s/%s)", s3_ctx->from.fileX.localname, s3_ctx->to.fileX.remote.bucket, s3_ctx->to.fileX.remote.key);
}
else
{
DBG_ER_LN("PutObject error - %s !!! (localname: %s -> bucket: %s/%s)", s3_put_res.GetError().GetMessage().c_str(), s3_ctx->from.fileX.localname, s3_ctx->to.fileX.remote.bucket, s3_ctx->to.fileX.remote.key);
ret = -1;
}
return ret;
}
void s3_ctx_init_copy(S3_InfoX_t *s3_ctx, char *from_bucket, char *from_key, char *to_bucket, char *to_key)
{
if ((s3_ctx) && (from_bucket) && (from_key) && (to_bucket) && (to_key))
{
SAFE_MEMSET(&s3_ctx->from, 0, sizeof(S3_FILEX_t));
SAFE_MEMSET(&s3_ctx->to, 0, sizeof(S3_FILEX_t));
SAFE_SPRINTF(s3_ctx->from.fileX.remote.bucket, "%s", from_bucket);
SAFE_SPRINTF(s3_ctx->from.fileX.remote.key, "%s", from_key);
SAFE_SPRINTF(s3_ctx->to.fileX.remote.bucket, "%s", to_bucket);
SAFE_SPRINTF(s3_ctx->to.fileX.remote.key, "%s", to_key);
}
}
void s3_ctx_init_delete(S3_InfoX_t *s3_ctx, char *from_bucket, char *from_key)
{
if ((s3_ctx) && (from_bucket) && (from_key))
{
SAFE_MEMSET(&s3_ctx->from, 0, sizeof(S3_FILEX_t));
SAFE_MEMSET(&s3_ctx->to, 0, sizeof(S3_FILEX_t));
SAFE_SPRINTF(s3_ctx->from.fileX.remote.bucket, "%s", from_bucket);
SAFE_SPRINTF(s3_ctx->from.fileX.remote.key, "%s", from_key);
}
}
void s3_ctx_init_get(S3_InfoX_t *s3_ctx, char *from_bucket, char *from_key, char *localname)
{
if ((s3_ctx) && (from_bucket) && (from_key) && (localname))
{
SAFE_MEMSET(&s3_ctx->from, 0, sizeof(S3_FILEX_t));
SAFE_MEMSET(&s3_ctx->to, 0, sizeof(S3_FILEX_t));
SAFE_SPRINTF(s3_ctx->from.fileX.remote.bucket, "%s", from_bucket);
SAFE_SPRINTF(s3_ctx->from.fileX.remote.key, "%s", from_key);
SAFE_SPRINTF(s3_ctx->to.fileX.localname, "%s", localname);
}
}
void s3_ctx_init_put(S3_InfoX_t *s3_ctx, char *localname, char *to_bucket, char *to_key)
{
if ((s3_ctx) && (to_bucket) && (to_key) && (localname))
{
SAFE_MEMSET(&s3_ctx->from, 0, sizeof(S3_FILEX_t));
SAFE_MEMSET(&s3_ctx->to, 0, sizeof(S3_FILEX_t));
SAFE_SPRINTF(s3_ctx->to.fileX.remote.bucket, "%s", to_bucket);
SAFE_SPRINTF(s3_ctx->to.fileX.remote.key, "%s", to_key);
SAFE_SPRINTF(s3_ctx->from.fileX.localname, "%s", localname);
}
}
void s3_ctx_free(S3_InfoX_t *s3_ctx)
{
if ((s3_ctx) && (s3_ctx->isfree == 0))
{
s3_ctx->isfree = 1;
}
}
void s3_ctx_init(S3_InfoX_t *s3_ctx, Aws::S3::S3Client *dydb_cli)
{
if ((s3_ctx) && (s3_ctx->isinit == 0))
{
s3_ctx->isinit = 1;
s3_ctx->isfree = 0;
s3_ctx->s3_cli = dydb_cli;
}
}