upload_file_sample.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (C) 2012-2019 UCloud. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include <limits.h>
  19. #include <stdbool.h>
  20. #include <string.h>
  21. #include <signal.h>
  22. #include "uiot_import.h"
  23. #include "ca.h"
  24. #include "utils_httpc.h"
  25. #include "uiot_export_http.h"
  26. //upload file name saved in ufile
  27. #define FILE_NAME "upload_file.txt"
  28. //data saved in file that you upload
  29. #define UPLOAD_BUFFER "This is a test file for upload test!"
  30. static int running_state = 0;
  31. static void http_upload_file_test_thread(void)
  32. {
  33. char md5[100];
  34. char *authorization = (char *)malloc(1024);
  35. memset(authorization, 0, 1024);
  36. char *put_url = (char *)malloc(1024);
  37. memset(put_url, 0, 1024);
  38. int ret = SUCCESS_RET;
  39. http_client_buffer_md5(UPLOAD_BUFFER, sizeof(UPLOAD_BUFFER), md5);
  40. HAL_Printf("MD5:%s\n", md5);
  41. ret = IOT_GET_URL_AND_AUTH(PKG_USING_UCLOUD_IOT_SDK_PRODUCT_SN, PKG_USING_UCLOUD_IOT_SDK_DEVICE_SN, PKG_USING_UCLOUD_IOT_SDK_DEVICE_SECRET, \
  42. FILE_NAME, UPLOAD_BUFFER, sizeof(UPLOAD_BUFFER), md5, authorization, put_url);
  43. if(SUCCESS_RET != ret)
  44. {
  45. HAL_Printf("get url and auth fail,ret:%d\r\n", ret);
  46. return;
  47. }
  48. HAL_Printf("get authorization:%s\n", authorization);
  49. HAL_Printf("get put_url:%s\n", put_url);
  50. //上传文件超时时间与文件长度相关
  51. ret = IOT_HTTP_UPLOAD_FILE(FILE_NAME, UPLOAD_BUFFER, sizeof(UPLOAD_BUFFER), md5, authorization, put_url, 10000);
  52. if(SUCCESS_RET != ret)
  53. {
  54. HAL_Printf("upload file fail,ret:%d\r\n", ret);
  55. return;
  56. }
  57. HAL_Printf("upload success\n");
  58. HAL_Free(authorization);
  59. HAL_Free(put_url);
  60. return;
  61. }
  62. static int http_upload_file_test_example(int argc, char **argv)
  63. {
  64. rt_thread_t tid;
  65. int stack_size = 8192;
  66. if (2 == argc)
  67. {
  68. if (!strcmp("start", argv[1]))
  69. {
  70. if (1 == running_state)
  71. {
  72. HAL_Printf("http_publish_test_example is already running\n");
  73. return 0;
  74. }
  75. }
  76. else if (!strcmp("stop", argv[1]))
  77. {
  78. if (0 == running_state)
  79. {
  80. HAL_Printf("http_publish_test_example is already stopped\n");
  81. return 0;
  82. }
  83. running_state = 0;
  84. return 0;
  85. }
  86. else
  87. {
  88. HAL_Printf("Usage: http_publish_test_example start/stop");
  89. return 0;
  90. }
  91. }
  92. else
  93. {
  94. HAL_Printf("Para err, usage: http_publish_test_example start/stop");
  95. return 0;
  96. }
  97. tid = rt_thread_create("http_upload_file_test", (void (*)(void *))http_upload_file_test_thread,
  98. NULL, stack_size, RT_THREAD_PRIORITY_MAX / 2 - 1, 100);
  99. if (tid != RT_NULL)
  100. {
  101. rt_thread_startup(tid);
  102. }
  103. return 0;
  104. }
  105. #ifdef RT_USING_FINSH
  106. #include <finsh.h>
  107. FINSH_FUNCTION_EXPORT(http_upload_file_test_example, startup http upload_file example);
  108. #endif
  109. #ifdef FINSH_USING_MSH
  110. MSH_CMD_EXPORT(http_upload_file_test_example, startup http upload_file example);
  111. #endif