在OSS中,用户操作的基本数据单元是文件(Object)。单个文件的最大允许大小根据上传数据方式不同而不同,Put Object方式文件最大不能超过5GB,使用分片上传方式文件大小不能超过48.8TB。
OSS C SDK提供了丰富的文件上传接口,用户可以通过以下方式向OSS中上传文件:
简单上传
从内存中上传数据到OSS
通过oss_put_object_from_buffer接口,可以实现从内存中上传数据到OSS:
- [backcolor=transparent]void[backcolor=transparent] put_object_from_buffer[backcolor=transparent]()
- [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]aos_pool_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]p [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]aos_string_t[backcolor=transparent] bucket[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]aos_string_t[backcolor=transparent] object[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]aos_table_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]headers [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]aos_table_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]resp_headers [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]oss_request_options_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]options [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]aos_list_t[backcolor=transparent] buffer[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]aos_buf_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]content [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]char[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]str [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"test oss c sdk"[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]aos_status_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]s [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
- [backcolor=transparent] aos_pool_create[backcolor=transparent](&[backcolor=transparent]p[backcolor=transparent],[backcolor=transparent] NULL[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]/* 创建并初始化options */
- [backcolor=transparent] options [backcolor=transparent]=[backcolor=transparent] oss_request_options_create[backcolor=transparent]([backcolor=transparent]p[backcolor=transparent]);
- [backcolor=transparent] init_options[backcolor=transparent]([backcolor=transparent]options[backcolor=transparent]);
- [backcolor=transparent] aos_str_set[backcolor=transparent](&[backcolor=transparent]bucket[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"<您的bucket名字>"[backcolor=transparent]);
- [backcolor=transparent] aos_str_set[backcolor=transparent](&[backcolor=transparent]object[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"<您的object名字>"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]/* 初始化参数 */
- [backcolor=transparent] aos_list_init[backcolor=transparent](&[backcolor=transparent]buffer[backcolor=transparent]);
- [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]));
- [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]);
- [backcolor=transparent] [backcolor=transparent]/* 上传文件 */
- [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]
- [backcolor=transparent] [backcolor=transparent]&[backcolor=transparent]buffer[backcolor=transparent],[backcolor=transparent] headers[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]&[backcolor=transparent]resp_headers[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]/* 判断是否上传成功 */
- [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]{
- [backcolor=transparent] printf[backcolor=transparent]([backcolor=transparent]"put object from buffer succeeded\n"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]}[backcolor=transparent] [backcolor=transparent]else[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] printf[backcolor=transparent]([backcolor=transparent]"put object from buffer failed\n"[backcolor=transparent]);[backcolor=transparent]
- [backcolor=transparent] [backcolor=transparent]}[backcolor=transparent]
- [backcolor=transparent] [backcolor=transparent]/* 释放资源*/
- [backcolor=transparent] aos_pool_destroy[backcolor=transparent]([backcolor=transparent]p[backcolor=transparent]);
- [backcolor=transparent]}
[backcolor=transparent]注:
上传本地文件到OSS
通过oss_put_object_from_file接口,并指定fliepath参数,可以实现上传一个本地文件到OSS:
- [backcolor=transparent]void[backcolor=transparent] put_object_from_file[backcolor=transparent]()
- [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]aos_pool_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]p [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]aos_string_t[backcolor=transparent] bucket[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]aos_string_t[backcolor=transparent] object[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]aos_table_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]headers [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]aos_table_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]resp_headers [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]oss_request_options_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]options [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]char[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]filename [backcolor=transparent]=[backcolor=transparent] __FILE__[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]aos_status_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]s [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]aos_string_t[backcolor=transparent] file[backcolor=transparent];
- [backcolor=transparent] aos_pool_create[backcolor=transparent](&[backcolor=transparent]p[backcolor=transparent],[backcolor=transparent] NULL[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]/* 创建并初始化options */
- [backcolor=transparent] options [backcolor=transparent]=[backcolor=transparent] oss_request_options_create[backcolor=transparent]([backcolor=transparent]p[backcolor=transparent]);
- [backcolor=transparent] init_options[backcolor=transparent]([backcolor=transparent]options[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]/* 初始化参数 */
- [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]);
- [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]);
- [backcolor=transparent] aos_str_set[backcolor=transparent](&[backcolor=transparent]bucket[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"<您的bucket名字>"[backcolor=transparent]);
- [backcolor=transparent] aos_str_set[backcolor=transparent](&[backcolor=transparent]object[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"<您的object名字>"[backcolor=transparent]);
- [backcolor=transparent] aos_str_set[backcolor=transparent](&[backcolor=transparent]file[backcolor=transparent],[backcolor=transparent] filename[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]/* 上传文件 */
- [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]
- [backcolor=transparent] headers[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]&[backcolor=transparent]resp_headers[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]/* 判断是否上传成功 */
- [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]{
- [backcolor=transparent] printf[backcolor=transparent]([backcolor=transparent]"put object from file succeeded\n"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]}[backcolor=transparent] [backcolor=transparent]else[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] printf[backcolor=transparent]([backcolor=transparent]"put object from file failed\n"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]/* 释放资源*/
- [backcolor=transparent] aos_pool_destroy[backcolor=transparent]([backcolor=transparent]p[backcolor=transparent]);
- [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:
- [backcolor=transparent]void[backcolor=transparent] append_object_from_buffer[backcolor=transparent]()
- [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]aos_pool_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]p [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]aos_string_t[backcolor=transparent] bucket[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]aos_string_t[backcolor=transparent] object[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]char[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]str [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"test oss c sdk"[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]aos_status_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]s [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]int64_t[backcolor=transparent] position [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]0[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]aos_table_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]headers1 [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]aos_table_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]headers2 [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]aos_table_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]resp_headers [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]oss_request_options_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]options [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]aos_list_t[backcolor=transparent] buffer[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]aos_buf_t[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]content [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]char[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]next_append_position [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
- [backcolor=transparent] aos_pool_create[backcolor=transparent](&[backcolor=transparent]p[backcolor=transparent],[backcolor=transparent] NULL[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]/* 创建并初始化options */
- [backcolor=transparent] options [backcolor=transparent]=[backcolor=transparent] oss_request_options_create[backcolor=transparent]([backcolor=transparent]p[backcolor=transparent]);
- [backcolor=transparent] init_options[backcolor=transparent]([backcolor=transparent]options[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]/* 初始化参数 */
- [backcolor=transparent] headers1 [backcolor=transparent]=[backcolor=transparent] aos_table_make[backcolor=transparent]([backcolor=transparent]p[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]0[backcolor=transparent]);
- [backcolor=transparent] aos_str_set[backcolor=transparent](&[backcolor=transparent]bucket[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"<您的bucket名字>"[backcolor=transparent]);
- [backcolor=transparent] aos_str_set[backcolor=transparent](&[backcolor=transparent]object[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"<您的object名字>"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]/* 获取起始追加位置 */
- [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]);
- [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]{
- [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]
- [backcolor=transparent] [backcolor=transparent]"x-oss-next-append-position"[backcolor=transparent]));
- [backcolor=transparent] position [backcolor=transparent]=[backcolor=transparent] atoi[backcolor=transparent]([backcolor=transparent]next_append_position[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]/* 追加文件 */
- [backcolor=transparent] headers2 [backcolor=transparent]=[backcolor=transparent] aos_table_make[backcolor=transparent]([backcolor=transparent]p[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]0[backcolor=transparent]);
- [backcolor=transparent] aos_list_init[backcolor=transparent](&[backcolor=transparent]buffer[backcolor=transparent]);
- [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]));
- [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]);
- [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]
- [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]);
- [backcolor=transparent] [backcolor=transparent]/* 判断是否追加成功 */
- [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]{
- [backcolor=transparent] printf[backcolor=transparent]([backcolor=transparent]"append object from buffer succeeded\n"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]}[backcolor=transparent] [backcolor=transparent]else[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] printf[backcolor=transparent]([backcolor=transparent]"append object from buffer failed\n"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]/* 释放资源*/
- [backcolor=transparent] aos_pool_destroy[backcolor=transparent]([backcolor=transparent]p[backcolor=transparent]);
- [backcolor=transparent]}
[backcolor=transparent]注: