edit-icon download-icon

サーバー操作

最終更新日: Sep 04, 2017

OSS MEDIA C SDK は、クライアント、サーバー、および HLS の 3 つの部分に分けられます。次に、主にサーバーに関連する操作を紹介します。その他の操作については、次の章を参照してください。

インターフェース

サーバーに関連する操作インターフェースはすべてoss_media.hに配置されています。現在、以下のインターフェースが提供されています。

  • 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

次に、さまざまなインターフェースの使用法と注意事項を詳しく説明します。

基本構造

  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: 例えば、 oss-cn-hangzhou.aliyuncs.com。
  • is_cname: CNAME が有効かどうか。
  • access_key_id: Alibaba Cloud によって提供されるアクセス制御用のアクセスキーID。
  • access_key_secret: Alibaba Cloud RAM で作成されたロールの ARN。この値は、RAM コンソール > 役割の管理 > 役割の名前 > 基本情報 > ARN をクリックすると利用できます。値の形式は、acs:ram::xxxxxx:role/yyyy に似ています。ロールを使用できない場合は、新しいロールを作成して、そのロールに AliyunOSSFullAccess および AliyunSTSAssumeRoleAccess 権限を付与する必要があります。
  • marker: 返された結果を、マーカーの後の最初のエントリからアルファベット順に開始するように設定します。
  • max_size: 返される最大数を設定します。値は 1,000 を超えることはできません。
  • next_marker: 次の実行の開始位置。

初期化

  1. /**
  2. * @brief Initialize the OSS media
  3. * @note This interface should 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 an error has occured. Possible causes of the failure include: insufficient memory, or the APR or CURL versions are too low.
  7. */
  8. int oss_media_init();

注意: サンプルコードはGitHub にあります。

削除

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

注意: サンプルコードは GitHub にあります。

バケットの作成

  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 the 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);

サンプルプロジェクト:

  1. static void init_media_config(oss_media_config_t *config) {
  2. config->endpoint = "your endpoint";
  3. config->access_key_id = "The access key ID provided by Alibaba Cloud";
  4. config->access_key_secret = "The access key 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. }

注意: サンプルコードは GitHub にあります。

バケットの削除

  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 occur. 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);

サンプルプロジェクト:

  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. }

注意: サンプルコードはGitHub にあります。

バケットのためのライフサイクルルールの作成

  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 occur. 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);

サンプルプロジェクト:

  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. }

注意: サンプルコードは GitHub にあります。

バケットのためのライフサイクルルールの取得

  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. 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);

サンプルプロジェクト:

  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. }

注意: サンプルコードは GitHub にあります。

バケットのためのライフサイクルルールの削除

  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 occur. 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);

サンプルプロジェクト:

  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. }

注意: サンプルコードは GitHub にあります。

オブジェクトの削除

  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. 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);

サンプルプロジェクト:

  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. }

注意: サンプルコードは GitHub にあります。

オブジェクトの表示

  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 occur. 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);

サンプルプロジェクト:

  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. }

注意: サンプルコードは GitHub にあります。

一時的なトークンの取得

  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. 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);

サンプルプロジェクト:

  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. }

注意: サンプルコードは GitHub にあります。

カスタムポリシーを使用してトークンの取得

  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 occur. 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);

サンプルプロジェクト:

  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. }

注意: サンプルコードは GitHub にあります。