全部产品
对象存储 OSS

下载文件

更新时间:2017-08-23 16:31:45   分享:   

OSS C SDK提供了丰富的文件下载接口,用户可以通过以下方式从OSS下载文件:

  • 下载文件到内存
  • 下载文件到本地文件
  • 分段下载文件
  • 断点续传下载

下载文件到内存

通过oss_get_object_to_buffer接口,可以实现将文件下载到内存中:

  1. aos_pool_t *p;
  2. oss_request_options_t *options;
  3. aos_status_t *s;
  4. aos_table_t *headers;
  5. aos_table_t *params;
  6. aos_table_t *resp_headers;
  7. char *bucket_name = "<您的bucket名字>";
  8. char *object_name = "<您的object名字>";
  9. aos_string_t bucket;
  10. aos_string_t object;
  11. aos_list_t buffer;
  12. aos_buf_t *content;
  13. char *buf;
  14. int64_t len = 0;
  15. int64_t size = 0;
  16. int64_t pos = 0;
  17. aos_pool_create(&p, NULL);
  18. /* 创建并初始化options */
  19. options = oss_request_options_create(p);
  20. init_options(options);
  21. /* 初始化参数 */
  22. aos_str_set(&object, object_name);
  23. aos_str_set(&bucket, bucket_name);
  24. headers = aos_table_make(p, 0);
  25. params = aos_table_make(p, 0);
  26. /* 下载文件 */
  27. aos_list_init(&buffer);
  28. s = oss_get_object_to_buffer(options, &bucket, &object, &buffer, headers, params, &resp_headers);
  29. if (aos_status_is_ok(s)) {
  30. printf("get object succeeded\n");
  31. /* 将下载内容拷贝到buffer中*/
  32. len = aos_buf_list_len(&buffer);
  33. buf = aos_pcalloc(p, len + 1);
  34. buf[len] = '\0';
  35. aos_list_for_each_entry(content, &buffer, node) {
  36. size = aos_buf_size(content);
  37. memcpy(buf + pos, content->pos, size);
  38. pos += size;
  39. }
  40. } else {
  41. printf("get object failed\n");
  42. }
  43. aos_pool_destroy(p);

注:

  • 2.0.0相对于1.0.0版本,oss_get_object_to_buffer接口增加了params参数,同时headers和params允许为NULL,1.0.0及其之前版本不支持为NULL
  • oss_get_object_to_buffer_by_url和oss_get_object_to_file_by_url参数也增加了params参数
  • 完整代码参考:GitHub

下载文件到本地文件

通过oss_get_object_to_file接口,可以实现将文件下载到指定文件:

  1. aos_pool_t *p;
  2. oss_request_options_t *options;
  3. aos_status_t *s;
  4. aos_table_t *headers;
  5. aos_table_t *params;
  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. aos_pool_create(&p, NULL);
  14. /* 创建并初始化options */
  15. options = oss_request_options_create(p);
  16. init_options(options);
  17. /* 初始化参数 */
  18. aos_str_set(&bucket, bucket_name);
  19. aos_str_set(&object, object_name);
  20. aos_str_set(&file, filepath);
  21. headers = aos_table_make(p, 0);
  22. params = aos_table_make(p, 0);
  23. /* 下载文件 */
  24. s = oss_get_object_to_file(options, &bucket, &object, &file, headers, params, &resp_headers);
  25. if (aos_status_is_ok(s)) {
  26. printf("get object succeeded\n");
  27. } else {
  28. printf("get object failed\n");
  29. }
  30. aos_pool_destroy(p);

注:

  • 2.0.0相对于1.0.0版本,oss_get_object_to_file接口增加了params参数,同时headerss和params允许为NULL,1.0.0及其之前版本不支持为NULL。
  • 如果本地有同名文件 filepath,本地文件将会被覆盖;
  • 完整代码参考:GitHub

分段下载文件

通过设置Range指定返回文件的传输范围,可以实现文件的分段下载。以下代码实现分段下载文件到内存中:

  1. aos_pool_t *p;
  2. oss_request_options_t *options;
  3. aos_status_t *s;
  4. aos_table_t *headers;
  5. aos_table_t *params;
  6. aos_table_t *resp_headers;
  7. char *bucket_name = "<您的bucket名字>";
  8. char *object_name = "<您的object名字>";
  9. aos_string_t bucket;
  10. aos_string_t object;
  11. aos_list_t buffer;
  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. params = NULL;
  20. headers = aos_table_make(p, 1);
  21. /* 设置Range,读取文件的指定范围,bytes=20-100包括第20和第100个字符 */
  22. apr_table_set(headers, "Range", "bytes=20-100");
  23. aos_list_init(&buffer);
  24. /* 分片下载文件 */
  25. s = oss_get_object_to_buffer(options, &bucket, &object, headers, params, &buffer, &resp_headers);
  26. if (aos_status_is_ok(s)) {
  27. printf("get object succeeded\n");
  28. } else {
  29. printf("get object failed\n");
  30. }
  31. aos_pool_destroy(p);

断点续传下载

大文件下载时,如果网络不稳定或者发生其它异常,则整个下载会失败。用户不得不重新下载,造成浪费资源,而且在网络不稳定的情况下,往往要重试多次。断点续传下载,支持并发、断点续传,通过 oss_resumable_clt_params_t 控制断点续传的行为,它有以下参数:

  • part_size 分片大小,从1B到5GB,单位是byte
  • thread_num 并发线程数,默认为1
  • enable_checkpoint 是否开启断点续传,默认不开启
  • checkpoint_path checkpoint文件的路径,默认放在上传文件目录{upload_file_path}.cp

断点续传实现的原理是,将要下载文件分成若干个分片分别下载,所有分片都下载成功后,完成整个文件的下载。在下载的过程中会记录当前下载的进度信息(记录在checkpoint文件中),如果下载过程中某一分片下载失败,再次下载时会从checkpoint文件中记录的点继续下载。这要求再次调用时要指定与上次相同的checkpoint文件。下载完成后,checkpoint文件会被删除。

  1. void resumable_download_with_resumable()
  2. {
  3. aos_pool_t *p = NULL;
  4. aos_string_t bucket;
  5. aos_string_t object;
  6. aos_string_t filename;
  7. aos_status_t *s = NULL;
  8. int is_cname = 0;
  9. aos_table_t *headers = NULL;
  10. aos_table_t *resp_headers = NULL;
  11. oss_request_options_t *options = NULL;
  12. oss_resumable_clt_params_t *clt_params;
  13. aos_pool_create(&p, NULL);
  14. options = oss_request_options_create(p);
  15. init_sample_request_options(options, is_cname);
  16. headers = aos_table_make(p, 0);
  17. aos_str_set(&bucket, BUCKET_NAME);
  18. aos_str_set(&object, "my_key_2.zip");
  19. aos_str_set(&filename, "local_big_file_2.zip");
  20. // download
  21. clt_params = oss_create_resumable_clt_params_content(p, 1024 * 100, 3, AOS_TRUE, NULL);
  22. s = oss_resumable_download_file(options, &bucket, &object, &filename, headers, NULL,
  23. clt_params, NULL, &resp_headers);
  24. if (aos_status_is_ok(s)) {
  25. printf("download succeeded\n");
  26. } else {
  27. printf("download failed\n");
  28. }
  29. aos_pool_destroy(p);
  30. }

注:

  • 断点续传下载支持进度条功能。
  • 完整代码参考:GitHub

进度条

OSS C SDK支持进度条功能,指示上传/下载的进度。下面的代码以GetObject为例,说明进度条功能的使用方法。

  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. /* get object */
  37. aos_pool_create(&p, NULL);
  38. options = oss_request_options_create(p);
  39. init_sample_request_options(options, is_cname);
  40. aos_str_set(&filename, download_filename);
  41. s = oss_do_get_object_to_file(options, &bucket, &object, NULL, NULL, &filename, percentage, NULL);
  42. if (aos_status_is_ok(s)) {
  43. printf("get object to file succeeded\n");
  44. } else {
  45. printf("get object to file failed\n");
  46. }
  47. aos_pool_destroy(p);
  48. }

注:

  • oss_do_get_object_to_buffer、oss_do_put_object_from_file支持进度条功能,代码请参考:GitHub
本文导读目录
本文导读目录
以上内容是否对您有帮助?