edit-icon download-icon

Server operations

Last Updated: Nov 21, 2017

OSS MEDIA C SDK has three parts: the client, the server, and the HLS. Next we introduce the operations related to the server. For other operations, see the following chapters.

Interface

Operation interfaces related to the server are all placed in oss_media.h. Currently the following interfaces are provided:

  • oss_media_create_bucket
  • oss_media_delete_bucket
  • oss_media_create_bucket_lifecycle
  • oss_media_get_bucket_lifecycle
  • oss_media_delete_bucket_lifecycle
  • oss_media_delete_file
  • oss_media_list_files
  • oss_media_get_token
  • oss_media_get_token_from_policy

The Functions and notes of usage of various interfaces are as follows:

Basic structs

  1. typedef struct oss_media_config_s {
  2. char *endpoint;
  3. int is_cname;
  4. char *access_key_id;
  5. char *access_key_secret;
  6. char *role_arn;
  7. } oss_media_config_t;
  8. typedef struct oss_media_files_s {
  9. char *path;
  10. char *marker;
  11. int max_size;
  12. char *next_marker;
  13. int size;
  14. char **file_names;
  15. } oss_media_files_t;
  • endpoint: For example, oss-cn-hangzhou.aliyuncs.com.
  • is_cname: Whether CNAME has been enabled.
  • access_key_id: The AccessKey ID for access control provided by Alibaba Cloud.
  • access_key_secret: The AccessKey Secret for access control provided by Alibaba Cloud.
  • role_arn: The ARN of the role created in Alibaba Cloud RAM. The value is available in RAM console > Manage Roles > click the name of a role > Basic Information > ARN. The value format is similar to: acs:ram::xxxxxx:role/yyyy. If no role is available, you must create a new role and grant the AliyunOSSFullAccess and AliyunSTSAssumeRoleAccess permissions to the role.
  • marker: Set the returned results to begin from the first entry after the Marker in alphabetical order.
  • max_size: Set the maximum number returned. The value cannot exceed 1,000.
  • next_marker: The start position of the next execution.

Initialization

  1. /**
  2. * @brief Initialize the OSS media
  3. * @note This interface must be called first when the program starts to initialize the OSS MEDIA C SDK.
  4. * @return:
  5. * If 0 is returned, it indicates the operation is successful.
  6. * Otherwise, it indicates that an error has occured. The possible causes of the failure include: insufficient memory, or the APR or CURL versions are too low.
  7. */
  8. int oss_media_init();

Note: For complete code, see GitHub.

Destruction

  1. /**
  2. * @brief Destroy the OSS media
  3. * @note This interface must be called at last when the program ends to destroy the OSS MEDIA C SDK.
  4. */
  5. void oss_media_destroy();

Note: For the complete code, see GitHub.

Create a bucket

  1. /**
  2. * @brief Create a new bucket
  3. * @paramp[in] oss_media_acl_t
  4. * OSS_ACL_PRIVATE Private read-write
  5. * OSS_ACL_PUBLIC_READ Public read and private write
  6. * OSS_ACL_PUBLIC_READ_WRITE Public read-write
  7. * @return:
  8. * If 0 is returned, it indicates the operation is successful.
  9. * Otherwise, if "-1" is returned, it indicates an error may have occurred. Possible causes of the failure include: the object cannot connect to OSS, or the object has no permission.
  10. */
  11. int oss_media_create_bucket(oss_media_config_t *config, const char *bucket_name, oss_media_acl_t acl);

Example project:

  1. static void init_media_config(oss_media_config_t *config) {
  2. config->endpoint = "your endpoint";
  3. config->access_key_id = "The AccessKey ID provided by Alibaba Cloud";
  4. config->access_key_secret = "The AccessKey Secret provided by Alibaba Cloud";
  5. config->role_arn = "The role ARN provided by Alibaba Cloud RAM";
  6. config->is_cname = 0;
  7. }
  8. void create_bucket() {
  9. int ret;
  10. char *bucket_name;
  11. oss_media_config_t config;
  12. /* Initialize the variable */
  13. bucket_name = "<your bucket name>";
  14. init_media_config(&config);
  15. /* Create the bucket */
  16. ret = oss_media_create_bucket(&config, bucket_name, OSS_ACL_PRIVATE);
  17. if (0 == ret) {
  18. printf("create bucket[%s] succeeded.\n", bucket_name);
  19. } else {
  20. printf("create bucket[%s] failed.\n", bucket_name);
  21. }
  22. }

Note: For the complete code, see GitHub.

Delete a bucket

  1. /*
  2. * @brief Delete the bucket
  3. * @return:
  4. * If 0 is returned, it indicates the operation is successful.
  5. * Otherwise, if "-1" is returned, it indicates an error may have occurred. Possible causes of the failure include: the object cannot connect to the OSS, or the object has no permission.
  6. */
  7. int oss_media_delete_bucket(oss_media_config_t *config, const char *bucket_name);

Example project:

  1. void delete_bucket() {
  2. int ret;
  3. char *bucket_name;
  4. oss_media_config_t config;
  5. /* Initialize the variable */
  6. bucket_name = "<your bucket name>";
  7. init_media_config(&config);
  8. /* Delete the bucket */
  9. ret = oss_media_delete_bucket(&config, bucket_name);
  10. if (0 == ret) {
  11. printf("delete bucket[%s] succeeded.\n", bucket_name);
  12. } else {
  13. printf("delete bucket[%s] failed.\n", bucket_name);
  14. }
  15. }

Note: Example code can be found at GitHub.

Create lifecycle rules for a bucket

  1. /**
  2. * @brief Create lifecycle rules for the bucket
  3. * @note These rules can control the time for automatically deleting objects
  4. * @return:
  5. * If 0 is returned, it indicates the operation is successful.
  6. * Otherwise, if "-1" is returned, it indicates an error may have occurred. The possible causes of the failure include: the object cannot connect to the OSS, or the object has no permission.
  7. */
  8. int oss_media_create_bucket_lifecycle(oss_media_config_t *config, const char *bucket_name, oss_media_lifecycle_rules_t *rules);

Example project:

  1. void create_bucket_lifecycle() {
  2. int ret;
  3. char *bucket_name;
  4. oss_media_lifecycle_rules_t *rules;
  5. oss_media_config_t config;
  6. /* Initialize the variable */
  7. bucket_name = "<your bucket name>";
  8. init_media_config(&config);
  9. /* Create lifecycle rules */
  10. rules = oss_media_create_lifecycle_rules(2);
  11. oss_media_lifecycle_rule_t rule1;
  12. rule1.name = "example-1";
  13. rule1.path = "/example/1";
  14. rule1.status = "Enabled";
  15. rule1.days = 1;
  16. oss_media_lifecycle_rule_t rule2;
  17. rule2.name = "example-2";
  18. rule2.path = "/example/2";
  19. rule2.status = "Disabled";
  20. rule2.days = 2;
  21. rules->rules[0] = &rule1;
  22. rules->rules[1] = &rule2;
  23. /* Set lifecycle rules of the bucket */
  24. ret = oss_media_create_bucket_lifecycle(&config,
  25. bucket_name, rules);
  26. if (0 == ret) {
  27. printf("create bucket[%s] lifecycle succeeded.\n", bucket_name);
  28. } else {
  29. printf("create bucket[%s] lifecycle failed.\n", bucket_name);
  30. }
  31. /* Release the resources */
  32. oss_media_free_lifecycle_rules(rules);
  33. }

Note: Example code can be found at GitHub.

Get lifecycle rules for the bucket

  1. /**
  2. * @brief Get lifecycle rules for the bucket
  3. * @return:
  4. * If 0 is returned, it indicates the operation is successful.
  5. * Otherwise, if "-1" is returned, it indicates an error may have occurred. The possible causes of the failure include: the object cannot connect to the OSS.
  6. */
  7. int oss_media_get_bucket_lifecycle(oss_media_config_t *config, const char *bucket_name, oss_media_lifecycle_rules_t *rules);

Example project:

  1. void get_bucket_lifecycle() {
  2. int ret, i;
  3. char *bucket_name;
  4. oss_media_lifecycle_rules_t *rules;
  5. oss_media_config_t config;
  6. /* Initialize the variable */
  7. bucket_name = "<your bucket name>";
  8. init_media_config(&config);
  9. /* Get lifecycle rules */
  10. rules = oss_media_create_lifecycle_rules(0);
  11. ret = oss_media_get_bucket_lifecycle(&config, bucket_name, rules);
  12. if (0 == ret) {
  13. printf("get bucket[%s] lifecycle succeeded.\n", bucket_name);
  14. } else {
  15. printf("get bucket[%s] lifecycle failed.\n", bucket_name);
  16. }
  17. for (i = 0; i < rules->size; i++) {
  18. printf(">>>> rule: [name:%s, path:%s, status=%s, days=%d]\n",
  19. rules->rules[i]->name, rules->rules[i]->path,
  20. rules->rules[i]->status, rules->rules[i]->days);
  21. }
  22. /* Release the resources */
  23. oss_media_free_lifecycle_rules(rules);
  24. }

Note: For the complete code, see GitHub.

Delete lifecycle rules for a bucket

  1. /**
  2. * @brief Delete the bucket lifecycle rules
  3. * @return:
  4. * If 0 is returned, it indicates the operation is successful.
  5. * Otherwise, if "-1" is returned, it indicates an error may have occured. The possible causes of the failure include: the object cannot connect to the OSS, or the object has no permission.
  6. */
  7. int oss_media_delete_bucket_lifecycle(oss_media_config_t *config, const char *bucket_name);

Example project:

  1. void delete_bucket_lifecycle()
  2. {
  3. int ret;
  4. char *bucket_name;
  5. oss_media_config_t config;
  6. /* Initialize the variable */
  7. bucket_name = "<your bucket name>";
  8. init_media_config(&config);
  9. /* Delete lifecycle rules */
  10. ret = oss_media_delete_bucket_lifecycle(&config, bucket_name);
  11. if (0 == ret) {
  12. printf("delete bucket[%s] lifecycle succeeded.\n", bucket_name);
  13. } else {
  14. printf("delete bucket[%s] lifecycle failed.\n", bucket_name);
  15. }
  16. }

Note: For the complete code, see GitHub.

Delete an object

  1. /**
  2. * @brief Delete a specific object in the bucket
  3. * @return:
  4. * If 0 is returned, it indicates the operation is successful.
  5. * Otherwise, if "-1" is returned, it indicates an error may occur. The possible causes of the failure include: the object cannot connect to the OSS, or the object has no permission.
  6. */
  7. int oss_media_delete_file(oss_media_config_t *config, const char *bucket_name, const char *key);

Example project:

  1. void delete_file() {
  2. int ret;
  3. oss_media_config_t config;
  4. char *file;
  5. char *bucket_name;
  6. /* Initialize the variable */
  7. file = "oss_media_file";
  8. bucket_name = "<your bucket name>";
  9. init_media_config(&config);
  10. /* Delete an object */
  11. ret = oss_media_delete_file(&config, bucket_name, file);
  12. if (0 == ret) {
  13. printf("delete file[%s] succeeded.\n", file);
  14. } else {
  15. printf("delete file[%s] lifecycle failed.\n", file);
  16. }
  17. }

Note: For the complete code, see GitHub.

List objects

  1. /**
  2. * @brief List objects in a specific bucket
  3. * @return:
  4. * If 0 is returned, it indicates the operation is successful.
  5. * Otherwise, if "-1" is returned, it indicates an error may have occurred. The possible causes of the failure include: the object cannot connect to the OSS, or the object has no permission.
  6. */
  7. int oss_media_list_files(oss_media_config_t *config, const char *bucket_name, oss_media_files_t *files);

Example project:

  1. void list_files() {
  2. int ret, i;
  3. char *bucket_name;
  4. oss_media_files_t *files;
  5. oss_media_config_t config;
  6. /* Initialize the variable */
  7. bucket_name = "<your bucket name>>";
  8. init_media_config(&config);
  9. files = oss_media_create_files();
  10. files->max_size = 50;
  11. /* List objects */
  12. ret = oss_media_list_files(&config, bucket_name, files);
  13. if (0 == ret) {
  14. printf("list files succeeded.\n");
  15. } else {
  16. printf("list files lifecycle failed.\n");
  17. }
  18. for (i = 0; i < files->size; i++) {
  19. printf(">>>>file name: %s\n", files->file_names[i]);
  20. }
  21. /* Release the resources */
  22. oss_media_free_files(files);
  23. }

Note: For the complete code, see GitHub.

Get the temporary token

  1. /*
  2. * @brief Get the temporary token
  3. * @param[in]:
  4. mode:
  5. * 'r': Read mode
  6. * 'w': Overwrite mode
  7. * 'a': Append write mode
  8. * expiration: The valid period of the temporary token. The value ranges from 15 minutes to one hour. The unit is seconds.
  9. * @return:
  10. * If 0 is returned, it indicates the operation is successful.
  11. * Otherwise, if "-1" is returned, it indicates an error may have occurred. The possible causes of the failure include: the object cannot connect to the Alibaba Cloud STS, the RAM or STS is not activated, no role is created and the parameter is invalid.
  12. */
  13. int oss_media_get_token(oss_media_config_t *config,
  14. const char *bucket_name,
  15. const char *path,
  16. const char *mode,
  17. int64_t expiration,
  18. oss_media_token_t *token);

Example project:

  1. void get_token() {
  2. int ret;
  3. char *bucket_name;
  4. oss_media_token_t token;
  5. oss_media_config_t config;
  6. /* Initialize the variable */
  7. bucket_name = "<your bucket name>";
  8. init_media_config(&config);
  9. /* Get the temporary token */
  10. ret = oss_media_get_token(&config, bucket_name, "/*", "rwa",
  11. 3600, &token);
  12. if (0 == ret) {
  13. printf("get token succeeded, access_key_id=%s, access_key_secret=%s, token=%s\n",
  14. token.tmpAccessKeyId, token.tmpAccessKeySecret, token.securityToken);
  15. } else {
  16. printf("get token failed.\n");
  17. }
  18. }

Note: For the complete code, see GitHub.

Get the token through a custom policy

  1. /**
  2. * @brief Get the token through the specific custom policy
  3. * @param[in]:
  4. * expiration: The valid period of the temporary token. The value ranges from 15 minutes to one hour. The unit is second.
  5. * @return:
  6. * If 0 is returned, it indicates the operation is successful.
  7. * Otherwise, if "-1" is returned, it indicates an error may have occurred. The possible causes of the failure include: the object cannot connect to the Alibaba Cloud STS, the RAM or STS is not activated, no role is created and the parameter is invalid.
  8. */
  9. int oss_media_get_token_from_policy(oss_media_config_t *config,
  10. const char *policy,
  11. int64_t expiration,
  12. oss_media_token_t *token);

Example project:

  1. void get_token_from_policy() {
  2. int ret;
  3. oss_media_token_t token;
  4. char *policy;
  5. oss_media_config_t config;
  6. /* Initialize the variable */
  7. init_media_config(&config);
  8. /* Set the custom policy */
  9. policy = "{\"Version\":\"1\",\"Statement\":[{\"Effect\":\"Allow\", "
  10. "\"Action\":\"*\", \"Resource\":\"*\"}]}";
  11. /* Get the token */
  12. ret = oss_media_get_token_from_policy(&config, policy, 3600, &token);
  13. if (0 == ret) {
  14. printf("get token succeeded, access_key_id=%s, access_key_secret=%s, token=%s\n",
  15. token.tmpAccessKeyId, token.tmpAccessKeySecret, token.securityToken);
  16. } else {
  17. printf("get token failed.\n");
  18. }
  19. }

Note: For the complete code, see GitHub.

Thank you! We've received your feedback.