开发者社区> 问答> 正文

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

取消分片上传事件

  1. void abort_multipart_upload()
  2. {
  3.     aos_pool_t *p = NULL;
  4.     aos_string_t bucket;
  5.     aos_string_t object;
  6.     aos_table_t *headers = NULL;
  7.     aos_table_t *resp_headers = NULL;
  8.     oss_request_options_t *options = NULL;
  9.     aos_string_t upload_id;
  10.     aos_status_t *s = NULL;
  11.     aos_pool_create(&p, NULL);
  12.     /* 创建并初始化options */
  13.     options = oss_request_options_create(p);
  14.     init_options(options);
  15.     /* 初始化参数 */
  16.     headers = aos_table_make(p, 1);
  17.     aos_str_set(&bucket, "<您的bucket名字>");
  18.     aos_str_set(&object, "<您的object名字>");
  19.     /* 初始化分片上传,获取一个Upload Id*/
  20.     s = oss_init_multipart_upload(options, &bucket, &object,
  21.                                   &upload_id, headers, &resp_headers);
  22.     if (aos_status_is_ok(s)) {
  23.         printf("Init multipart upload succeeded, upload_id:%.*s\n",
  24.                upload_id.len, upload_id.data);
  25.     } else {
  26.         printf("Init multipart upload failed\n");
  27.     }
  28.     /* 取消这次分片上传 */
  29.     s = oss_abort_multipart_upload(options, &bucket, &object, &upload_id,
  30.                                    &resp_headers);
  31.     /* 判断取消分片上传是否成功 */
  32.     if (aos_status_is_ok(s)) {
  33.         printf("Abort multipart upload succeeded, upload_id::%.*s\n",
  34.                upload_id.len, upload_id.data);
  35.     } else {
  36.         printf("Abort multipart upload failed\n");
  37.     }    
  38.     /* 释放资源 */
  39.     aos_pool_destroy(p);
  40. }

注:
  • 当一个分片上传事件被中止后,就不能再使用这个upload_id做任何操作,已经上传的分片数据也会被删除。
  • 完整代码参考:GitHub


设置元信息


文件元信息(Object Meta),是对用户上传到OSS的文件的属性描述,分为两种:HTTP标准属性(HTTP Headers)和用户自定义元信息(User Meta)。 文件元信息可以在上传(流上传、文件上传、追加上传、分片上传、断点续传),或 拷贝时设置。元信息的名称大小写不敏感。更多文件元信息的介绍,请参看 文件元信息

HTTP标准属性


常用的HTTP标准属性说明如下,更详细的说明请参考 RFC2616
名称描述默认值
Content-MD5文件数据校验,设置了该值后OSS会启用文件内容MD5校验,把您提供的MD5与文件的MD5比较,不一致会返回错误
Content-Type文件的MIME,定义文件的类型及网页编码,决定浏览器将以什么形式、什么编码读取文件。如果用户没有指定则根据Key或文件名的扩展名生成,如果没有扩展名则填默认值application/octet-stream
Content-Disposition指示MIME用户代理如何显示附加的文件,打开或下载,及文件名称
Content-Length上传的数据的长度,超过长度的数据会被截断,不足为实际值数据的实际长度
Expires缓存过期时间,OSS未使用,格式是格林威治时间(GMT)
Cache-Control指定该Object被下载时的网页的缓存行为
ExpectHTTP客户端期望特定的行为,目前支持100-continue与libcurl实现相关
Transfer-Encoding传输数据时候编码方式,目前支持chunked与libcurl实现相关
  1. void put_object_from_file()
  2. {
  3.     aos_pool_t *p = NULL;
  4.     aos_string_t bucket;
  5.     aos_string_t object;
  6.     aos_table_t *headers = NULL;
  7.     aos_table_t *resp_headers = NULL;
  8.     oss_request_options_t *options = NULL;
  9.     char *filename = __FILE__;
  10.     aos_status_t *s = NULL;
  11.     aos_string_t file;
  12.     aos_pool_create(&p, NULL);
  13.     /* 创建并初始化options */
  14.     options = oss_request_options_create(p);
  15.     init_options(options);
  16.     /* 初始化参数 */
  17.     headers = aos_table_make(options->pool, 2);
  18.     apr_table_set(headers, OSS_CONTENT_TYPE, "image/jpeg");
  19.     apr_table_set(headers, OSS_CONTENT_MD5, "eB5eJF1ptWaXm4bijSPyxw==");
  20.     aos_str_set(&bucket, "<您的bucket名字>");
  21.     aos_str_set(&object, "<您的object名字>");
  22.     aos_str_set(&file, filename);
  23.     /* 上传文件 */
  24.     s = oss_put_object_from_file(options, &bucket, &object, &file,
  25.                                  headers, &resp_headers);
  26.     /* 判断是否上传成功 */
  27.     if (aos_status_is_ok(s)) {
  28.         printf("put object from file succeeded\n");
  29.     } else {
  30.         printf("put object from file failed\n");
  31.     }
  32.     /* 释放资源*/
  33.     aos_pool_destroy(p);
  34. }

注:
  • 数据、文件MD5值的计算请参看Put Object
  • HTTP标准属性会改变数据传输行为,请在明确知晓其行为后设置。


用户自定义元信息


为了便于用户对文件进行更多描述,OSS中规定所有以x-oss-meta-为前缀的参数视为User Meta,一个文件可以有多个类似的参数。User Meta会在下载GetObject或HeadObject的时,在HTTP头部中返回。
  1. void put_object_from_file()
  2. {
  3.     aos_pool_t *p = NULL;
  4.     aos_string_t bucket;
  5.     aos_string_t object;
  6.     aos_table_t *headers = NULL;
  7.     aos_table_t *resp_headers = NULL;
  8.     oss_request_options_t *options = NULL;
  9.     char *filename = __FILE__;
  10.     aos_status_t *s = NULL;
  11.     aos_string_t file;
  12.     aos_pool_create(&p, NULL);
  13.     /* 创建并初始化options */
  14.     options = oss_request_options_create(p);
  15.     init_options(options);
  16.     /* 初始化参数 */
  17.     headers = aos_table_make(options->pool, 4);
  18.     apr_table_set(headers, OSS_CONTENT_TYPE, "image/jpeg");
  19.     apr_table_set(headers, OSS_CONTENT_MD5, "eB5eJF1ptWaXm4bijSPyxw==");
  20.     apr_table_set(headers, "x-oss-meta-author", "mingdi");
  21.     apr_table_set(headers, "x-oss-meta-category", "computer");
  22.     aos_str_set(&bucket, "<您的bucket名字>");
  23.     aos_str_set(&object, "<您的object名字>");
  24.     aos_str_set(&file, filename);
  25.     /* 上传文件 */
  26.     s = oss_put_object_from_file(options, &bucket, &object, &file,
  27.                                  headers, &resp_headers);
  28.     /* 判断是否上传成功 */
  29.     if (aos_status_is_ok(s)) {
  30.         printf("put object from file succeeded\n");
  31.     } else {
  32.         printf("put object from file failed\n");
  33.     }
  34.     /* 释放资源*/
  35.     aos_pool_destroy(p);
  36. }


上传回调


OSS在上传文件完成的时,可以提供 回调(Callback)给应用服务器。用户只需要在发送给OSS的请求中携带相应的回调参数,即能实现回调。支持上传回调接口有:PutObject、PostObject、CompleteMultipartUpload。您想了解上传回调的更详细信息,请参考 上传回调。下面以PutObject为例说明上传回调的用法。
  1. void put_object_from_buffer_with_callback()
  2. {
  3.     aos_pool_t *p = NULL;
  4.     char *str = "test oss c sdk";
  5.     aos_status_t *s = NULL;
  6.     int is_cname = 0;
  7.     aos_string_t bucket;
  8.     aos_string_t object;
  9.     aos_table_t *headers = NULL;
  10.     oss_request_options_t *options = NULL;
  11.     aos_table_t *resp_headers = NULL;
  12.     aos_list_t resp_body;
  13.     aos_list_t buffer;
  14.     aos_buf_t *content;
  15.     char *buf = NULL;
  16.     int64_t len = 0;
  17.     int64_t size = 0;
  18.     int64_t pos = 0;
  19.     char b64_buf[1024];
  20.     int b64_len;
  21.     /* JSON format */
  22.     char *callback =  "{"
  23.         "\"callbackUrl\":\"http://callback.oss-demo.com:23450\","
  24.         "\"callbackHost\":\"oss-cn-hangzhou.aliyuncs.com\","
  25.         "\"callbackBody\":\"bucket=${bucket}&object=${object}&size=${size}&mimeType=${mimeType}\","
  26.         "\"callbackBodyType\":\"application/x-www-form-urlencoded\""
  27.         "}";
  28.     /* init sample */
  29.     aos_pool_create(&p, NULL);
  30.     options = oss_request_options_create(p);
  31.     init_sample_request_options(options, is_cname);
  32.     aos_str_set(&bucket, BUCKET_NAME);
  33.     aos_str_set(&object, OBJECT_NAME);
  34.     aos_list_init(&resp_body);
  35.     aos_list_init(&buffer);
  36.     content = aos_buf_pack(options->pool, str, strlen(str));
  37.     aos_list_add_tail(&content->node, &buffer);
  38.     /* put call into header */
  39.     b64_len = aos_base64_encode((unsigned char*)callback, strlen(callback), b64_buf);
  40.     b64_buf[b64_len] = '\0';
  41.     headers = aos_table_make(p, 1);
  42.     apr_table_set(headers, OSS_CALLBACK, b64_buf);
  43.     /* test put object */
  44.     s = oss_do_put_object_from_buffer(options, &bucket, &object, &buffer,
  45.         headers, NULL, NULL, &resp_headers, &resp_body);
  46.     if (aos_status_is_ok(s)) {
  47.         printf("put object from buffer succeeded\n");
  48.     } else {
  49.         printf("put object from buffer failed\n");      
  50.     }    
  51.     /* get buffer len */
  52.     len = aos_buf_list_len(&resp_body);
  53.     buf = (char *)aos_pcalloc(p, (apr_size_t)(len + 1));
  54.     buf[len] = '\0';
  55.     /* copy buffer content to memory */
  56.     aos_list_for_each_entry(aos_buf_t, content, &resp_body, node) {
  57.         size = aos_buf_size(content);
  58.         memcpy(buf + pos, content->pos, (size_t)size);
  59.         pos += size;
  60.     }
  61.     aos_pool_destroy(p);
  62. }

注:


进度条


OSS C SDK支持进度条功能,指示上传/下载的进度。下面的代码以PutObject为例,说明进度条功能的使用方法。
  1. void percentage(int64_t consumed_bytes, int64_t total_bytes)
  2. {
  3.     assert(total_bytes >= consumed_bytes);
  4.     printf("%%%" APR_INT64_T_FMT "\n", consumed_bytes * 100 / total_bytes);
  5. }
  6. void put_and_get_from_file_with_progress()
  7. {
  8.     aos_pool_t *p = NULL;
  9.     aos_status_t *s = NULL;
  10.     int is_cname = 0;
  11.     aos_string_t bucket;
  12.     aos_string_t object;
  13.     aos_string_t filename;
  14.     oss_request_options_t *options = NULL;
  15.     aos_table_t *resp_headers = NULL;
  16.     aos_list_t resp_body;
  17.     char *download_filename = "get_object_to_local_file.txt";
  18.     /* init test*/
  19.     aos_pool_create(&p, NULL);
  20.     options = oss_request_options_create(p);
  21.     init_sample_request_options(options, is_cname);
  22.     aos_str_set(&bucket, BUCKET_NAME);
  23.     aos_str_set(&object, OBJECT_NAME);
  24.     aos_str_set(&filename, __FILE__);
  25.     aos_list_init(&resp_body);
  26.     /* put object */
  27.     s = oss_do_put_object_from_file(options, &bucket, &object, &filename, NULL, NULL, percentage, &resp_headers, &resp_body);
  28.     if (aos_status_is_ok(s)) {
  29.         printf("put object from file succeeded\n");
  30.     } else {
  31.         printf("put object from file failed\n");
  32.         aos_pool_destroy(p);
  33.         return;
  34.     }
  35.     aos_pool_destroy(p);
  36. }

注:
  • 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

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

相关电子书

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