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