开发者社区> 问答> 正文

C-SDK之如何实现上传文件(一)?

在OSS中,用户操作的基本数据单元是文件(Object)。单个文件的最大允许大小根据上传数据方式不同而不同,Put Object方式文件最大不能超过5GB,使用分片上传方式文件大小不能超过48.8TB。
OSS C SDK提供了丰富的文件上传接口,用户可以通过以下方式向OSS中上传文件:

  • 简单上传
  • 追加上传
  • 断点续传上传
  • 分片上传


简单上传



从内存中上传数据到OSS


通过oss_put_object_from_buffer接口,可以实现从内存中上传数据到OSS:
  1. [backcolor=transparent]void[backcolor=transparent] put_object_from_buffer[backcolor=transparent]()
  2. [backcolor=transparent]{
  3. [backcolor=transparent]    [backcolor=transparent]aos_pool_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]p [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
  4. [backcolor=transparent]    [backcolor=transparent]aos_string_t[backcolor=transparent] bucket[backcolor=transparent];
  5. [backcolor=transparent]    [backcolor=transparent]aos_string_t[backcolor=transparent] object[backcolor=transparent];
  6. [backcolor=transparent]    [backcolor=transparent]aos_table_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]headers [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
  7. [backcolor=transparent]    [backcolor=transparent]aos_table_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]resp_headers [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
  8. [backcolor=transparent]    [backcolor=transparent]oss_request_options_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]options [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
  9. [backcolor=transparent]    [backcolor=transparent]aos_list_t[backcolor=transparent] buffer[backcolor=transparent];
  10. [backcolor=transparent]    [backcolor=transparent]aos_buf_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]content [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
  11. [backcolor=transparent]    [backcolor=transparent]char[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]str [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"test oss c sdk"[backcolor=transparent];
  12. [backcolor=transparent]    [backcolor=transparent]aos_status_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]s [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
  13. [backcolor=transparent]    aos_pool_create[backcolor=transparent](&[backcolor=transparent]p[backcolor=transparent],[backcolor=transparent] NULL[backcolor=transparent]);
  14. [backcolor=transparent]    [backcolor=transparent]/* 创建并初始化options */
  15. [backcolor=transparent]    options [backcolor=transparent]=[backcolor=transparent] oss_request_options_create[backcolor=transparent]([backcolor=transparent]p[backcolor=transparent]);
  16. [backcolor=transparent]    init_options[backcolor=transparent]([backcolor=transparent]options[backcolor=transparent]);
  17. [backcolor=transparent]    aos_str_set[backcolor=transparent](&[backcolor=transparent]bucket[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"<您的bucket名字>"[backcolor=transparent]);
  18. [backcolor=transparent]    aos_str_set[backcolor=transparent](&[backcolor=transparent]object[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"<您的object名字>"[backcolor=transparent]);
  19. [backcolor=transparent]    [backcolor=transparent]/* 初始化参数 */
  20. [backcolor=transparent]    aos_list_init[backcolor=transparent](&[backcolor=transparent]buffer[backcolor=transparent]);
  21. [backcolor=transparent]    content [backcolor=transparent]=[backcolor=transparent] aos_buf_pack[backcolor=transparent]([backcolor=transparent]options[backcolor=transparent]->[backcolor=transparent]pool[backcolor=transparent],[backcolor=transparent] str[backcolor=transparent],[backcolor=transparent] strlen[backcolor=transparent]([backcolor=transparent]str[backcolor=transparent]));
  22. [backcolor=transparent]    aos_list_add_tail[backcolor=transparent](&[backcolor=transparent]content[backcolor=transparent]->[backcolor=transparent]node[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]&[backcolor=transparent]buffer[backcolor=transparent]);
  23. [backcolor=transparent]    [backcolor=transparent]/* 上传文件 */
  24. [backcolor=transparent]    s [backcolor=transparent]=[backcolor=transparent] oss_put_object_from_buffer[backcolor=transparent]([backcolor=transparent]options[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]&[backcolor=transparent]bucket[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]&[backcolor=transparent]object[backcolor=transparent],[backcolor=transparent]
  25. [backcolor=transparent]                   [backcolor=transparent]&[backcolor=transparent]buffer[backcolor=transparent],[backcolor=transparent] headers[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]&[backcolor=transparent]resp_headers[backcolor=transparent]);
  26. [backcolor=transparent]    [backcolor=transparent]/* 判断是否上传成功 */
  27. [backcolor=transparent]    [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]aos_status_is_ok[backcolor=transparent]([backcolor=transparent]s[backcolor=transparent]))[backcolor=transparent] [backcolor=transparent]{
  28. [backcolor=transparent]        printf[backcolor=transparent]([backcolor=transparent]"put object from buffer succeeded\n"[backcolor=transparent]);
  29. [backcolor=transparent]    [backcolor=transparent]}[backcolor=transparent] [backcolor=transparent]else[backcolor=transparent] [backcolor=transparent]{
  30. [backcolor=transparent]        printf[backcolor=transparent]([backcolor=transparent]"put object from buffer failed\n"[backcolor=transparent]);[backcolor=transparent]      
  31. [backcolor=transparent]    [backcolor=transparent]}[backcolor=transparent]    
  32. [backcolor=transparent]    [backcolor=transparent]/* 释放资源*/
  33. [backcolor=transparent]    aos_pool_destroy[backcolor=transparent]([backcolor=transparent]p[backcolor=transparent]);
  34. [backcolor=transparent]}

[backcolor=transparent]注:


上传本地文件到OSS


通过oss_put_object_from_file接口,并指定fliepath参数,可以实现上传一个本地文件到OSS:
  1. [backcolor=transparent]void[backcolor=transparent] put_object_from_file[backcolor=transparent]()
  2. [backcolor=transparent]{
  3. [backcolor=transparent]    [backcolor=transparent]aos_pool_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]p [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
  4. [backcolor=transparent]    [backcolor=transparent]aos_string_t[backcolor=transparent] bucket[backcolor=transparent];
  5. [backcolor=transparent]    [backcolor=transparent]aos_string_t[backcolor=transparent] object[backcolor=transparent];
  6. [backcolor=transparent]    [backcolor=transparent]aos_table_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]headers [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
  7. [backcolor=transparent]    [backcolor=transparent]aos_table_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]resp_headers [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
  8. [backcolor=transparent]    [backcolor=transparent]oss_request_options_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]options [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
  9. [backcolor=transparent]    [backcolor=transparent]char[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]filename [backcolor=transparent]=[backcolor=transparent] __FILE__[backcolor=transparent];
  10. [backcolor=transparent]    [backcolor=transparent]aos_status_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]s [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
  11. [backcolor=transparent]    [backcolor=transparent]aos_string_t[backcolor=transparent] file[backcolor=transparent];
  12. [backcolor=transparent]    aos_pool_create[backcolor=transparent](&[backcolor=transparent]p[backcolor=transparent],[backcolor=transparent] NULL[backcolor=transparent]);
  13. [backcolor=transparent]    [backcolor=transparent]/* 创建并初始化options */
  14. [backcolor=transparent]    options [backcolor=transparent]=[backcolor=transparent] oss_request_options_create[backcolor=transparent]([backcolor=transparent]p[backcolor=transparent]);
  15. [backcolor=transparent]    init_options[backcolor=transparent]([backcolor=transparent]options[backcolor=transparent]);
  16. [backcolor=transparent]    [backcolor=transparent]/* 初始化参数 */
  17. [backcolor=transparent]    headers [backcolor=transparent]=[backcolor=transparent] aos_table_make[backcolor=transparent]([backcolor=transparent]options[backcolor=transparent]->[backcolor=transparent]pool[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]1[backcolor=transparent]);
  18. [backcolor=transparent]    apr_table_set[backcolor=transparent]([backcolor=transparent]headers[backcolor=transparent],[backcolor=transparent] OSS_CONTENT_TYPE[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"image/jpeg"[backcolor=transparent]);
  19. [backcolor=transparent]    aos_str_set[backcolor=transparent](&[backcolor=transparent]bucket[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"<您的bucket名字>"[backcolor=transparent]);
  20. [backcolor=transparent]    aos_str_set[backcolor=transparent](&[backcolor=transparent]object[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"<您的object名字>"[backcolor=transparent]);
  21. [backcolor=transparent]    aos_str_set[backcolor=transparent](&[backcolor=transparent]file[backcolor=transparent],[backcolor=transparent] filename[backcolor=transparent]);
  22. [backcolor=transparent]    [backcolor=transparent]/* 上传文件 */
  23. [backcolor=transparent]    s [backcolor=transparent]=[backcolor=transparent] oss_put_object_from_file[backcolor=transparent]([backcolor=transparent]options[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]&[backcolor=transparent]bucket[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]&[backcolor=transparent]object[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]&[backcolor=transparent]file[backcolor=transparent],[backcolor=transparent]
  24. [backcolor=transparent]                                 headers[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]&[backcolor=transparent]resp_headers[backcolor=transparent]);
  25. [backcolor=transparent]    [backcolor=transparent]/* 判断是否上传成功 */
  26. [backcolor=transparent]    [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]aos_status_is_ok[backcolor=transparent]([backcolor=transparent]s[backcolor=transparent]))[backcolor=transparent] [backcolor=transparent]{
  27. [backcolor=transparent]        printf[backcolor=transparent]([backcolor=transparent]"put object from file succeeded\n"[backcolor=transparent]);
  28. [backcolor=transparent]    [backcolor=transparent]}[backcolor=transparent] [backcolor=transparent]else[backcolor=transparent] [backcolor=transparent]{
  29. [backcolor=transparent]        printf[backcolor=transparent]([backcolor=transparent]"put object from file failed\n"[backcolor=transparent]);
  30. [backcolor=transparent]    [backcolor=transparent]}
  31. [backcolor=transparent]    [backcolor=transparent]/* 释放资源*/
  32. [backcolor=transparent]    aos_pool_destroy[backcolor=transparent]([backcolor=transparent]p[backcolor=transparent]);
  33. [backcolor=transparent]}

[backcolor=transparent]注:
  • 使用该方式上传最大文件不能超过5G。如果超过可以使用分片上传。
  • 完整代码参考:GitHub


追加上传


OSS支持可追加的文件类型,调用时需要指定文件追加的位置,对于新创建文件,这个位置是0;对于已经存在的文件,这个位置必须是追加前文件的长度。
  • 文件不存在时,调用Append Object会创建一个可追加的文件
  • 文件存在时,调用Append Object会向文件末尾追加内容

Append Object以追加写的方式上传文件,通过Append Object操作创建的文件类型为Appendable Object,而通过Put Object上传的文件类型是Normal Object。

从内存中追加数据到OSS


通过oss_append_object_from_buffer接口,可以实现从内存中上传数据到OSS:
  1. [backcolor=transparent]void[backcolor=transparent] append_object_from_buffer[backcolor=transparent]()
  2. [backcolor=transparent]{
  3. [backcolor=transparent]    [backcolor=transparent]aos_pool_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]p [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
  4. [backcolor=transparent]    [backcolor=transparent]aos_string_t[backcolor=transparent] bucket[backcolor=transparent];
  5. [backcolor=transparent]    [backcolor=transparent]aos_string_t[backcolor=transparent] object[backcolor=transparent];
  6. [backcolor=transparent]    [backcolor=transparent]char[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]str [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"test oss c sdk"[backcolor=transparent];
  7. [backcolor=transparent]    [backcolor=transparent]aos_status_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]s [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
  8. [backcolor=transparent]    [backcolor=transparent]int64_t[backcolor=transparent] position [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]0[backcolor=transparent];
  9. [backcolor=transparent]    [backcolor=transparent]aos_table_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]headers1 [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
  10. [backcolor=transparent]    [backcolor=transparent]aos_table_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]headers2 [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
  11. [backcolor=transparent]    [backcolor=transparent]aos_table_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]resp_headers [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
  12. [backcolor=transparent]    [backcolor=transparent]oss_request_options_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]options [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
  13. [backcolor=transparent]    [backcolor=transparent]aos_list_t[backcolor=transparent] buffer[backcolor=transparent];
  14. [backcolor=transparent]    [backcolor=transparent]aos_buf_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]content [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
  15. [backcolor=transparent]    [backcolor=transparent]char[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]next_append_position [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
  16. [backcolor=transparent]    aos_pool_create[backcolor=transparent](&[backcolor=transparent]p[backcolor=transparent],[backcolor=transparent] NULL[backcolor=transparent]);
  17. [backcolor=transparent]    [backcolor=transparent]/* 创建并初始化options */
  18. [backcolor=transparent]    options [backcolor=transparent]=[backcolor=transparent] oss_request_options_create[backcolor=transparent]([backcolor=transparent]p[backcolor=transparent]);
  19. [backcolor=transparent]    init_options[backcolor=transparent]([backcolor=transparent]options[backcolor=transparent]);
  20. [backcolor=transparent]    [backcolor=transparent]/* 初始化参数 */
  21. [backcolor=transparent]    headers1 [backcolor=transparent]=[backcolor=transparent] aos_table_make[backcolor=transparent]([backcolor=transparent]p[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]0[backcolor=transparent]);
  22. [backcolor=transparent]    aos_str_set[backcolor=transparent](&[backcolor=transparent]bucket[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"<您的bucket名字>"[backcolor=transparent]);
  23. [backcolor=transparent]    aos_str_set[backcolor=transparent](&[backcolor=transparent]object[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"<您的object名字>"[backcolor=transparent]);
  24. [backcolor=transparent]    [backcolor=transparent]/* 获取起始追加位置 */
  25. [backcolor=transparent]    s [backcolor=transparent]=[backcolor=transparent] oss_head_object[backcolor=transparent]([backcolor=transparent]options[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]&[backcolor=transparent]bucket[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]&[backcolor=transparent]object[backcolor=transparent],[backcolor=transparent] headers1[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]&[backcolor=transparent]resp_headers[backcolor=transparent]);
  26. [backcolor=transparent]    [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]aos_status_is_ok[backcolor=transparent]([backcolor=transparent]s[backcolor=transparent]))[backcolor=transparent] [backcolor=transparent]{
  27. [backcolor=transparent]        next_append_position [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]char[backcolor=transparent]*)([backcolor=transparent]apr_table_get[backcolor=transparent]([backcolor=transparent]resp_headers[backcolor=transparent],[backcolor=transparent]
  28. [backcolor=transparent]                        [backcolor=transparent]"x-oss-next-append-position"[backcolor=transparent]));
  29. [backcolor=transparent]        position [backcolor=transparent]=[backcolor=transparent] atoi[backcolor=transparent]([backcolor=transparent]next_append_position[backcolor=transparent]);
  30. [backcolor=transparent]    [backcolor=transparent]}
  31. [backcolor=transparent]    [backcolor=transparent]/* 追加文件 */
  32. [backcolor=transparent]    headers2 [backcolor=transparent]=[backcolor=transparent] aos_table_make[backcolor=transparent]([backcolor=transparent]p[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]0[backcolor=transparent]);
  33. [backcolor=transparent]    aos_list_init[backcolor=transparent](&[backcolor=transparent]buffer[backcolor=transparent]);
  34. [backcolor=transparent]    content [backcolor=transparent]=[backcolor=transparent] aos_buf_pack[backcolor=transparent]([backcolor=transparent]p[backcolor=transparent],[backcolor=transparent] str[backcolor=transparent],[backcolor=transparent] strlen[backcolor=transparent]([backcolor=transparent]str[backcolor=transparent]));
  35. [backcolor=transparent]    aos_list_add_tail[backcolor=transparent](&[backcolor=transparent]content[backcolor=transparent]->[backcolor=transparent]node[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]&[backcolor=transparent]buffer[backcolor=transparent]);
  36. [backcolor=transparent]    s [backcolor=transparent]=[backcolor=transparent] oss_append_object_from_buffer[backcolor=transparent]([backcolor=transparent]options[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]&[backcolor=transparent]bucket[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]&[backcolor=transparent]object[backcolor=transparent],[backcolor=transparent]
  37. [backcolor=transparent]            position[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]&[backcolor=transparent]buffer[backcolor=transparent],[backcolor=transparent] headers2[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]&[backcolor=transparent]resp_headers[backcolor=transparent]);
  38. [backcolor=transparent]    [backcolor=transparent]/* 判断是否追加成功 */
  39. [backcolor=transparent]    [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]aos_status_is_ok[backcolor=transparent]([backcolor=transparent]s[backcolor=transparent]))
  40. [backcolor=transparent]    [backcolor=transparent]{
  41. [backcolor=transparent]        printf[backcolor=transparent]([backcolor=transparent]"append object from buffer succeeded\n"[backcolor=transparent]);
  42. [backcolor=transparent]    [backcolor=transparent]}[backcolor=transparent] [backcolor=transparent]else[backcolor=transparent] [backcolor=transparent]{
  43. [backcolor=transparent]        printf[backcolor=transparent]([backcolor=transparent]"append object from buffer failed\n"[backcolor=transparent]);
  44. [backcolor=transparent]    [backcolor=transparent]}
  45. [backcolor=transparent]    [backcolor=transparent]/* 释放资源*/
  46. [backcolor=transparent]    aos_pool_destroy[backcolor=transparent]([backcolor=transparent]p[backcolor=transparent]);
  47. [backcolor=transparent]}

[backcolor=transparent]注:





展开
收起
青衫无名 2017-10-19 10:19:16 1914 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
从 SDK 到编解码:视频直播架构解析 立即下载
跨平台的云服务SDK需要什么 立即下载
一个跨平台的云服务SDK需要什么 立即下载