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:
- Simple streaming download
- Download to a local file
- Download to an in-memory string
- Resumable download
- Range download
- Download progress bar
Simple streaming download
Users can export the object into a file stream using the following code:
1void getObject(Client& client, const std::string& bucketName, const std::string& objectKey) {
2
3 // Get file stream
4 FileOutputStream outStream("test.txt");
5 //Method I: Via request and response
6 //Initialize request
7 GetObjectRequest getObjectRequest(bucketName, objectKey);
8 //Use outStream as the output object
9 GetObjectResponse getObjectResponse(&outStream);
10 int ret = client.get_object(getObjectRequest, &getObjectResponse);
11 //Method II: Via a convenience API, write the object to the outStream file stream
12 ret = client.download_file(bucketName, objectKey, outStream);
13 ...
14}
Directly download an object to a file
Users can directly download an object to the specified file using the following code:
1// Download object to file
2std::string localFileName = "test.txt";
3ObjectMetaData ObjectMetaData;
4 //Set the object reading range
5int64_t start = 0;
6int64_t length = 100;
7 //Download object
8 int ret = client.download_file(bucketName, objectKey, localFileName, start, length, &ObjectMetaData);//Save to test.txt, ObjectMetaData
9...
When downloading an object directly to the specified file using the above method, the method returns an ObjectMetaData object.
Use download progress bar
1// Upload progress callback function
2 //Note: There must be no time-consuming/blocking operations in this callback function, as it will affect data download performance.
3 //increment: The amount of data downloaded this time
4 //transfered: The total amount of data downloaded
5 //total: The total amount of data to be downloaded
6 //userData: User-defined data, such as object bucket + key, etc.
7void progress_callback(int64_t increment, int64_t transfered, int64_t total, void* user_data) {
8 std::cout << "progress_callback[" << user_data << "] => " <<
9 increment <<" ," << transfered << "," << total << std::endl;
10}
11 //Local file to receive data
12std::string filename = "/tmp/get_file_test";
13FileOutputStream outStream(filename);
14GetObjectRequest req(BUCKET, "transfer_progress_2");
15GetObjectResponse rsp(&outStream);
16 //Set data related to download progress bar
17TransferProgress progress;
18progress.transfer_progress_cb = progress_callback;
19rsp.set_progress(progress);
20 //Download data to the local filename
21int ret = client()->get_object(req, &rsp);
22if (ret) {
23 LOGF(WARN, "client err: %d", ret);
24}
25if (rsp.is_fail()) {
26 LOGF(WARN,
27 "get_object: [status_code = %d], [message = %s], [requestid = %s]",
28 rsp.status_code(),
29 rsp.error().message().c_str(),
30 rsp.error().request_id().c_str());
31}
Use single-link rate limit
1//Local file to receive data
2std::string filename = "/tmp/get_file_test";
3FileOutputStream outStream(filename);
4GetObjectRequest req(BUCKET, "traffic_limit_file");
5 //Set download speed limit to 819,200 bit/s (100KB/s)
6 //The speed limit range is: [819,200-838,860,800] i.e., [100 KB-100 MB/s]
7req.set_traffic_limit(819200);
8GetObjectResponse rsp(&outStream);
9 //Download data to the local filename
10int ret = client()->get_object(req, &rsp);
11if (ret) {
12 LOGF(WARN, "client err: %d", ret);
13}
14if (rsp.is_fail()) {
15 LOGF(WARN,
16 "get_object: [status_code = %d], [message = %s], [requestid = %s]",
17 rsp.status_code(),
18 rsp.error().message().c_str(),
19 rsp.error().request_id().c_str());
20}
Download to an in-memory string
This method directly stores the object in a string without writing to the disk
1std::string inMemoryData;
2 //Method I: Via request and response
3GetObjectRequest getObjectRequest(bucketName, objectKey);
4 //Use inMemoryData as the output object
5GetObjectResponse getObjectResponse(&inMemoryData);
6int ret = client.get_object(getObjectRequest, &getObjectResponse);
7if (ret != 0) {
8 return ret;
9}
10 //Method II: Via a convenience API
11ret = client.get_object(bucketName, objectKey, &inMemoryData);
12 //Print it out
13std::cout << inMemoryData << std::endl;
Range download
To implement more functions, you can use GetObjectRequest 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, i.e., [0, 100].
1GetObjectRequest getObjectRequest(bucketName, objectKey);
2 //Initialize the file output stream
3FileOutputStream outStream("test.txt");
4GetObjectResponse getObjectResponse(&outStream);
5 //Set the download range to [0, 100] bytes of data
6int start = 0;
7int end = 100;
8getObjectRequest.set_range(start, end);
9int ret = client.get_object(getObjectRequest, &getObjectResponse);
10if (ret != 0) {
11 return ret;
12}
13return 0;
The range of the returned object can be set through the set_range的getObjectRequest. Users can utilize this function to achieve segmented download and resumable upload of files.
Specify the range end as a larger value to download object data starting from start.
