开发者社区> 问答> 正文

对象存储 OSSURL签名授权

已解决

展开
收起
游客rbe7bxy4miorq 2018-03-12 02:25:40 1142 0
1 条回答
写回答
取消 提交回答
  • 采纳回答

    详细解答可以参考官方帮助文档

    可以通过生成签名URL的形式提供给用户一个临时的访问URL。在生成URL时,可以指定URL过期的时间,从而限制用户长时间访问。

    生成签名url

    通过oss_gen_signed_url接口生成请求url签名。

    生成下载请求的url签名

    1. aos_pool_t *p;
    2. oss_request_options_t *options;
    3. aos_http_request_t *req;
    4. char *url_str;
    5. char *bucket_name = "<您的bucket名字>";
    6. char *object_name = "<您的object名字>";
    7. aos_string_t bucket;
    8. aos_string_t object;
    9. apr_time_t now;
    10. int64_t expire_time;
    11. int one_hour = 3600; /* 单位:秒*/
    12. aos_pool_create(&p, NULL);
    13. /* 创建并初始化options */
    14. options = oss_request_options_create(p);
    15. init_options(options);
    16. /* 初始化参数 */
    17. aos_str_set(&bucket, bucket_name);
    18. aos_str_set(&object, object_name);
    19. req = aos_http_request_create(p);
    20. req->method = HTTP_GET;
    21. now = apr_time_now(); //millisecond
    22. expire_time = now / 1000000 + one_hour;
    23. /* 生成签名url */
    24. url_str = oss_gen_signed_url(options, &bucket, &object, expire_time, req);
    25. printf("临时下载url:%s\n", url_str);
    26. aos_pool_destroy(p);

    生成上传文件请求的url签名:

    1. aos_pool_t *p;
    2. oss_request_options_t *options;
    3. aos_http_request_t *req;
    4. char *bucket_name = "<您的bucket名字>";
    5. char *object_name = "<您的object名字>";
    6. aos_string_t bucket;
    7. aos_string_t object;
    8. apr_time_t now;
    9. int64_t expire_time;
    10. int one_hour = 3600;
    11. char *url_str = NULL;
    12. aos_pool_create(&p, NULL);
    13. /* 创建并初始化options */
    14. options = oss_request_options_create(p);
    15. init_options(options);
    16. /* 初始化参数 */
    17. aos_str_set(&bucket, bucket_name);
    18. aos_str_set(&object, object_name);
    19. req = aos_http_request_create(p);
    20. req->method = HTTP_PUT;
    21. now = apr_time_now(); //millisecond
    22. expire_time = now / 1000000 + one_hour;
    23. /* 生成签名url */
    24. url_str = oss_gen_signed_url((options, &bucket, &object, expire_time, req);
    25. printf("临时上传url:%s\n", url_str);
    26. aos_pool_destroy(p);

    使用签名URL下载文件

    1. aos_pool_t *p;
    2. oss_request_options_t *options;
    3. aos_http_request_t *req;
    4. aos_table_t *headers;
    5. aos_table_t *resp_headers;
    6. char *bucket_name = "<您的bucket名字>";
    7. char *object_name = "<您的object名字>";
    8. char *filepath = "<本地文件路径>";
    9. aos_string_t bucket;
    10. aos_string_t object;
    11. aos_string_t file;
    12. char *url_str;
    13. apr_time_t now;
    14. int64_t expire_time;
    15. int one_hour = 3600;
    16. aos_pool_create(&p, NULL);
    17. /* 创建并初始化options */
    18. options = oss_request_options_create(p);
    19. init_options(options);
    20. /* 初始化参数 */
    21. aos_str_set(&bucket, bucket_name);
    22. aos_str_set(&object, object_name);
    23. aos_str_set(&file, filepath);
    24. headers = aos_table_make(p, 0);
    25. req = aos_http_request_create(p);
    26. req->method = HTTP_GET;
    27. now = apr_time_now(); /* 单位:微秒 */
    28. expire_time = now / 1000000 + one_hour;
    29. /* 生成签名url */
    30. url_str = oss_gen_signed_url(options, &bucket, &object, expire_time, req);
    31. /* 使用签名url下载文件 */
    32. s = oss_get_object_to_file_by_url(options, url_str, headers, &file, &resp_headers);
    33. if (aos_status_is_ok(s)) {
    34. printf("get object succeeded\n");
    35. } else {
    36. printf("get object failed\n");
    37. }
    38. aos_pool_destroy(p);

    使用URL签名的方式上传文件

    1. aos_pool_t *p;
    2. int is_oss_domain = 1;//是否使用三级域名
    3. oss_request_options_t *options;
    4. aos_http_request_t *req;
    5. aos_table_t *headers;
    6. aos_table_t *resp_headers;
    7. char *bucket_name = "<您的bucket名字>";
    8. char *object_name = "<您的object名字>";
    9. char *filepath = "<本地文件路径>";
    10. aos_string_t bucket;
    11. aos_string_t object;
    12. aos_string_t file;
    13. char *url_str;
    14. apr_time_t now;
    15. int64_t expire_time;
    16. int one_hour = 3600;
    17. aos_pool_create(&p, NULL);
    18. /* 创建并初始化options */
    19. options = oss_request_options_create(p);
    20. init_options(options);
    21. /* 初始化参数 */
    22. aos_str_set(&bucket, bucket_name);
    23. aos_str_set(&object, object_name);
    24. aos_str_set(&file, filepath);
    25. headers = aos_table_make(p, 0);
    26. req = aos_http_request_create(p);
    27. req->method = HTTP_PUT;
    28. now = apr_time_now(); /* 单位:微秒*/
    29. expire_time = now / 1000000 + one_hour;
    30. /* 生成签名url */
    31. url_str = oss_gen_signed_url(options, &bucket, &object, expire_time, req);
    32. /* 使用签名url上传文件 */
    33. s = oss_put_object_from_file_by_url(options, url_str, &file, headers, &resp_headers);
    34. if (aos_status_is_ok(s)) {
    35. printf("put objects by signed url succeeded\n");
    36. } else {
    37. printf("put objects by signed url failed\n");
    38. }
    39. aos_pool_destroy(p);
    2018-03-14 14:22:23
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
OSS运维进阶实战手册 立即下载
《OSS运维基础实战手册》 立即下载
OSS运维基础实战手册 立即下载