oss_image_sample.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. #if defined(WIN32)
  11. static char *image_file = "../oss_c_sdk_test/example.jpg";
  12. #else
  13. static char *image_file = "oss_c_sdk_test/example.jpg";
  14. #endif
  15. static char *sample_image = "example.jpg";
  16. static void put_sample_image();
  17. void image_resize()
  18. {
  19. aos_pool_t *p = NULL;
  20. aos_string_t bucket;
  21. aos_string_t object;
  22. int is_cname = 0;
  23. oss_request_options_t *options = NULL;
  24. aos_table_t *headers = NULL;
  25. aos_table_t *params = NULL;
  26. aos_table_t *resp_headers = NULL;
  27. aos_status_t *s = NULL;
  28. aos_string_t filename;
  29. aos_pool_create(&p, NULL);
  30. options = oss_request_options_create(p);
  31. init_sample_request_options(options, is_cname);
  32. aos_str_set(&bucket, BUCKET_NAME);
  33. aos_str_set(&object, sample_image);
  34. aos_str_set(&filename, "example-new.jpg");
  35. params = aos_table_make(p, 1);
  36. apr_table_set(params, OSS_PROCESS, "image/resize,m_fixed,w_100,h_100");
  37. /* get processed image to file */
  38. s = oss_get_object_to_file(options, &bucket, &object, headers,
  39. params, &filename, &resp_headers);
  40. if (aos_status_is_ok(s)) {
  41. printf("get object to file succeeded\n");
  42. } else {
  43. printf("get object to file failed\n");
  44. }
  45. aos_pool_destroy(p);
  46. }
  47. void image_crop()
  48. {
  49. aos_pool_t *p = NULL;
  50. aos_string_t bucket;
  51. aos_string_t object;
  52. int is_cname = 0;
  53. oss_request_options_t *options = NULL;
  54. aos_table_t *headers = NULL;
  55. aos_table_t *params = NULL;
  56. aos_table_t *resp_headers = NULL;
  57. aos_status_t *s = NULL;
  58. aos_string_t filename;
  59. aos_pool_create(&p, NULL);
  60. options = oss_request_options_create(p);
  61. init_sample_request_options(options, is_cname);
  62. aos_str_set(&bucket, BUCKET_NAME);
  63. aos_str_set(&object, sample_image);
  64. aos_str_set(&filename, "example-new.jpg");
  65. params = aos_table_make(p, 1);
  66. apr_table_set(params, OSS_PROCESS, "image/crop,w_100,h_100,x_100,y_100,r_1");
  67. /* get processed image to file */
  68. s = oss_get_object_to_file(options, &bucket, &object, headers,
  69. params, &filename, &resp_headers);
  70. if (aos_status_is_ok(s)) {
  71. printf("get object to file succeeded\n");
  72. } else {
  73. printf("get object to file failed\n");
  74. }
  75. aos_pool_destroy(p);
  76. }
  77. void image_rotate()
  78. {
  79. aos_pool_t *p = NULL;
  80. aos_string_t bucket;
  81. aos_string_t object;
  82. int is_cname = 0;
  83. oss_request_options_t *options = NULL;
  84. aos_table_t *headers = NULL;
  85. aos_table_t *params = NULL;
  86. aos_table_t *resp_headers = NULL;
  87. aos_status_t *s = NULL;
  88. aos_string_t filename;
  89. aos_pool_create(&p, NULL);
  90. options = oss_request_options_create(p);
  91. init_sample_request_options(options, is_cname);
  92. aos_str_set(&bucket, BUCKET_NAME);
  93. aos_str_set(&object, sample_image);
  94. aos_str_set(&filename, "example-new.jpg");
  95. params = aos_table_make(p, 1);
  96. apr_table_set(params, OSS_PROCESS, "image/rotate,90");
  97. /* get processed image to file */
  98. s = oss_get_object_to_file(options, &bucket, &object, headers,
  99. params, &filename, &resp_headers);
  100. if (aos_status_is_ok(s)) {
  101. printf("get object to file succeeded\n");
  102. } else {
  103. printf("get object to file failed\n");
  104. }
  105. aos_pool_destroy(p);
  106. }
  107. void image_sharpen()
  108. {
  109. aos_pool_t *p = NULL;
  110. aos_string_t bucket;
  111. aos_string_t object;
  112. int is_cname = 0;
  113. oss_request_options_t *options = NULL;
  114. aos_table_t *headers = NULL;
  115. aos_table_t *params = NULL;
  116. aos_table_t *resp_headers = NULL;
  117. aos_status_t *s = NULL;
  118. aos_string_t filename;
  119. aos_pool_create(&p, NULL);
  120. options = oss_request_options_create(p);
  121. init_sample_request_options(options, is_cname);
  122. aos_str_set(&bucket, BUCKET_NAME);
  123. aos_str_set(&object, sample_image);
  124. aos_str_set(&filename, "example-new.jpg");
  125. params = aos_table_make(p, 1);
  126. apr_table_set(params, OSS_PROCESS, "image/sharpen,100");
  127. /* get processed image to file */
  128. s = oss_get_object_to_file(options, &bucket, &object, headers,
  129. params, &filename, &resp_headers);
  130. if (aos_status_is_ok(s)) {
  131. printf("get object to file succeeded\n");
  132. } else {
  133. printf("get object to file failed\n");
  134. }
  135. aos_pool_destroy(p);
  136. }
  137. void image_watermark()
  138. {
  139. aos_pool_t *p = NULL;
  140. aos_string_t bucket;
  141. aos_string_t object;
  142. int is_cname = 0;
  143. oss_request_options_t *options = NULL;
  144. aos_table_t *headers = NULL;
  145. aos_table_t *params = NULL;
  146. aos_table_t *resp_headers = NULL;
  147. aos_status_t *s = NULL;
  148. aos_string_t filename;
  149. aos_pool_create(&p, NULL);
  150. options = oss_request_options_create(p);
  151. init_sample_request_options(options, is_cname);
  152. aos_str_set(&bucket, BUCKET_NAME);
  153. aos_str_set(&object, sample_image);
  154. aos_str_set(&filename, "example-new.jpg");
  155. params = aos_table_make(p, 1);
  156. apr_table_set(params, OSS_PROCESS, "image/watermark,text_SGVsbG8g5Zu-54mH5pyN5YqhIQ");
  157. /* get processed image to file */
  158. s = oss_get_object_to_file(options, &bucket, &object, headers,
  159. params, &filename, &resp_headers);
  160. if (aos_status_is_ok(s)) {
  161. printf("get object to file succeeded\n");
  162. } else {
  163. printf("get object to file failed\n");
  164. }
  165. aos_pool_destroy(p);
  166. }
  167. void image_format() {
  168. aos_pool_t *p = NULL;
  169. aos_string_t bucket;
  170. aos_string_t object;
  171. int is_cname = 0;
  172. oss_request_options_t *options = NULL;
  173. aos_table_t *headers = NULL;
  174. aos_table_t *params = NULL;
  175. aos_table_t *resp_headers = NULL;
  176. aos_status_t *s = NULL;
  177. aos_string_t filename;
  178. aos_pool_create(&p, NULL);
  179. options = oss_request_options_create(p);
  180. init_sample_request_options(options, is_cname);
  181. aos_str_set(&bucket, BUCKET_NAME);
  182. aos_str_set(&object, sample_image);
  183. aos_str_set(&filename, "example-new.jpg");
  184. params = aos_table_make(p, 1);
  185. apr_table_set(params, OSS_PROCESS, "image/format,png");
  186. /* get processed image to file */
  187. s = oss_get_object_to_file(options, &bucket, &object, headers,
  188. params, &filename, &resp_headers);
  189. if (aos_status_is_ok(s)) {
  190. printf("get object to file succeeded\n");
  191. } else {
  192. printf("get object to file failed\n");
  193. }
  194. aos_pool_destroy(p);
  195. }
  196. void iamge_info()
  197. {
  198. aos_pool_t *p = NULL;
  199. aos_string_t bucket;
  200. aos_string_t object;
  201. int is_cname = 0;
  202. oss_request_options_t *options = NULL;
  203. aos_table_t *headers = NULL;
  204. aos_table_t *params = NULL;
  205. aos_table_t *resp_headers = NULL;
  206. aos_status_t *s = NULL;
  207. aos_list_t buffer;
  208. aos_buf_t *content = NULL;
  209. char *buf = NULL;
  210. int64_t len = 0;
  211. int64_t size = 0;
  212. int64_t pos = 0;
  213. aos_pool_create(&p, NULL);
  214. options = oss_request_options_create(p);
  215. init_sample_request_options(options, is_cname);
  216. aos_str_set(&bucket, BUCKET_NAME);
  217. aos_str_set(&object, sample_image);
  218. aos_list_init(&buffer);
  219. params = aos_table_make(p, 1);
  220. apr_table_set(params, OSS_PROCESS, "image/info");
  221. /* test get object to buffer */
  222. s = oss_get_object_to_buffer(options, &bucket, &object, headers,
  223. params, &buffer, &resp_headers);
  224. if (aos_status_is_ok(s)) {
  225. printf("put object from file succeeded\n");
  226. } else {
  227. printf("put object from file failed\n");
  228. }
  229. /* get buffer len */
  230. len = aos_buf_list_len(&buffer);
  231. buf = (char *)aos_pcalloc(p, (apr_size_t)(len + 1));
  232. buf[len] = '\0';
  233. /* copy buffer content to memory */
  234. aos_list_for_each_entry(aos_buf_t, content, &buffer, node) {
  235. size = aos_buf_size(content);
  236. memcpy(buf + pos, content->pos, (size_t)size);
  237. pos += size;
  238. }
  239. aos_pool_destroy(p);
  240. }
  241. void put_example_image()
  242. {
  243. aos_pool_t *p = NULL;
  244. aos_string_t bucket;
  245. aos_string_t object;
  246. aos_string_t filename;
  247. aos_status_t *s = NULL;
  248. oss_request_options_t *options = NULL;
  249. int is_cname = 0;
  250. aos_table_t *headers = NULL;
  251. aos_table_t *resp_headers = NULL;
  252. aos_pool_create(&p, NULL);
  253. options = oss_request_options_create(p);
  254. init_sample_request_options(options, is_cname);
  255. aos_str_set(&bucket, BUCKET_NAME);
  256. aos_str_set(&object, sample_image);
  257. aos_str_set(&filename, image_file);
  258. s = oss_put_object_from_file(options, &bucket, &object, &filename,
  259. headers, &resp_headers);
  260. if (aos_status_is_ok(s)) {
  261. printf("put object from file succeeded\n");
  262. } else {
  263. printf("put object from file failed\n");
  264. }
  265. aos_pool_destroy(p);
  266. }
  267. /**
  268. * ÉÏ´«Ê¾ÀýͼƬ
  269. */
  270. void put_sample_image()
  271. {
  272. aos_pool_t *p = NULL;
  273. aos_string_t bucket;
  274. aos_string_t object;
  275. aos_string_t filename;
  276. aos_status_t *s = NULL;
  277. oss_request_options_t *options = NULL;
  278. int is_cname = 0;
  279. aos_table_t *headers = NULL;
  280. aos_table_t *resp_headers = NULL;
  281. aos_pool_create(&p, NULL);
  282. options = oss_request_options_create(p);
  283. init_sample_request_options(options, is_cname);
  284. aos_str_set(&bucket, BUCKET_NAME);
  285. aos_str_set(&object, sample_image);
  286. aos_str_set(&filename, image_file);
  287. s = oss_put_object_from_file(options, &bucket, &object, &filename,
  288. headers, &resp_headers);
  289. if (aos_status_is_ok(s)) {
  290. printf("put object from file succeeded\n");
  291. } else {
  292. printf("put object from file failed\n");
  293. }
  294. aos_pool_destroy(p);
  295. }
  296. void image_sample()
  297. {
  298. put_sample_image();
  299. image_resize();
  300. image_crop();
  301. image_rotate();
  302. image_sharpen();
  303. image_watermark();
  304. image_format();
  305. iamge_info();
  306. }