下载文件
更新时间:2024-04-24
下载文件
BOS C SDK提供了丰富的文件下载接口,用户可以通过以下方式从BOS中下载文件:
- 下载到本地文件
- 下载到内存中的字符串
- 范围下载
下载到内存中的字符串
用户可以通过如下代码将Object输出到一个文件流中:
C
1 bos_pool_t *p = NULL;
2 bos_string_t bucket;
3 char *object_name = "bos_test_put_object.ts";
4 bos_string_t object;
5 int is_cname = 0;
6 bos_request_options_t *options = NULL;
7 bos_table_t *headers = NULL;
8 bos_table_t *params = NULL;
9 bos_table_t *resp_headers = NULL;
10 bos_status_t *s = NULL;
11 bos_list_t buffer;
12 bos_buf_t *content = NULL;
13 char *expect_content = "test bos c sdk";
14 char *buf = NULL;
15 int64_t len = 0;
16 int64_t size = 0;
17 int64_t pos = 0;
18 char *content_type = NULL;
19
20 bos_pool_create(&p, NULL);
21 options = bos_request_options_create(p);
22 init_test_request_options(options, is_cname);
23 bos_str_set(&bucket, TEST_BUCKET_NAME);
24 bos_str_set(&object, object_name);
25 bos_list_init(&buffer);
26
27 /* test get object to buffer */
28 s = bos_get_object_to_buffer(options, &bucket, &object, headers,
29 params, &buffer, &resp_headers);
直接下载Object到文件
用户可以通过如下代码直接将Object下载到指定文件:
C
1 bos_pool_t *p = NULL;
2 bos_string_t bucket;
3 char *object_name = "bos_test_put_object_from_file2.txt";
4 bos_string_t object;
5 char *filename = "bos_test_get_object_to_file";
6 char *source_filename = __FILE__;
7 bos_string_t file;
8 bos_request_options_t *options = NULL;
9 int is_cname = 0;
10 bos_table_t *headers = NULL;
11 bos_table_t *params = NULL;
12 bos_table_t *resp_headers = NULL;
13 bos_status_t *s = NULL;
14 char *content_type = NULL;
15
16 bos_pool_create(&p, NULL);
17 options = bos_request_options_create(p);
18 init_test_request_options(options, is_cname);
19 bos_str_set(&bucket, TEST_BUCKET_NAME);
20 bos_str_set(&object, object_name);
21 bos_str_set(&file, filename);
22
23 /* test get object to file */
24 s = bos_get_object_to_file(options, &bucket, &object, headers,
25 params, &file, &resp_headers);
范围下载
为了实现更多的功能,可以通过使用apr_table_set(headers, "Range", " bytes=5-13");
来指定下载范围,实现更精细化地获取Object。如果指定的下载范围是0 - 100,则返回第0到第100个字节的数据,包括第100个,共101字节的数据。
C
1void test_get_object_to_buffer_with_range(CuTest *tc)
2{
3 bos_pool_t *p = NULL;
4 bos_string_t bucket;
5 char *object_name = "bos_test_put_object.ts";
6 bos_string_t object;
7 int is_cname = 0;
8 bos_request_options_t *options = NULL;
9 bos_table_t *headers = NULL;
10 bos_table_t *params = NULL;
11 bos_table_t *resp_headers = NULL;
12 bos_status_t *s = NULL;
13 bos_list_t buffer;
14 bos_buf_t *content = NULL;
15 char *expect_content = "bos c sdk";
16 char *buf = NULL;
17 int64_t len = 0;
18 int64_t size = 0;
19 int64_t pos = 0;
20
21 bos_pool_create(&p, NULL);
22 options = bos_request_options_create(p);
23 init_test_request_options(options, is_cname);
24 bos_str_set(&bucket, TEST_BUCKET_NAME);
25 bos_str_set(&object, object_name);
26 headers = bos_table_make(p, 1);
27 apr_table_set(headers, "Range", " bytes=5-13");
28 bos_list_init(&buffer);
29 /* test get object to buffer */
30 s = bos_get_object_to_buffer(options, &bucket, &object, headers,
31 params, &buffer, &resp_headers);
32
33 /* get buffer len */
34 len = bos_buf_list_len(&buffer);
35
36 buf = bos_pcalloc(p, (apr_size_t)(len + 1));
37 buf[len] = '\0';
38
39 /* copy buffer content to memory */
40 bos_list_for_each_entry(bos_buf_t, content, &buffer, node) {
41 size = bos_buf_size(content);
42 memcpy(buf + pos, content->pos, (size_t)size);
43 pos += size;
44 }
通过apr_table_set(headers, "Range", " bytes=5-13")的方法可以设置返回Object的范围。用户也可以用此功能实现文件的分段下载和断点续传。