1 /*
2  * Copyright (C) 2015-2021 Alibaba Group Holding Limited
3  */
4 
5 #include "OssClient.h"
6 #include "iostream"
7 #include "fstream"
8 #include "string.h"
9 #include "oss_app.h"
10 #include "aos/vfs.h"
11 #include "fcntl.h"
12 
13 #ifdef USE_SD_FOR_OSS
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <errno.h>
19 #include <signal.h>
20 #include "aos/kernel.h"
21 
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include "ulog/ulog.h"
26 int sd_fd;
27 #endif
28 
29 using namespace AlibabaCloud::OSS;
30 
ProgressCallback(size_t increment,int64_t transfered,int64_t total,void * userData)31 static void ProgressCallback(size_t increment, int64_t transfered, int64_t total, void* userData)
32 {
33     std::cout << "ProgressCallback[" << userData << "] => " <<
34               increment <<" ," << transfered << "," << total << std::endl;
35 }
36 
getFileSize(const std::string & file)37 static int64_t getFileSize(const std::string& file)
38 {
39     std::fstream f(file, std::ios::in | std::ios::binary);
40     f.seekg(0, f.end);
41     int64_t size = f.tellg();
42     f.close();
43     return size;
44 }
45 
46 extern "C"{
47 
48 #ifdef USE_SD_FOR_OSS
sd_file_open(char * read_file)49 int sd_file_open(char * read_file)
50 {
51     struct stat s;
52     const char file[30]={0};
53 
54     if(!read_file){
55         memcpy(file,"/sdcard/test.h264",strlen("/sdcard/test.h264"));
56     }
57     else{
58         memcpy(file,read_file,strlen(read_file));
59     }
60     LOG("open file %s",file);
61     sd_fd = open(file, O_RDONLY);
62     if (sd_fd < 0) {
63         LOG("Failed to open file %s\r\n", file);
64         return -1;
65     }
66     stat(file,&s);
67     return s.st_size;
68 }
69 
sd_file_close(void)70 int sd_file_close(void)
71 {
72     close(sd_fd);
73 }
74 
sd_file_read(unsigned char * buf,int read_size)75 void sd_file_read(unsigned char * buf,int read_size)
76 {
77     int rc, i;
78 
79     rc = read(sd_fd, buf, read_size);
80     if (rc < 0) {
81         LOG("Failed to read file\r\n");
82     }
83 
84     return;
85 }
86 #endif
87 
88 extern Url g_ags_url;
89 std::string g_url;
oss_upload_local_file(char * keyId,char * keySecret,char * endPoint,char * bucketName,char * localfilepath)90 char* oss_upload_local_file(char *keyId, char *keySecret, char *endPoint, char *bucketName, char* localfilepath)
91 {
92     /* 初始化OSS账号信息 */
93     std::string AccessKeyId;
94     std::string AccessKeySecret;
95     std::string Endpoint;
96     std::string BucketName;
97     /* yourObjectName表示上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg */
98     std::string ObjectName ;
99 
100     char *p_url;
101     char *pfile_path,file_path[256];
102 
103     if((keyId == NULL)||(keySecret == NULL)||(endPoint == NULL)||(bucketName == NULL)||(localfilepath == NULL))
104     {
105         return NULL;
106     }
107 
108     AccessKeyId = keyId;
109     AccessKeySecret = keySecret;
110     Endpoint = endPoint;
111     BucketName = bucketName;
112 
113 #ifdef OSS_DEBUG
114     std::cout << "Input_AccessKeyId:" << AccessKeyId <<std::endl;
115     std::cout << "Input_AccessKeySecret:" << AccessKeySecret <<std::endl;
116     std::cout << "Input_Endpoint:" << Endpoint <<std::endl;
117     std::cout << "Input_BucketName:" << BucketName <<std::endl;
118 #endif
119     memset(file_path,0,256);
120     pfile_path = localfilepath;
121     strncpy(file_path,&pfile_path[1],strlen(pfile_path)-1);
122     ObjectName = file_path;
123 
124 #ifdef USE_SD_FOR_OSS
125     int file_total_size;
126     char *alloc_file_content;
127     std::string sContent;
128 
129     /* 初始化网络等资源 */
130     InitializeSdk();
131 
132     ClientConfiguration conf;
133     OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
134 
135     /* 上传文件 */
136 #ifdef OSS_DEBUG
137     std::cout << "objectfile_path:" << ObjectName <<std::endl;
138     std::cout << "localfile_path:" << localfilepath <<std::endl;
139 #endif
140     file_total_size = sd_file_open(localfilepath);
141     if(file_total_size > READ_SD_SIZE_MAX){
142         LOG("---SD open file size too Large %d > 10K",file_total_size);
143         sd_file_close();
144         ShutdownSdk();
145         return NULL;
146     }
147     LOG("SD open file size <%d>.",file_total_size);
148     alloc_file_content = (unsigned char *) aos_malloc(file_total_size);
149     if(!alloc_file_content){
150         LOG("malloc err");
151         aos_free(alloc_file_content);
152         sd_file_close();
153         ShutdownSdk();
154         return NULL;
155     }
156     sd_file_read(alloc_file_content,file_total_size);
157     sContent = alloc_file_content;
158     std::shared_ptr<std::iostream> content = std::make_shared<std::stringstream>();
159     *content << sContent;
160 
161     PutObjectRequest request(BucketName, ObjectName, content);
162     TransferProgress progressCallback = { ProgressCallback };
163     request.setTransferProgress(progressCallback);
164 
165     auto outcome = client.PutObject(request);
166     g_url = g_ags_url.toString();
167     std::cout << "oss ->url:" << g_url << std::endl;
168     p_url = (char *)(g_url.data());
169 
170     if (!outcome.isSuccess()) {
171         /* 异常处理 */
172         std::cout << "PutObject fail" <<
173                   ",code:" << outcome.error().Code() <<
174                   ",message:" << outcome.error().Message() <<
175                   ",requestId:" << outcome.error().RequestId() << std::endl;
176         aos_free(alloc_file_content);
177         sd_file_close();
178         ShutdownSdk();
179         return NULL;
180     }
181     std::cout << __FUNCTION__ << " success, ETag:" << outcome.result().ETag() << std::endl;
182     /* 释放网络等资源 */
183     aos_free(alloc_file_content);
184     sd_file_close();
185     ShutdownSdk();
186     return p_url;
187 #else
188     /* 初始化网络等资源 */
189     InitializeSdk();
190 
191     ClientConfiguration conf;
192     OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
193     //OssClient client(Endpoint, AccessKeyId, AccessKeySecret,SecretToken, conf);
194 
195     /* 上传文件 */
196     auto fileSize = getFileSize(localfilepath);
197 #ifdef OSS_DEBUG
198     std::cout << "objectfile_path:" << ObjectName <<std::endl;
199     std::cout << "localfile_path:" << localfilepath <<std::endl;
200     std::cout << "localfile_path size:" << fileSize <<std::endl;
201 #endif
202 
203     std::shared_ptr<std::iostream> content = std::make_shared<std::fstream>(localfilepath, std::ios::in | std::ios::binary);
204     PutObjectRequest request(BucketName, ObjectName, content);
205     TransferProgress progressCallback = { ProgressCallback };
206     request.setTransferProgress(progressCallback);
207 
208     auto outcome = client.PutObject(request);
209     g_url = g_ags_url.toString();
210     std::cout << "oss ->url:" << g_url << std::endl;
211     p_url = (char *)(g_url.data());
212 
213     if (!outcome.isSuccess()) {
214         /* 异常处理 */
215         std::cout << "PutObject fail" <<
216                   ",code:" << outcome.error().Code() <<
217                   ",message:" << outcome.error().Message() <<
218                   ",requestId:" << outcome.error().RequestId() << std::endl;
219         ShutdownSdk();
220         return NULL;
221     }
222     std::cout << __FUNCTION__ << " success, ETag:" << outcome.result().ETag() << std::endl;
223     /* 释放网络等资源 */
224     ShutdownSdk();
225     return p_url;
226 #endif
227 }
228 
oss_upload_file(char * keyId,char * keySecret,char * endPoint,char * bucketName,char * objectName,char * localfilepath)229 char* oss_upload_file(char *keyId, char *keySecret, char *endPoint, char *bucketName, char *objectName, char* localfilepath)
230 {
231     /* 初始化OSS账号信息 */
232     std::string AccessKeyId;
233     std::string AccessKeySecret;
234     std::string Endpoint;
235     std::string BucketName;
236     /* yourObjectName表示上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg */
237     std::string ObjectName ;
238 
239     char *p_url;
240 
241     if((keyId == NULL)||(keySecret == NULL)||(endPoint == NULL)||(bucketName == NULL)||(localfilepath == NULL))
242     {
243         return NULL;
244     }
245 
246     AccessKeyId = keyId;
247     AccessKeySecret = keySecret;
248     Endpoint = endPoint;
249     BucketName = bucketName;
250     ObjectName = objectName;
251 
252 #ifdef OSS_DEBUG
253     std::cout << "Input_AccessKeyId:" << AccessKeyId <<std::endl;
254     std::cout << "Input_AccessKeySecret:" << AccessKeySecret <<std::endl;
255     std::cout << "Input_Endpoint:" << Endpoint <<std::endl;
256     std::cout << "Input_BucketName:" << BucketName <<std::endl;
257 #endif
258 
259 #ifdef USE_SD_FOR_OSS
260     int file_total_size;
261     char *alloc_file_content;
262     std::string sContent;
263 
264     /* 初始化网络等资源 */
265     InitializeSdk();
266 
267     ClientConfiguration conf;
268     OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
269 
270     /* 上传文件 */
271 #ifdef OSS_DEBUG
272     std::cout << "objectfile_path:" << ObjectName <<std::endl;
273     std::cout << "localfile_path:" << localfilepath <<std::endl;
274 #endif
275     file_total_size = sd_file_open(localfilepath);
276     if(file_total_size > READ_SD_SIZE_MAX){
277         LOG("---SD open file size too Large %d > 10K",file_total_size);
278         sd_file_close();
279         ShutdownSdk();
280         return NULL;
281     }
282     LOG("SD open file size <%d>.",file_total_size);
283     alloc_file_content = (unsigned char *) aos_malloc(file_total_size);
284     if(!alloc_file_content){
285         LOG("malloc err");
286         aos_free(alloc_file_content);
287         sd_file_close();
288         ShutdownSdk();
289         return NULL;
290     }
291     sd_file_read(alloc_file_content,file_total_size);
292     sContent = alloc_file_content;
293     std::shared_ptr<std::iostream> content = std::make_shared<std::stringstream>();
294     *content << sContent;
295 
296     PutObjectRequest request(BucketName, ObjectName, content);
297     TransferProgress progressCallback = { ProgressCallback };
298     request.setTransferProgress(progressCallback);
299 
300     auto outcome = client.PutObject(request);
301     g_url = g_ags_url.toString();
302     std::cout << "oss ->url:" << g_url << std::endl;
303     p_url = (char *)(g_url.data());
304 
305     if (!outcome.isSuccess()) {
306         /* 异常处理 */
307         std::cout << "PutObject fail" <<
308                   ",code:" << outcome.error().Code() <<
309                   ",message:" << outcome.error().Message() <<
310                   ",requestId:" << outcome.error().RequestId() << std::endl;
311         aos_free(alloc_file_content);
312         sd_file_close();
313         ShutdownSdk();
314         return NULL;
315     }
316     std::cout << __FUNCTION__ << " success, ETag:" << outcome.result().ETag() << std::endl;
317     /* 释放网络等资源 */
318     aos_free(alloc_file_content);
319     sd_file_close();
320     ShutdownSdk();
321     return p_url;
322 #else
323     /* 初始化网络等资源 */
324     InitializeSdk();
325 
326     ClientConfiguration conf;
327     OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
328     //OssClient client(Endpoint, AccessKeyId, AccessKeySecret,SecretToken, conf);
329 
330     /* 上传文件 */
331     auto fileSize = getFileSize(localfilepath);
332 #ifdef OSS_DEBUG
333     std::cout << "objectfile_path:" << ObjectName <<std::endl;
334     std::cout << "localfile_path:" << localfilepath <<std::endl;
335     std::cout << "localfile_path size:" << fileSize <<std::endl;
336 #endif
337 
338     std::shared_ptr<std::iostream> content = std::make_shared<std::fstream>(localfilepath, std::ios::in | std::ios::binary);
339     PutObjectRequest request(BucketName, ObjectName, content);
340     TransferProgress progressCallback = { ProgressCallback };
341     request.setTransferProgress(progressCallback);
342 
343     auto outcome = client.PutObject(request);
344     g_url = g_ags_url.toString();
345     std::cout << "oss ->url:" << g_url << std::endl;
346     p_url = (char *)(g_url.data());
347 
348     if (!outcome.isSuccess()) {
349         /* 异常处理 */
350         std::cout << "PutObject fail" <<
351                   ",code:" << outcome.error().Code() <<
352                   ",message:" << outcome.error().Message() <<
353                   ",requestId:" << outcome.error().RequestId() << std::endl;
354         ShutdownSdk();
355         return NULL;
356     }
357     std::cout << __FUNCTION__ << " success, ETag:" << outcome.result().ETag() << std::endl;
358     /* 释放网络等资源 */
359     ShutdownSdk();
360     return p_url;
361 #endif
362 }
363 
oss_upload_local_content(char * keyId,char * keySecret,char * endPoint,char * bucketName,char * scontent)364 char* oss_upload_local_content(char *keyId, char *keySecret, char *endPoint, char *bucketName, char *scontent)
365 {
366     /* 初始化OSS账号信息 */
367     std::string AccessKeyId;
368     std::string AccessKeySecret;
369     std::string Endpoint;
370     std::string BucketName;
371     /* yourObjectName表示上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg */
372     std::string ObjectName ;
373 
374     std::string sContent;
375 
376     if((keyId == NULL)||(keySecret == NULL)||(endPoint == NULL)||(bucketName == NULL))
377     {
378         return NULL;
379     }
380     AccessKeyId = keyId;
381     AccessKeySecret = keySecret;
382     Endpoint = endPoint;
383     BucketName = bucketName;
384 
385     if(scontent == NULL){
386         sContent = "Welcome to HaaS";
387     }
388     else{
389         sContent = scontent;
390     }
391 
392 #ifdef OSS_DEBUG
393     std::cout << "Input_AccessKeyId:" << AccessKeyId <<std::endl;
394     std::cout << "Input_AccessKeySecret:" << AccessKeySecret <<std::endl;
395     std::cout << "Input_Endpoint:" << Endpoint <<std::endl;
396     std::cout << "Input_BucketName:" << BucketName <<std::endl;
397 #endif
398 
399     /* 初始化网络等资源 */
400     InitializeSdk();
401 
402     ClientConfiguration conf;
403     OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
404 
405     /* 上传文件 */
406     std::shared_ptr<std::iostream> content = std::make_shared<std::stringstream>();
407     *content << sContent;
408 
409     ObjectName = "oss/test/oss.txt";
410     std::cout << "objectfile_path:" << ObjectName <<std::endl;
411     PutObjectRequest request(BucketName, ObjectName, content);
412 
413     TransferProgress progressCallback = { ProgressCallback };
414     request.setTransferProgress(progressCallback);
415 
416     auto outcome = client.PutObject(request);
417     g_url = g_ags_url.toString();
418     std::cout << "oss ->url:" << g_url << std::endl;
419 
420     if (!outcome.isSuccess()) {
421         /* 异常处理 */
422         std::cout << "PutObject fail: " <<
423                   ",code:" << outcome.error().Code() <<
424                   ",message:" << outcome.error().Message() <<
425                   ",requestId:" << outcome.error().RequestId() << std::endl;
426         ShutdownSdk();
427         return NULL;
428     }
429     std::cout << __FUNCTION__ << " success, ETag:" << outcome.result().ETag() << std::endl;
430     /* 释放网络等资源 */
431     ShutdownSdk();
432     return NULL;
433 }
434 
oss_download_file(char * keyId,char * keySecret,char * endPoint,char * bucketName,char * objectName,char * localfilepath)435 char* oss_download_file(char *keyId, char *keySecret, char *endPoint, char *bucketName, char *objectName, char* localfilepath)
436 {
437     /* 初始化OSS账号信息 */
438     std::string AccessKeyId;
439     std::string AccessKeySecret;
440     std::string Endpoint;
441     std::string BucketName;
442     /* yourObjectName表示上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg */
443     std::string ObjectName ;
444     /*设置下载文件的文件名*/
445     std::string FileNametoSave = "yourFileName";
446 
447     if((keyId == NULL)||(keySecret == NULL)||(endPoint == NULL)||(bucketName == NULL)||(localfilepath == NULL))
448     {
449         return NULL;
450     }
451 
452     AccessKeyId = keyId;
453     AccessKeySecret = keySecret;
454     Endpoint = endPoint;
455     BucketName = bucketName;
456     ObjectName = objectName;
457 
458     int fp = aos_open(localfilepath, O_RDWR | O_CREAT | O_TRUNC);
459     if (fp < 0) {
460         std::cout <<"open file fail\n";
461         return NULL;
462     }
463 
464     /*初始化网络等资源*/
465     InitializeSdk();
466 
467     ClientConfiguration conf;
468     OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
469 
470     /*获取文件到本地内存。*/
471     GetObjectRequest request(BucketName, ObjectName);
472     auto outcome = client.GetObject(request);
473     if (outcome.isSuccess()) {
474       std::cout << "getObjectToBuffer" << " success, Content-Length:" << outcome.result().Metadata().ContentLength() << std::endl;
475         /*通过read接口读取数据。*/
476         auto& stream = outcome.result().Content();
477         char buffer[256];
478         while (stream->good()) {
479             stream->read(buffer, 256);
480             auto count = stream->gcount();
481             /*根据实际情况处理数据。*/
482             int ret = aos_write(fp, buffer, count);
483             if (ret <= 0) {
484                 aos_close(fp);
485                 fp = -1;
486                 return NULL;
487             }
488 	    }
489     }
490     else {
491         /*异常处理。*/
492         std::cout << "getObjectToBuffer fail" <<
493         ",code:" << outcome.error().Code() <<
494         ",message:" << outcome.error().Message() <<
495         ",requestId:" << outcome.error().RequestId() << std::endl;
496         ShutdownSdk();
497         return NULL;
498     }
499     if (fp >= 0) {
500         aos_close(fp);
501     }
502 
503     /*释放网络等资源*/
504     ShutdownSdk();
505     return NULL;
506 }
507 
508 //cplusplus
509 }
510