取消分片上传事件
- void abort_multipart_upload()
- {
- aos_pool_t *p = NULL;
- aos_string_t bucket;
- aos_string_t object;
- aos_table_t *headers = NULL;
- aos_table_t *resp_headers = NULL;
- oss_request_options_t *options = NULL;
- aos_string_t upload_id;
- aos_status_t *s = NULL;
- aos_pool_create(&p, NULL);
- /* 创建并初始化options */
- options = oss_request_options_create(p);
- init_options(options);
- /* 初始化参数 */
- headers = aos_table_make(p, 1);
- aos_str_set(&bucket, "<您的bucket名字>");
- aos_str_set(&object, "<您的object名字>");
- /* 初始化分片上传,获取一个Upload Id*/
- s = oss_init_multipart_upload(options, &bucket, &object,
- &upload_id, headers, &resp_headers);
- if (aos_status_is_ok(s)) {
- printf("Init multipart upload succeeded, upload_id:%.*s\n",
- upload_id.len, upload_id.data);
- } else {
- printf("Init multipart upload failed\n");
- }
- /* 取消这次分片上传 */
- s = oss_abort_multipart_upload(options, &bucket, &object, &upload_id,
- &resp_headers);
- /* 判断取消分片上传是否成功 */
- if (aos_status_is_ok(s)) {
- printf("Abort multipart upload succeeded, upload_id::%.*s\n",
- upload_id.len, upload_id.data);
- } else {
- printf("Abort multipart upload failed\n");
- }
- /* 释放资源 */
- aos_pool_destroy(p);
- }
注:
- 当一个分片上传事件被中止后,就不能再使用这个upload_id做任何操作,已经上传的分片数据也会被删除。
- 完整代码参考:GitHub
设置元信息
文件元信息(Object Meta),是对用户上传到OSS的文件的属性描述,分为两种:HTTP标准属性(HTTP Headers)和用户自定义元信息(User Meta)。 文件元信息可以在上传(流上传、文件上传、追加上传、分片上传、断点续传),或
拷贝时设置。元信息的名称大小写不敏感。更多文件元信息的介绍,请参看
文件元信息。
HTTP标准属性
常用的HTTP标准属性说明如下,更详细的说明请参考
RFC2616。
- void put_object_from_file()
- {
- aos_pool_t *p = NULL;
- aos_string_t bucket;
- aos_string_t object;
- aos_table_t *headers = NULL;
- aos_table_t *resp_headers = NULL;
- oss_request_options_t *options = NULL;
- char *filename = __FILE__;
- aos_status_t *s = NULL;
- aos_string_t file;
- aos_pool_create(&p, NULL);
- /* 创建并初始化options */
- options = oss_request_options_create(p);
- init_options(options);
- /* 初始化参数 */
- headers = aos_table_make(options->pool, 2);
- apr_table_set(headers, OSS_CONTENT_TYPE, "image/jpeg");
- apr_table_set(headers, OSS_CONTENT_MD5, "eB5eJF1ptWaXm4bijSPyxw==");
- aos_str_set(&bucket, "<您的bucket名字>");
- aos_str_set(&object, "<您的object名字>");
- aos_str_set(&file, filename);
- /* 上传文件 */
- s = oss_put_object_from_file(options, &bucket, &object, &file,
- headers, &resp_headers);
- /* 判断是否上传成功 */
- if (aos_status_is_ok(s)) {
- printf("put object from file succeeded\n");
- } else {
- printf("put object from file failed\n");
- }
- /* 释放资源*/
- aos_pool_destroy(p);
- }
注:
- 数据、文件MD5值的计算请参看Put Object。
- HTTP标准属性会改变数据传输行为,请在明确知晓其行为后设置。
用户自定义元信息
为了便于用户对文件进行更多描述,OSS中规定所有以x-oss-meta-为前缀的参数视为User Meta,一个文件可以有多个类似的参数。User Meta会在下载GetObject或HeadObject的时,在HTTP头部中返回。
- void put_object_from_file()
- {
- aos_pool_t *p = NULL;
- aos_string_t bucket;
- aos_string_t object;
- aos_table_t *headers = NULL;
- aos_table_t *resp_headers = NULL;
- oss_request_options_t *options = NULL;
- char *filename = __FILE__;
- aos_status_t *s = NULL;
- aos_string_t file;
- aos_pool_create(&p, NULL);
- /* 创建并初始化options */
- options = oss_request_options_create(p);
- init_options(options);
- /* 初始化参数 */
- headers = aos_table_make(options->pool, 4);
- apr_table_set(headers, OSS_CONTENT_TYPE, "image/jpeg");
- apr_table_set(headers, OSS_CONTENT_MD5, "eB5eJF1ptWaXm4bijSPyxw==");
- apr_table_set(headers, "x-oss-meta-author", "mingdi");
- apr_table_set(headers, "x-oss-meta-category", "computer");
- aos_str_set(&bucket, "<您的bucket名字>");
- aos_str_set(&object, "<您的object名字>");
- aos_str_set(&file, filename);
- /* 上传文件 */
- s = oss_put_object_from_file(options, &bucket, &object, &file,
- headers, &resp_headers);
- /* 判断是否上传成功 */
- if (aos_status_is_ok(s)) {
- printf("put object from file succeeded\n");
- } else {
- printf("put object from file failed\n");
- }
- /* 释放资源*/
- aos_pool_destroy(p);
- }
上传回调
OSS在上传文件完成的时,可以提供
回调(Callback)给应用服务器。用户只需要在发送给OSS的请求中携带相应的回调参数,即能实现回调。支持上传回调接口有:PutObject、PostObject、CompleteMultipartUpload。您想了解上传回调的更详细信息,请参考
上传回调。下面以PutObject为例说明上传回调的用法。
- void put_object_from_buffer_with_callback()
- {
- aos_pool_t *p = NULL;
- char *str = "test oss c sdk";
- aos_status_t *s = NULL;
- int is_cname = 0;
- aos_string_t bucket;
- aos_string_t object;
- aos_table_t *headers = NULL;
- oss_request_options_t *options = NULL;
- aos_table_t *resp_headers = NULL;
- aos_list_t resp_body;
- aos_list_t buffer;
- aos_buf_t *content;
- char *buf = NULL;
- int64_t len = 0;
- int64_t size = 0;
- int64_t pos = 0;
- char b64_buf[1024];
- int b64_len;
- /* JSON format */
- char *callback = "{"
- "\"callbackUrl\":\"http://callback.oss-demo.com:23450\","
- "\"callbackHost\":\"oss-cn-hangzhou.aliyuncs.com\","
- "\"callbackBody\":\"bucket=${bucket}&object=${object}&size=${size}&mimeType=${mimeType}\","
- "\"callbackBodyType\":\"application/x-www-form-urlencoded\""
- "}";
- /* init sample */
- aos_pool_create(&p, NULL);
- options = oss_request_options_create(p);
- init_sample_request_options(options, is_cname);
- aos_str_set(&bucket, BUCKET_NAME);
- aos_str_set(&object, OBJECT_NAME);
- aos_list_init(&resp_body);
- aos_list_init(&buffer);
- content = aos_buf_pack(options->pool, str, strlen(str));
- aos_list_add_tail(&content->node, &buffer);
- /* put call into header */
- b64_len = aos_base64_encode((unsigned char*)callback, strlen(callback), b64_buf);
- b64_buf[b64_len] = '\0';
- headers = aos_table_make(p, 1);
- apr_table_set(headers, OSS_CALLBACK, b64_buf);
- /* test put object */
- s = oss_do_put_object_from_buffer(options, &bucket, &object, &buffer,
- headers, NULL, NULL, &resp_headers, &resp_body);
- if (aos_status_is_ok(s)) {
- printf("put object from buffer succeeded\n");
- } else {
- printf("put object from buffer failed\n");
- }
- /* get buffer len */
- len = aos_buf_list_len(&resp_body);
- buf = (char *)aos_pcalloc(p, (apr_size_t)(len + 1));
- buf[len] = '\0';
- /* copy buffer content to memory */
- aos_list_for_each_entry(aos_buf_t, content, &resp_body, node) {
- size = aos_buf_size(content);
- memcpy(buf + pos, content->pos, (size_t)size);
- pos += size;
- }
- aos_pool_destroy(p);
- }
注:
- oss_do_complete_multipart_upload、oss_resumable_upload_file都支持上传回调,代码请参考:GitHub。
- 上传回调 的调试方法和错误排除,请参考上传回调错误及排除。
进度条
OSS C SDK支持进度条功能,指示上传/下载的进度。下面的代码以PutObject为例,说明进度条功能的使用方法。
- void percentage(int64_t consumed_bytes, int64_t total_bytes)
- {
- assert(total_bytes >= consumed_bytes);
- printf("%%%" APR_INT64_T_FMT "\n", consumed_bytes * 100 / total_bytes);
- }
- void put_and_get_from_file_with_progress()
- {
- aos_pool_t *p = NULL;
- aos_status_t *s = NULL;
- int is_cname = 0;
- aos_string_t bucket;
- aos_string_t object;
- aos_string_t filename;
- oss_request_options_t *options = NULL;
- aos_table_t *resp_headers = NULL;
- aos_list_t resp_body;
- char *download_filename = "get_object_to_local_file.txt";
- /* init test*/
- aos_pool_create(&p, NULL);
- options = oss_request_options_create(p);
- init_sample_request_options(options, is_cname);
- aos_str_set(&bucket, BUCKET_NAME);
- aos_str_set(&object, OBJECT_NAME);
- aos_str_set(&filename, __FILE__);
- aos_list_init(&resp_body);
- /* put object */
- s = oss_do_put_object_from_file(options, &bucket, &object, &filename, NULL, NULL, percentage, &resp_headers, &resp_body);
- if (aos_status_is_ok(s)) {
- printf("put object from file succeeded\n");
- } else {
- printf("put object from file failed\n");
- aos_pool_destroy(p);
- return;
- }
- aos_pool_destroy(p);
- }
注:
- oss_do_put_object_from_buffer、oss_do_put_object_from_file、oss_do_append_object_from_buffer、oss_do_append_object_from_file、oss_do_upload_part_from_buffer、oss_do_upload_part_from_file、oss_resumable_upload_file支持进度条功能,代码请参考:GitHub。