oss_callback_sample.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include "aos_log.h"
  2. #include "aos_util.h"
  3. #include "aos_string.h"
  4. #include "aos_status.h"
  5. #include "oss_auth.h"
  6. #include "oss_util.h"
  7. #include "oss_api.h"
  8. #include "oss_config.h"
  9. #include "oss_sample_util.h"
  10. void put_object_from_buffer_with_callback()
  11. {
  12. aos_pool_t *p = NULL;
  13. char *str = "test oss c sdk";
  14. aos_status_t *s = NULL;
  15. int is_cname = 0;
  16. aos_string_t bucket;
  17. aos_string_t object;
  18. aos_table_t *headers = NULL;
  19. oss_request_options_t *options = NULL;
  20. aos_table_t *resp_headers = NULL;
  21. aos_list_t resp_body;
  22. aos_list_t buffer;
  23. aos_buf_t *content;
  24. char *buf = NULL;
  25. int64_t len = 0;
  26. int64_t size = 0;
  27. int64_t pos = 0;
  28. char b64_buf[1024];
  29. int b64_len;
  30. /* JSON format */
  31. char *callback = "{"
  32. "\"callbackUrl\":\"http://callback.oss-demo.com:23450\","
  33. "\"callbackHost\":\"oss-cn-hangzhou.aliyuncs.com\","
  34. "\"callbackBody\":\"bucket=${bucket}&object=${object}&size=${size}&mimeType=${mimeType}\","
  35. "\"callbackBodyType\":\"application/x-www-form-urlencoded\""
  36. "}";
  37. /* init sample */
  38. aos_pool_create(&p, NULL);
  39. options = oss_request_options_create(p);
  40. init_sample_request_options(options, is_cname);
  41. aos_str_set(&bucket, BUCKET_NAME);
  42. aos_str_set(&object, OBJECT_NAME);
  43. aos_list_init(&resp_body);
  44. aos_list_init(&buffer);
  45. content = aos_buf_pack(options->pool, str, strlen(str));
  46. aos_list_add_tail(&content->node, &buffer);
  47. /* put call into header */
  48. b64_len = aos_base64_encode((unsigned char*)callback, strlen(callback), b64_buf);
  49. b64_buf[b64_len] = '\0';
  50. headers = aos_table_make(p, 1);
  51. apr_table_set(headers, OSS_CALLBACK, b64_buf);
  52. /* test put object */
  53. s = oss_do_put_object_from_buffer(options, &bucket, &object, &buffer,
  54. headers, NULL, NULL, &resp_headers, &resp_body);
  55. if (aos_status_is_ok(s)) {
  56. printf("put object from buffer succeeded\n");
  57. } else {
  58. printf("put object from buffer failed\n");
  59. }
  60. /* get buffer len */
  61. len = aos_buf_list_len(&resp_body);
  62. buf = (char *)aos_pcalloc(p, (apr_size_t)(len + 1));
  63. buf[len] = '\0';
  64. /* copy buffer content to memory */
  65. aos_list_for_each_entry(aos_buf_t, content, &resp_body, node) {
  66. size = aos_buf_size(content);
  67. memcpy(buf + pos, content->pos, (size_t)size);
  68. pos += size;
  69. }
  70. aos_pool_destroy(p);
  71. }
  72. void multipart_from_buffer_with_callback()
  73. {
  74. aos_pool_t *p = NULL;
  75. aos_string_t bucket;
  76. aos_string_t object;
  77. int is_cname = 0;
  78. oss_request_options_t *options = NULL;
  79. aos_status_t *s = NULL;
  80. aos_list_t buffer;
  81. oss_list_upload_part_params_t *params = NULL;
  82. aos_string_t upload_id;
  83. aos_list_t complete_part_list;
  84. aos_table_t *headers = NULL;
  85. aos_table_t *resp_headers = NULL;
  86. oss_list_part_content_t *part_content1 = NULL;
  87. oss_complete_part_content_t *complete_content1 = NULL;
  88. int part_num = 1;
  89. aos_list_t resp_body;
  90. aos_buf_t *content;
  91. char *buf = NULL;
  92. int64_t len = 0;
  93. int64_t size = 0;
  94. int64_t pos = 0;
  95. char b64_buf[1024];
  96. int b64_len;
  97. /* JSON format */
  98. char *callback = "{"
  99. "\"callbackUrl\":\"http://callback.oss-demo.com:23450\","
  100. "\"callbackHost\":\"oss-cn-hangzhou.aliyuncs.com\","
  101. "\"callbackBody\":\"bucket=${bucket}&object=${object}&size=${size}&mimeType=${mimeType}\","
  102. "\"callbackBodyType\":\"application/x-www-form-urlencoded\""
  103. "}";
  104. aos_pool_create(&p, NULL);
  105. options = oss_request_options_create(p);
  106. init_sample_request_options(options, is_cname);
  107. aos_str_set(&bucket, BUCKET_NAME);
  108. aos_str_set(&object, OBJECT_NAME);
  109. /* put call into header */
  110. b64_len = aos_base64_encode((unsigned char*)callback, strlen(callback), b64_buf);
  111. b64_buf[b64_len] = '\0';
  112. headers = aos_table_make(p, 3);
  113. apr_table_set(headers, OSS_CALLBACK, b64_buf);
  114. apr_table_set(headers, OSS_CALLBACK, b64_buf);
  115. apr_table_set(headers, OSS_CALLBACK, b64_buf);
  116. /* init mulitipart */
  117. s = oss_init_multipart_upload(options, &bucket, &object, &upload_id, NULL, &resp_headers);
  118. if (aos_status_is_ok(s)) {
  119. printf("Init multipart upload succeeded, upload_id:%.*s\n",
  120. upload_id.len, upload_id.data);
  121. } else {
  122. printf("Init multipart upload failed\n");
  123. aos_pool_destroy(p);
  124. return;
  125. }
  126. /* upload part */
  127. aos_list_init(&buffer);
  128. make_random_body(p, 2, &buffer);
  129. s = oss_upload_part_from_buffer(options, &bucket, &object, &upload_id,
  130. part_num++, &buffer, NULL);
  131. if (aos_status_is_ok(s)) {
  132. printf("Multipart upload part from file succeeded\n");
  133. } else {
  134. printf("Multipart upload part from file failed\n");
  135. aos_pool_destroy(p);
  136. return;
  137. }
  138. aos_list_init(&buffer);
  139. make_random_body(p, 200, &buffer);
  140. s = oss_upload_part_from_buffer(options, &bucket, &object, &upload_id,
  141. part_num++, &buffer, NULL);
  142. if (aos_status_is_ok(s)) {
  143. printf("Multipart upload part from file succeeded\n");
  144. } else {
  145. printf("Multipart upload part from file failed\n");
  146. aos_pool_destroy(p);
  147. return;
  148. }
  149. /* list part */
  150. params = oss_create_list_upload_part_params(p);
  151. params->max_ret = 1;
  152. aos_list_init(&complete_part_list);
  153. s = oss_list_upload_part(options, &bucket, &object, &upload_id,
  154. params, NULL);
  155. if (aos_status_is_ok(s)) {
  156. printf("List multipart succeeded\n");
  157. } else {
  158. printf("List multipart failed\n");
  159. aos_pool_destroy(p);
  160. return;
  161. }
  162. aos_list_for_each_entry(oss_list_part_content_t, part_content1, &params->part_list, node) {
  163. complete_content1 = oss_create_complete_part_content(p);
  164. aos_str_set(&complete_content1->part_number, part_content1->part_number.data);
  165. aos_str_set(&complete_content1->etag, part_content1->etag.data);
  166. aos_list_add_tail(&complete_content1->node, &complete_part_list);
  167. }
  168. /* complete multipart */
  169. s = oss_do_complete_multipart_upload(options, &bucket, &object, &upload_id,
  170. &complete_part_list, headers, NULL, &resp_headers, &resp_body);
  171. if (aos_status_is_ok(s)) {
  172. printf("Complete multipart upload from file succeeded, upload_id:%.*s\n",
  173. upload_id.len, upload_id.data);
  174. } else {
  175. printf("Complete multipart upload from file failed\n");
  176. printf("%d, %s, %s, %s\n", s->code, s->error_code, s->error_msg, s->req_id);
  177. }
  178. /* get buffer len */
  179. len = aos_buf_list_len(&resp_body);
  180. buf = (char *)aos_pcalloc(p, (apr_size_t)(len + 1));
  181. buf[len] = '\0';
  182. /* copy buffer content to memory */
  183. aos_list_for_each_entry(aos_buf_t, content, &resp_body, node) {
  184. size = aos_buf_size(content);
  185. memcpy(buf + pos, content->pos, (size_t)size);
  186. pos += size;
  187. }
  188. aos_pool_destroy(p);
  189. }
  190. void callback_sample()
  191. {
  192. put_object_from_buffer_with_callback();
  193. multipart_from_buffer_with_callback();
  194. }