-
Notifications
You must be signed in to change notification settings - Fork 13
/
cos_demo.c
3207 lines (2772 loc) · 119 KB
/
cos_demo.c
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "cos_http_io.h"
#include "cos_api.h"
#include "cos_log.h"
#include <stdint.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
// endpoint 是 COS 访问域名信息,详情请参见 https://cloud.tencent.com/document/product/436/6224 文档
static char TEST_COS_ENDPOINT[] = "cos.ap-guangzhou.myqcloud.com";
// 数据万象的访问域名,详情请参见 https://cloud.tencent.com/document/product/460/31066 文档
static char TEST_CI_ENDPOINT[] = "https://ci.ap-guangzhou.myqcloud.com";
// 开发者拥有的项目身份ID/密钥,可在 https://console.cloud.tencent.com/cam/capi 页面获取
static char *TEST_ACCESS_KEY_ID; //your secret_id
static char *TEST_ACCESS_KEY_SECRET; //your secret_key
// 开发者访问 COS 服务时拥有的用户维度唯一资源标识,用以标识资源,可在 https://console.cloud.tencent.com/cam/capi 页面获取
static char TEST_APPID[] = "<APPID>"; //your appid
//the cos bucket name, syntax: [bucket]-[appid], for example: mybucket-1253666666,可在 https://console.cloud.tencent.com/cos5/bucket 查看
static char TEST_BUCKET_NAME[] = "<bucketname-appid>";
// 对象拥有者,比如用户UIN:100000000001
static char TEST_UIN[] = "<Uin>"; //your uin
// 地域信息,枚举值可参见 https://cloud.tencent.com/document/product/436/6224 文档,例如:ap-beijing、ap-hongkong、eu-frankfurt 等
static char TEST_REGION[] = "ap-guangzhou"; //region in endpoint
// 对象键,对象(Object)在存储桶(Bucket)中的唯一标识。有关对象与对象键的进一步说明,请参见 https://cloud.tencent.com/document/product/436/13324 文档
static char TEST_OBJECT_NAME1[] = "1.txt";
static char TEST_OBJECT_NAME2[] = "test2.dat";
static char TEST_OBJECT_NAME3[] = "test3.dat";
static char TEST_OBJECT_NAME4[] = "multipart.txt";
//static char TEST_DOWNLOAD_NAME2[] = "download_test2.dat";
static char *TEST_APPEND_NAMES[] = {"test.7z.001", "test.7z.002"};
static char TEST_DOWNLOAD_NAME3[] = "download_test3.dat";
static char TEST_MULTIPART_OBJECT[] = "multipart.dat";
static char TEST_DOWNLOAD_NAME4[] = "multipart_download.dat";
static char TEST_MULTIPART_FILE[] = "test.zip";
//static char TEST_MULTIPART_OBJECT2[] = "multipart2.dat";
static char TEST_MULTIPART_OBJECT3[] = "multipart3.dat";
static char TEST_MULTIPART_OBJECT4[] = "multipart4.dat";
static void print_headers(cos_table_t *headers)
{
const cos_array_header_t *tarr;
const cos_table_entry_t *telts;
int i = 0;
if (apr_is_empty_table(headers)) {
return;
}
tarr = cos_table_elts(headers);
telts = (cos_table_entry_t*)tarr->elts;
printf("headers:\n");
for (; i < tarr->nelts; i++) {
telts = (cos_table_entry_t*)(tarr->elts + i * tarr->elt_size);
printf("%s: %s\n", telts->key, telts->val);
}
}
void init_test_config(cos_config_t *config, int is_cname)
{
cos_str_set(&config->endpoint, TEST_COS_ENDPOINT);
cos_str_set(&config->access_key_id, TEST_ACCESS_KEY_ID);
cos_str_set(&config->access_key_secret, TEST_ACCESS_KEY_SECRET);
cos_str_set(&config->appid, TEST_APPID);
config->is_cname = is_cname;
}
void init_test_request_options(cos_request_options_t *options, int is_cname)
{
options->config = cos_config_create(options->pool);
init_test_config(options->config, is_cname);
options->ctl = cos_http_controller_create(options->pool, 0);
}
void log_status(cos_status_t *s)
{
cos_warn_log("status->code: %d", s->code);
if (s->error_code) cos_warn_log("status->error_code: %s", s->error_code);
if (s->error_msg) cos_warn_log("status->error_msg: %s", s->error_msg);
if (s->req_id) cos_warn_log("status->req_id: %s", s->req_id);
}
void test_sign()
{
cos_pool_t *p = NULL;
const unsigned char secret_key[] = "your secret_key";
const unsigned char time_str[] = "1480932292;1481012292";
unsigned char sign_key[40];
cos_buf_t *fmt_str;
const char * value = NULL;
const char * uri = "/testfile";
const char * host = "testbucket-125000000.cn-north.myqcloud.com&range=bytes%3d0-3";
unsigned char fmt_str_hex[40];
cos_pool_create(&p, NULL);
fmt_str = cos_create_buf(p, 1024);
cos_get_hmac_sha1_hexdigest(sign_key, secret_key, sizeof(secret_key)-1, time_str, sizeof(time_str)-1);
char * pstr = apr_pstrndup(p, (char*)sign_key, sizeof(sign_key));
cos_warn_log("sign_key: %s", pstr);
// method
value = "get";
cos_buf_append_string(p, fmt_str, value, strlen(value));
cos_buf_append_string(p, fmt_str, "\n", sizeof("\n")-1);
// canonicalized resource(URI)
cos_buf_append_string(p, fmt_str, uri, strlen(uri));
cos_buf_append_string(p, fmt_str, "\n", sizeof("\n")-1);
// query-parameters
cos_buf_append_string(p, fmt_str, "\n", sizeof("\n")-1);
// Host
cos_buf_append_string(p, fmt_str, "host=", sizeof("host=")-1);
cos_buf_append_string(p, fmt_str, host, strlen(host));
cos_buf_append_string(p, fmt_str, "\n", sizeof("\n")-1);
char * pstr3 = apr_pstrndup(p, (char*)fmt_str->pos, cos_buf_size(fmt_str));
cos_warn_log("Format string: %s", pstr3);
// Format-String sha1hash
cos_get_sha1_hexdigest(fmt_str_hex, (unsigned char*)fmt_str->pos, cos_buf_size(fmt_str));
char * pstr2 = apr_pstrndup(p, (char*)fmt_str_hex, sizeof(fmt_str_hex));
cos_warn_log("Format string sha1hash: %s", pstr2);
cos_pool_destroy(p);
}
void test_bucket()
{
cos_pool_t *p = NULL;
int is_cname = 0;
cos_status_t *s = NULL;
cos_request_options_t *options = NULL;
cos_acl_e cos_acl = COS_ACL_PRIVATE;
cos_string_t bucket;
cos_table_t *resp_headers = NULL;
cos_pool_create(&p, NULL);
options = cos_request_options_create(p);
init_test_request_options(options, is_cname);
cos_str_set(&bucket, TEST_BUCKET_NAME);
//create test bucket
s = cos_create_bucket(options, &bucket, cos_acl, &resp_headers);
log_status(s);
//list object (get bucket)
cos_list_object_params_t *list_params = NULL;
list_params = cos_create_list_object_params(p);
cos_str_set(&list_params->encoding_type, "url");
s = cos_list_object(options, &bucket, list_params, &resp_headers);
log_status(s);
cos_list_object_content_t *content = NULL;
char *line = NULL;
cos_list_for_each_entry(cos_list_object_content_t, content, &list_params->object_list, node) {
line = apr_psprintf(p, "%.*s\t%.*s\t%.*s\n", content->key.len, content->key.data,
content->size.len, content->size.data,
content->last_modified.len, content->last_modified.data);
printf("%s", line);
printf("next marker: %s\n", list_params->next_marker.data);
}
cos_list_object_common_prefix_t *common_prefix = NULL;
cos_list_for_each_entry(cos_list_object_common_prefix_t, common_prefix, &list_params->common_prefix_list, node) {
printf("common prefix: %s\n", common_prefix->prefix.data);
}
//delete bucket
s = cos_delete_bucket(options, &bucket, &resp_headers);
log_status(s);
cos_pool_destroy(p);
}
void test_list_objects()
{
cos_pool_t *p = NULL;
int is_cname = 0;
cos_status_t *s = NULL;
cos_request_options_t *options = NULL;
cos_string_t bucket;
cos_table_t *resp_headers = NULL;
//创建内存池
cos_pool_create(&p, NULL);
//初始化请求选项
options = cos_request_options_create(p);
init_test_request_options(options, is_cname);
cos_str_set(&bucket, TEST_BUCKET_NAME);
//获取对象列表
cos_list_object_params_t *list_params = NULL;
cos_list_object_content_t *content = NULL;
list_params = cos_create_list_object_params(p);
s = cos_list_object(options, &bucket, list_params, &resp_headers);
if (cos_status_is_ok(s)) {
printf("list object succeeded\n");
cos_list_for_each_entry(cos_list_object_content_t, content, &list_params->object_list, node) {
printf("object: %.*s\n", content->key.len, content->key.data);
}
} else {
printf("list object failed\n");
}
log_status(s);
//销毁内存池
cos_pool_destroy(p);
}
void test_bucket_lifecycle()
{
cos_pool_t *p = NULL;
int is_cname = 0;
cos_status_t *s = NULL;
cos_request_options_t *options = NULL;
cos_string_t bucket;
cos_table_t *resp_headers = NULL;
cos_pool_create(&p, NULL);
options = cos_request_options_create(p);
init_test_request_options(options, is_cname);
cos_str_set(&bucket, TEST_BUCKET_NAME);
cos_list_t rule_list;
cos_list_init(&rule_list);
cos_lifecycle_rule_content_t *rule_content = NULL;
rule_content = cos_create_lifecycle_rule_content(p);
cos_str_set(&rule_content->id, "testrule1");
cos_str_set(&rule_content->prefix, "abc/");
cos_str_set(&rule_content->status, "Enabled");
rule_content->expire.days = 365;
cos_list_add_tail(&rule_content->node, &rule_list);
rule_content = cos_create_lifecycle_rule_content(p);
cos_str_set(&rule_content->id, "testrule2");
cos_str_set(&rule_content->prefix, "efg/");
cos_str_set(&rule_content->status, "Disabled");
cos_str_set(&rule_content->transition.storage_class, "Standard_IA");
rule_content->transition.days = 999;
cos_list_add_tail(&rule_content->node, &rule_list);
rule_content = cos_create_lifecycle_rule_content(p);
cos_str_set(&rule_content->id, "testrule3");
cos_str_set(&rule_content->prefix, "xxx/");
cos_str_set(&rule_content->status, "Enabled");
rule_content->abort.days = 1;
cos_list_add_tail(&rule_content->node, &rule_list);
s = cos_put_bucket_lifecycle(options, &bucket, &rule_list, &resp_headers);
log_status(s);
cos_list_t rule_list_ret;
cos_list_init(&rule_list_ret);
s = cos_get_bucket_lifecycle(options, &bucket, &rule_list_ret, &resp_headers);
log_status(s);
cos_delete_bucket_lifecycle(options, &bucket, &resp_headers);
log_status(s);
cos_pool_destroy(p);
}
void test_put_object_with_limit()
{
cos_pool_t *p = NULL;
int is_cname = 0;
cos_status_t *s = NULL;
cos_request_options_t *options = NULL;
cos_string_t bucket;
cos_string_t object;
cos_string_t file;
cos_table_t *resp_headers = NULL;
cos_table_t *headers = NULL;
//创建内存池
cos_pool_create(&p, NULL);
//初始化请求选项
options = cos_request_options_create(p);
init_test_request_options(options, is_cname);
cos_str_set(&bucket, TEST_BUCKET_NAME);
//限速值设置范围为819200 - 838860800,即100KB/s - 100MB/s,如果超出该范围将返回400错误
headers = cos_table_make(p, 1);
cos_table_add_int(headers, "x-cos-traffic-limit", 819200);
//上传对象
cos_str_set(&file, "test_file.bin");
cos_str_set(&object, TEST_OBJECT_NAME1);
s = cos_put_object_from_file(options, &bucket, &object, &file, headers, &resp_headers);
if (cos_status_is_ok(s)) {
printf("put object succeeded\n");
} else {
printf("put object failed\n");
}
log_status(s);
//销毁内存池
cos_pool_destroy(p);
}
void test_get_object_with_limit()
{
cos_pool_t *p = NULL;
int is_cname = 0;
cos_status_t *s = NULL;
cos_request_options_t *options = NULL;
cos_string_t bucket;
cos_string_t object;
cos_string_t file;
cos_table_t *resp_headers = NULL;
cos_table_t *headers = NULL;
//创建内存池
cos_pool_create(&p, NULL);
//初始化请求选项
options = cos_request_options_create(p);
init_test_request_options(options, is_cname);
cos_str_set(&bucket, TEST_BUCKET_NAME);
//限速值设置范围为819200 - 838860800,即100KB/s - 100MB/s,如果超出该范围将返回400错误
headers = cos_table_make(p, 1);
cos_table_add_int(headers, "x-cos-traffic-limit", 819200);
//下载对象
cos_str_set(&file, "test_file.bin");
cos_str_set(&object, TEST_OBJECT_NAME1);
s = cos_get_object_to_file(options, &bucket, &object, headers, NULL, &file, &resp_headers);
if (cos_status_is_ok(s)) {
printf("get object succeeded\n");
} else {
printf("get object failed\n");
}
log_status(s);
//销毁内存池
cos_pool_destroy(p);
}
void test_gen_object_url()
{
cos_pool_t *p = NULL;
int is_cname = 0;
cos_request_options_t *options = NULL;
cos_string_t bucket;
cos_string_t object;
cos_pool_create(&p, NULL);
options = cos_request_options_create(p);
init_test_request_options(options, is_cname);
cos_str_set(&bucket, TEST_BUCKET_NAME);
cos_str_set(&object, TEST_OBJECT_NAME1);
printf("url:%s\n", cos_gen_object_url(options, &bucket, &object));
cos_pool_destroy(p);
}
void test_create_dir()
{
cos_pool_t *p = NULL;
int is_cname = 0;
cos_status_t *s = NULL;
cos_request_options_t *options = NULL;
cos_string_t bucket;
cos_string_t object;
cos_table_t *resp_headers;
cos_table_t *headers = NULL;
cos_list_t buffer;
cos_pool_create(&p, NULL);
options = cos_request_options_create(p);
init_test_request_options(options, is_cname);
cos_str_set(&bucket, TEST_BUCKET_NAME);
cos_str_set(&object, "folder/");
//上传文件夹
cos_list_init(&buffer);
s = cos_put_object_from_buffer(options, &bucket, &object,
&buffer, headers, &resp_headers);
if (cos_status_is_ok(s)) {
printf("put object succeeded\n");
} else {
printf("put object failed\n");
}
log_status(s);
cos_pool_destroy(p);
}
void test_object()
{
cos_pool_t *p = NULL;
int is_cname = 0;
cos_status_t *s = NULL;
cos_request_options_t *options = NULL;
cos_string_t bucket;
cos_string_t object;
cos_table_t *resp_headers;
cos_table_t *headers = NULL;
cos_list_t buffer;
cos_buf_t *content = NULL;
char * str = "This is my test data.";
cos_string_t file;
int traffic_limit = 0;
cos_pool_create(&p, NULL);
options = cos_request_options_create(p);
init_test_request_options(options, is_cname);
cos_str_set(&bucket, TEST_BUCKET_NAME);
cos_str_set(&object, TEST_OBJECT_NAME1);
cos_list_init(&buffer);
content = cos_buf_pack(options->pool, str, strlen(str));
cos_list_add_tail(&content->node, &buffer);
s = cos_put_object_from_buffer(options, &bucket, &object,
&buffer, headers, &resp_headers);
log_status(s);
cos_list_t download_buffer;
cos_list_init(&download_buffer);
if (traffic_limit) {
// 限速值设置范围为819200 - 838860800,即100KB/s - 100MB/s,如果超出该范围将返回400错误
headers = cos_table_make(p, 1);
cos_table_add_int(headers, "x-cos-traffic-limit", 819200);
}
s = cos_get_object_to_buffer(options, &bucket, &object,
headers, NULL, &download_buffer, &resp_headers);
log_status(s);
print_headers(resp_headers);
int64_t len = 0;
int64_t size = 0;
int64_t pos = 0;
cos_list_for_each_entry(cos_buf_t, content, &download_buffer, node) {
len += cos_buf_size(content);
}
char *buf = cos_pcalloc(p, (apr_size_t)(len + 1));
buf[len] = '\0';
cos_list_for_each_entry(cos_buf_t, content, &download_buffer, node) {
size = cos_buf_size(content);
memcpy(buf + pos, content->pos, (size_t)size);
pos += size;
}
cos_warn_log("Download data=%s", buf);
cos_str_set(&file, TEST_OBJECT_NAME4);
cos_str_set(&object, TEST_OBJECT_NAME4);
s = cos_put_object_from_file(options, &bucket, &object, &file, NULL, &resp_headers);
log_status(s);
cos_str_set(&file, TEST_DOWNLOAD_NAME3);
cos_str_set(&object, TEST_OBJECT_NAME3);
s = cos_get_object_to_file(options, &bucket, &object, NULL, NULL, &file, &resp_headers);
log_status(s);
cos_str_set(&object, TEST_OBJECT_NAME2);
s = cos_head_object(options, &bucket, &object, NULL, &resp_headers);
log_status(s);
cos_str_set(&object, TEST_OBJECT_NAME1);
s = cos_delete_object(options, &bucket, &object, &resp_headers);
log_status(s);
cos_str_set(&object, TEST_OBJECT_NAME3);
s = cos_delete_object(options, &bucket, &object, &resp_headers);
log_status(s);
cos_pool_destroy(p);
}
void test_append_object()
{
cos_pool_t *p = NULL;
int is_cname = 0;
cos_status_t *s = NULL;
cos_request_options_t *options = NULL;
cos_string_t bucket;
cos_string_t object;
cos_string_t file;
cos_table_t *resp_headers = NULL;
//创建内存池
cos_pool_create(&p, NULL);
//初始化请求选项
options = cos_request_options_create(p);
init_test_request_options(options, is_cname);
cos_str_set(&bucket, TEST_BUCKET_NAME);
//追加上传对象
cos_str_set(&object, TEST_OBJECT_NAME3);
int32_t count = sizeof(TEST_APPEND_NAMES)/sizeof(char*);
int32_t index = 0;
int64_t position = 0;
s = cos_head_object(options, &bucket, &object, NULL, &resp_headers);
if(s->code == 200) {
char *content_length_str = (char*)apr_table_get(resp_headers, COS_CONTENT_LENGTH);
if (content_length_str != NULL) {
position = atol(content_length_str);
}
}
for (; index < count; index++)
{
cos_str_set(&file, TEST_APPEND_NAMES[index]);
s = cos_append_object_from_file(options, &bucket, &object,
position, &file, NULL, &resp_headers);
log_status(s);
s = cos_head_object(options, &bucket, &object, NULL, &resp_headers);
if(s->code == 200) {
char *content_length_str = (char*)apr_table_get(resp_headers, COS_CONTENT_LENGTH);
if (content_length_str != NULL) {
position = atol(content_length_str);
}
}
}
//销毁内存池
cos_pool_destroy(p);
}
void test_head_object()
{
cos_pool_t *p = NULL;
int is_cname = 0;
cos_status_t *s = NULL;
cos_request_options_t *options = NULL;
cos_string_t bucket;
cos_string_t object;
cos_table_t *resp_headers = NULL;
//创建内存池
cos_pool_create(&p, NULL);
//初始化请求选项
options = cos_request_options_create(p);
init_test_request_options(options, is_cname);
cos_str_set(&bucket, TEST_BUCKET_NAME);
//获取对象元数据
cos_str_set(&object, TEST_OBJECT_NAME1);
s = cos_head_object(options, &bucket, &object, NULL, &resp_headers);
print_headers(resp_headers);
log_status(s);
if (cos_status_is_ok(s)) {
printf("head object succeeded\n");
} else {
printf("head object failed\n");
}
//销毁内存池
cos_pool_destroy(p);
}
void test_check_object_exist()
{
cos_pool_t *p = NULL;
int is_cname = 0;
cos_status_t *s = NULL;
cos_request_options_t *options = NULL;
cos_string_t bucket;
cos_string_t object;
cos_table_t *resp_headers;
cos_table_t *headers = NULL;
cos_object_exist_status_e object_exist;
cos_pool_create(&p, NULL);
options = cos_request_options_create(p);
init_test_request_options(options, is_cname);
cos_str_set(&bucket, TEST_BUCKET_NAME);
cos_str_set(&object, TEST_OBJECT_NAME1);
// 检查对象是否存在
s = cos_check_object_exist(options, &bucket, &object, headers, &object_exist, &resp_headers);
if (object_exist == COS_OBJECT_NON_EXIST) {
printf("object: %.*s non exist.\n", object.len, object.data);
} else if (object_exist == COS_OBJECT_EXIST) {
printf("object: %.*s exist.\n", object.len, object.data);
} else {
printf("object: %.*s unknown status.\n", object.len, object.data);
log_status(s);
}
cos_pool_destroy(p);
}
void test_object_restore()
{
cos_pool_t *p = NULL;
cos_string_t bucket;
cos_string_t object;
int is_cname = 0;
cos_table_t *resp_headers = NULL;
cos_request_options_t *options = NULL;
cos_status_t *s = NULL;
cos_pool_create(&p, NULL);
options = cos_request_options_create(p);
init_test_request_options(options, is_cname);
cos_str_set(&bucket, TEST_BUCKET_NAME);
cos_str_set(&object, "test_restore.dat");
cos_object_restore_params_t *restore_params = cos_create_object_restore_params(p);
restore_params->days = 30;
cos_str_set(&restore_params->tier, "Standard");
s = cos_post_object_restore(options, &bucket, &object, restore_params, NULL, NULL, &resp_headers);
log_status(s);
cos_pool_destroy(p);
}
void progress_callback(int64_t consumed_bytes, int64_t total_bytes)
{
printf("consumed_bytes = %"APR_INT64_T_FMT", total_bytes = %"APR_INT64_T_FMT"\n", consumed_bytes, total_bytes);
}
void test_put_object_from_file()
{
cos_pool_t *p = NULL;
int is_cname = 0;
cos_status_t *s = NULL;
cos_request_options_t *options = NULL;
cos_string_t bucket;
cos_string_t object;
cos_table_t *resp_headers;
cos_string_t file;
int traffic_limit = 0;
cos_pool_create(&p, NULL);
options = cos_request_options_create(p);
init_test_request_options(options, is_cname);
cos_table_t *headers = NULL;
if (traffic_limit) {
// 限速值设置范围为819200 - 838860800,即100KB/s - 100MB/s,如果超出该范围将返回400错误
headers = cos_table_make(p, 1);
cos_table_add_int(headers, "x-cos-traffic-limit", 819200);
}
cos_str_set(&bucket, TEST_BUCKET_NAME);
cos_str_set(&file, TEST_OBJECT_NAME4);
cos_str_set(&object, TEST_OBJECT_NAME4);
s = cos_put_object_from_file(options, &bucket, &object, &file, headers, &resp_headers);
log_status(s);
cos_pool_destroy(p);
}
void test_put_object_from_file_with_sse()
{
cos_pool_t *p = NULL;
int is_cname = 0;
cos_status_t *s = NULL;
cos_request_options_t *options = NULL;
cos_string_t bucket;
cos_string_t object;
cos_table_t *resp_headers;
cos_string_t file;
cos_pool_create(&p, NULL);
options = cos_request_options_create(p);
init_test_request_options(options, is_cname);
cos_table_t *headers = NULL;
headers = cos_table_make(p, 3);
// apr_table_add(headers, "x-cos-server-side-encryption", "AES256");
apr_table_add(headers, "x-cos-server-side-encryption-customer-algorithm", "AES256");
apr_table_add(headers, "x-cos-server-side-encryption-customer-key", "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=");
apr_table_add(headers, "x-cos-server-side-encryption-customer-key-MD5", "U5L61r7jcwdNvT7frmUG8g==");
cos_str_set(&bucket, TEST_BUCKET_NAME);
cos_str_set(&file, "/home/jojoliang/data/test.jpg");
cos_str_set(&object, "pic");
s = cos_put_object_from_file(options, &bucket, &object, &file, headers, &resp_headers);
log_status(s);
{
int i = 0;
apr_array_header_t * pp = (apr_array_header_t *) apr_table_elts(resp_headers);
for ( ; i < pp->nelts; i++)
{
apr_table_entry_t *ele = (apr_table_entry_t *)pp->elts+i;
printf("%s: %s\n",ele->key,ele->val);
}
}
cos_pool_destroy(p);
}
void test_get_object_to_file_with_sse()
{
cos_pool_t *p = NULL;
int is_cname = 0;
cos_status_t *s = NULL;
cos_request_options_t *options = NULL;
cos_string_t bucket;
cos_string_t object;
cos_table_t *resp_headers;
cos_string_t file;
cos_table_t *headers = NULL;
cos_table_t *params = NULL;
cos_pool_create(&p, NULL);
options = cos_request_options_create(p);
init_test_request_options(options, is_cname);
headers = cos_table_make(p, 3);
/*
apr_table_add(headers, "x-cos-server-side-encryption", "AES256");
*/
/*
apr_table_add(headers, "x-cos-server-side-encryption-customer-algorithm", "AES256");
apr_table_add(headers, "x-cos-server-side-encryption-customer-key", "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=");
apr_table_add(headers, "x-cos-server-side-encryption-customer-key-MD5", "U5L61r7jcwdNvT7frmUG8g==");
*/
cos_str_set(&bucket, TEST_BUCKET_NAME);
cos_str_set(&file, "getfile");
cos_str_set(&object, TEST_OBJECT_NAME1);
s = cos_get_object_to_file(options, &bucket, &object, headers, params, &file, &resp_headers);
log_status(s);
{
int i = 0;
apr_array_header_t * pp = (apr_array_header_t *) apr_table_elts(resp_headers);
for ( ; i < pp->nelts; i++)
{
apr_table_entry_t *ele = (apr_table_entry_t *)pp->elts+i;
printf("%s: %s\n",ele->key,ele->val);
}
}
cos_pool_destroy(p);
}
void multipart_upload_file_from_file()
{
cos_pool_t *p = NULL;
cos_string_t bucket;
cos_string_t object;
int is_cname = 0;
cos_table_t *headers = NULL;
cos_table_t *complete_headers = NULL;
cos_table_t *resp_headers = NULL;
cos_request_options_t *options = NULL;
cos_string_t upload_id;
cos_upload_file_t *upload_file = NULL;
cos_status_t *s = NULL;
cos_list_upload_part_params_t *params = NULL;
cos_list_t complete_part_list;
cos_list_part_content_t *part_content = NULL;
cos_complete_part_content_t *complete_part_content = NULL;
int part_num = 1;
int64_t pos = 0;
int64_t file_length = 0;
cos_pool_create(&p, NULL);
options = cos_request_options_create(p);
init_test_request_options(options, is_cname);
headers = cos_table_make(p, 1);
complete_headers = cos_table_make(p, 1);
cos_str_set(&bucket, TEST_BUCKET_NAME);
cos_str_set(&object, TEST_MULTIPART_OBJECT);
//init mulitipart
s = cos_init_multipart_upload(options, &bucket, &object,
&upload_id, headers, &resp_headers);
log_status(s);
if (cos_status_is_ok(s)) {
printf("Init multipart upload succeeded, upload_id:%.*s\n",
upload_id.len, upload_id.data);
} else {
printf("Init multipart upload failed\n");
cos_pool_destroy(p);
return;
}
//upload part from file
int res = COSE_OK;
cos_file_buf_t *fb = cos_create_file_buf(p);
res = cos_open_file_for_all_read(p, TEST_MULTIPART_FILE, fb);
if (res != COSE_OK) {
cos_error_log("Open read file fail, filename:%s\n", TEST_MULTIPART_FILE);
return;
}
file_length = fb->file_last;
apr_file_close(fb->file);
while(pos < file_length) {
upload_file = cos_create_upload_file(p);
cos_str_set(&upload_file->filename, TEST_MULTIPART_FILE);
upload_file->file_pos = pos;
pos += 2 * 1024 * 1024;
upload_file->file_last = pos < file_length ? pos : file_length; //2MB
s = cos_upload_part_from_file(options, &bucket, &object, &upload_id,
part_num++, upload_file, &resp_headers);
if (cos_status_is_ok(s)) {
printf("Multipart upload part from file succeeded\n");
} else {
printf("Multipart upload part from file failed\n");
}
}
//list part
params = cos_create_list_upload_part_params(p);
params->max_ret = 1000;
cos_list_init(&complete_part_list);
s = cos_list_upload_part(options, &bucket, &object, &upload_id,
params, &resp_headers);
if (cos_status_is_ok(s)) {
printf("List multipart succeeded\n");
cos_list_for_each_entry(cos_list_part_content_t, part_content, ¶ms->part_list, node) {
printf("part_number = %s, size = %s, last_modified = %s, etag = %s\n",
part_content->part_number.data,
part_content->size.data,
part_content->last_modified.data,
part_content->etag.data);
}
} else {
printf("List multipart failed\n");
cos_pool_destroy(p);
return;
}
cos_list_for_each_entry(cos_list_part_content_t, part_content, ¶ms->part_list, node) {
complete_part_content = cos_create_complete_part_content(p);
cos_str_set(&complete_part_content->part_number, part_content->part_number.data);
cos_str_set(&complete_part_content->etag, part_content->etag.data);
cos_list_add_tail(&complete_part_content->node, &complete_part_list);
}
//complete multipart
s = cos_complete_multipart_upload(options, &bucket, &object, &upload_id,
&complete_part_list, complete_headers, &resp_headers);
log_status(s);
if (cos_status_is_ok(s)) {
printf("Complete multipart upload from file succeeded, upload_id:%.*s\n",
upload_id.len, upload_id.data);
} else {
printf("Complete multipart upload from file failed\n");
}
cos_pool_destroy(p);
}
void multipart_upload_file_from_buffer()
{
cos_pool_t *p = NULL;
cos_string_t bucket;
cos_string_t object;
int is_cname = 0;
cos_table_t *headers = NULL;
cos_table_t *complete_headers = NULL;
cos_table_t *resp_headers = NULL;
cos_request_options_t *options = NULL;
cos_string_t upload_id;
cos_status_t *s = NULL;
cos_list_t complete_part_list;
cos_complete_part_content_t *complete_part_content = NULL;
cos_pool_create(&p, NULL);
options = cos_request_options_create(p);
init_test_request_options(options, is_cname);
headers = cos_table_make(p, 1);
complete_headers = cos_table_make(p, 1);
cos_str_set(&bucket, TEST_BUCKET_NAME);
cos_str_set(&object, TEST_MULTIPART_OBJECT);
//init mulitipart
s = cos_init_multipart_upload(options, &bucket, &object,
&upload_id, headers, &resp_headers);
log_status(s);
if (cos_status_is_ok(s)) {
printf("Init multipart upload succeeded, upload_id:%.*s\n",
upload_id.len, upload_id.data);
} else {
printf("Init multipart upload failed\n");
cos_pool_destroy(p);
return;
}
//upload part from buffer
char *str = "This is my test data....";
cos_list_t buffer;
cos_buf_t *content;
// 上传一个分块
cos_list_init(&buffer);
content = cos_buf_pack(p, str, strlen(str));
cos_list_add_tail(&content->node, &buffer);
s = cos_upload_part_from_buffer(options, &bucket, &object, &upload_id, 1, &buffer, &resp_headers);
log_status(s);
// 直接获取etag
char *etag = apr_pstrdup(p, (char*)apr_table_get(resp_headers, "ETag"));
cos_list_init(&complete_part_list);
complete_part_content = cos_create_complete_part_content(p);
cos_str_set(&complete_part_content->part_number, "1");
cos_str_set(&complete_part_content->etag, etag);
cos_list_add_tail(&complete_part_content->node, &complete_part_list);
// 也可以通过 list part 获取取etag
/*
//list part
params = cos_create_list_upload_part_params(p);
params->max_ret = 1000;
cos_list_init(&complete_part_list);
s = cos_list_upload_part(options, &bucket, &object, &upload_id,
params, &resp_headers);
if (cos_status_is_ok(s)) {
printf("List multipart succeeded\n");
cos_list_for_each_entry(cos_list_part_content_t, part_content, ¶ms->part_list, node) {
printf("part_number = %s, size = %s, last_modified = %s, etag = %s\n",
part_content->part_number.data,
part_content->size.data,
part_content->last_modified.data,
part_content->etag.data);
}
} else {
printf("List multipart failed\n");
cos_pool_destroy(p);
return;
}
cos_list_for_each_entry(cos_list_part_content_t, part_content, ¶ms->part_list, node) {
complete_part_content = cos_create_complete_part_content(p);
cos_str_set(&complete_part_content->part_number, part_content->part_number.data);
cos_str_set(&complete_part_content->etag, part_content->etag.data);
cos_list_add_tail(&complete_part_content->node, &complete_part_list);
}
*/
//complete multipart
s = cos_complete_multipart_upload(options, &bucket, &object, &upload_id,
&complete_part_list, complete_headers, &resp_headers);
log_status(s);
if (cos_status_is_ok(s)) {
printf("Complete multipart upload from file succeeded, upload_id:%.*s\n",
upload_id.len, upload_id.data);
} else {
printf("Complete multipart upload from file failed\n");
}
cos_pool_destroy(p);
}
void abort_multipart_upload()
{
cos_pool_t *p = NULL;
cos_string_t bucket;
cos_string_t object;
int is_cname = 0;
cos_table_t *headers = NULL;
cos_table_t *resp_headers = NULL;
cos_request_options_t *options = NULL;
cos_string_t upload_id;
cos_status_t *s = NULL;
cos_pool_create(&p, NULL);
options = cos_request_options_create(p);
init_test_request_options(options, is_cname);
headers = cos_table_make(p, 1);
cos_str_set(&bucket, TEST_BUCKET_NAME);
cos_str_set(&object, TEST_MULTIPART_OBJECT);
s = cos_init_multipart_upload(options, &bucket, &object,
&upload_id, headers, &resp_headers);
log_status(s);
if (cos_status_is_ok(s)) {
printf("Init multipart upload succeeded, upload_id:%.*s\n",
upload_id.len, upload_id.data);
} else {
printf("Init multipart upload failed\n");
cos_pool_destroy(p);
return;
}
s = cos_abort_multipart_upload(options, &bucket, &object, &upload_id,
&resp_headers);
log_status(s);
if (cos_status_is_ok(s)) {
printf("Abort multipart upload succeeded, upload_id::%.*s\n",
upload_id.len, upload_id.data);
} else {
printf("Abort multipart upload failed\n");
}
cos_pool_destroy(p);
}
void list_multipart()