edit-icon download-icon

Scenario

Last Updated: Nov 21, 2017

The previous two sections described relative operations on the client and on the server. This section explains how to connect the client and server for use. We recommend, that you to read previous two sections before you proceed with this section.

Video monitoring

In the preface, we have introduced the devices like network cameras and its functions and usage are described as follows:.

Procedure

Procedure

This function involves the three roles such as the network camera, the app server, and Alibaba Cloud Object Storage Service (OSS). The network camera uses the client part of the OSS MEDIA C SDK and the app server uses the server part of the OSS C MEDIA SDK.

Following is the procedure to upload a video clip captured by a network camera:

  1. To upload a video clip on the OSS, the network camera sends a network request to the app server to get authorization.

  2. After the app server receives the request, it performs a check to confirm that the upload permission can be granted to the network camera, and then requests a token with a specific validity period and with only the upload permission from Alibaba Cloud through the get_token interface of the OSS MEDIA C SDK.

  3. After Alibaba Cloud receives the request from the app server to get a token, it checks the user configuration. If the check verifies that the request can be allowed, it issues a temporary token (including the temporary AccessKey ID, the temporary AccessKey Secret, and a temporary STS token) with only the upload permission to the OSS and only validity for a specified duration and sends the token to the app server.

  4. The app server forwards the token to the network camera once the temporary token is received.

  5. After the network camera gets the token, you can upload video files to the OSS through the oss_media_write interface of the OSS MEDIA C SDK client.

  6. You can also use the server part of the C SDK or use Java, C#, Go, Python, PHP, Ruby SDKs on the app server to implement an HTTP service. This helps the other users to view and manage various video files on the webpage.

Example code

A simple example project simulating operations on the client and on the server is as follows:

  1. char* global_temp_access_key_id = NULL;
  2. char* global_temp_access_key_secret = NULL;
  3. char* global_temp_token = NULL;
  4. /* Authorization function */
  5. static void auth_func(oss_media_file_t *file) {
  6. file->endpoint = "your endpoint";
  7. file->is_cname = 0;
  8. file->access_key_id = global_temp_access_key_id;
  9. file->access_key_secret = global_temp_access_key_secret;
  10. file->token = global_temp_token;
  11. /* Effective time of this authorization */
  12. file->expiration = time(NULL) + 300;
  13. }
  14. /* Simulate sending a token to the client from the server */
  15. static void send_token_to_client(oss_media_token_t token) {
  16. global_temp_access_key_id = token.tmpAccessKeyId;
  17. global_temp_access_key_secret = token.tmpAccessKeySecret;
  18. global_temp_token = token.securityToken;
  19. }
  20. void get_and_use_token() {
  21. oss_media_token_t token;
  22. /* Server logic: Get a temporary token from Alibaba Cloud and send it to the client */
  23. {
  24. int ret;
  25. char *policy = NULL;
  26. oss_media_config_t config;
  27. policy = "{\n"
  28. "\"Statement\": [\n"
  29. "{"
  30. "\"Action\": \"oss:*\",\n"
  31. "\"Effect\": \"Allow\",\n"
  32. "\"Resource\": \"*\"\n"
  33. "}\n"
  34. "],\n"
  35. "\"Version\": \"1\"\n"
  36. "}\n";
  37. init_media_config(&config);
  38. /* Request a temporary authorization token from Alibaba Cloud */
  39. ret = oss_media_get_token_from_policy(&config, policy,
  40. 17 * 60, &token);
  41. if (ret != 0) {
  42. printf ("Get token failed.");
  43. return;
  44. }
  45. /* Simulate sending the temporary token to the client */
  46. send_token_to_client(token);
  47. }
  48. /* Client logic: Use the temporary token to operate on files after the temporary token is obtained from the server */
  49. {
  50. int ret;
  51. int64_t write_size = 0;
  52. oss_media_file_t *file = NULL;
  53. char *content = NULL;
  54. char *bucket_name;
  55. char *object_key;
  56. oss_media_file_stat_t stat;
  57. content = "hello oss media file\n";
  58. bucket_name = "<your bucket name>";
  59. object_key = "key";
  60. /* Open the object */
  61. file = oss_media_file_open(bucket_name, object_key, "w", auth_func);
  62. if (file != NULL) {
  63. printf ("open file failed.");
  64. return;
  65. }
  66. /* Write an object */
  67. write_size = oss_media_file_write(file, content, strlen(content));
  68. if (write_size != strlen(content)) {
  69. printf ("write file failed.");
  70. return;
  71. }
  72. /* Close the object and release the resource */
  73. oss_media_file_close(file);
  74. }
  75. }

Note:

  • Log on to the RAM console. Click Roles, from the left-side navigation pane. Locate the role and click Manage. Go to the Basic Information page, the policy is available.

  • If precise control of permissions is not required, you can use the simpler oss_media_get_token interface. The path parameter can be /*, and the mode parameter can be rwa.

  • For more information, see the RAM and STS Guide.

Thank you! We've received your feedback.