Download file
Download file
The BOS C SDK provides a variety of file download APIs, allowing users to download files from BOS in the following ways:
- Download to a local file
- Download to an in-memory string
- Range download
Download to an in-memory string
Users can export the object into a file stream using the following code:
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 bos_pool_create(&p, NULL);
20 options = bos_request_options_create(p);
21 init_test_request_options(options, is_cname);
22 bos_str_set(&bucket, TEST_BUCKET_NAME);
23 bos_str_set(&object, object_name);
24 bos_list_init(&buffer);
25 /* test get object to buffer */
26 s = bos_get_object_to_buffer(options, &bucket, &object, headers,
27 params, &buffer, &resp_headers);
Directly download an object to a file
Users can directly download an object to the specified file using the following code:
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 bos_pool_create(&p, NULL);
16 options = bos_request_options_create(p);
17 init_test_request_options(options, is_cname);
18 bos_str_set(&bucket, TEST_BUCKET_NAME);
19 bos_str_set(&object, object_name);
20 bos_str_set(&file, filename);
21 /* test get object to file */
22 s = bos_get_object_to_file(options, &bucket, &object, headers,
23 params, &file, &resp_headers);
Range download
To implement more functions, you can use apr_table_set(headers, "Range", " bytes=5-13"); to specify the download range for more refined acquisition of the object. If the specified download range is 0-100, it will return data from byte 0 to byte 100, including byte 100, with a total of 101 bytes of data.
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 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 headers = bos_table_make(p, 1);
26 apr_table_set(headers, "Range", " bytes=5-13");
27 bos_list_init(&buffer);
28 /* test get object to buffer */
29 s = bos_get_object_to_buffer(options, &bucket, &object, headers,
30 params, &buffer, &resp_headers);
31 /* get buffer len */
32 len = bos_buf_list_len(&buffer);
33 buf = bos_pcalloc(p, (apr_size_t)(len + 1));
34 buf[len] = '\0';
35 /* copy buffer content to memory */
36 bos_list_for_each_entry(bos_buf_t, content, &buffer, node) {
37 size = bos_buf_size(content);
38 memcpy(buf + pos, content->pos, (size_t)size);
39 pos += size;
40 }
The method apr_table_set(headers, "Range", "bytes=5-13") allows users to specify the range of the returned object. This function can be used to enable segmented downloads and resumable file uploads.
