Rust
Rust
Download Binary File to In-Memory Bytes
See more Google Drive Examples
This example demonstrates how to download the binary content of a file from Google Drive directly into local memory.Chilkat Rust Downloads
let _ = true;
// It requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// This example uses a previously obtained access token having permission for the
// Google Drive scope.
let g_auth = chilkat::AuthGoogle::new();
g_auth.set_access_token("GOOGLE-DRIVE-ACCESS-TOKEN");
let rest = chilkat::Rest::new();
// Connect using TLS.
// A single REST object, once connected, can be used for many Google Drive REST API calls.
// The auto-reconnect indicates that if the already-established HTTPS connection is closed,
// then it will be automatically re-established as needed.
let b_auto_reconnect = true;
let _ = rest.connect("www.googleapis.com", 443, true, b_auto_reconnect).is_ok();
// Provide the authentication credentials (i.e. the access token)
let _ = rest.set_auth_google(&g_auth);
// ------------------------------------------------------------------------------
// To download a file, we must know the file ID.
// In a previous example (see Build Local Metadata Cache
// we built a local cache to make it easy to lookup file IDs given a file path.
// Let's say we want to download "testFolder/abc/123/penguins.jpg".
// First we lookup the fileId in the cache. With the fileId, we can download the file.
let gd_cache = chilkat::Cache::new();
gd_cache.set_level(0);
gd_cache.add_root("C:/ckCache/googleDrive");
let Ok(file_id) = gd_cache.fetch_text("testFolder/abc/123/penguins.jpg") else {
println!("Filepath not found in cache.");
return;
};
// We need to send a GET request like this:
// GET https://www.googleapis.com/drive/v3/files/fileId?alt=media
// The fileId is part of the path.
let sb_path = chilkat::StringBuilder::new();
let _ = sb_path.append("/drive/v3/files/");
let _ = sb_path.append(&file_id);
let _ = rest.add_query_param("alt", "media");
// To download to memory, we'll send the request in one call, then receive
// the response header, and then the response body.
// If the response header indicates failure or an unexpected response, then the
// body is not the data we desire.
// First send the HTTP request.
if rest.send_req_no_body("GET", &sb_path.get_as_string().unwrap_or_default()).is_err() {
println!("{}", rest.last_error_text());
return;
}
// Get the response header.
let status_code = rest.read_response_header();
if status_code != 200 {
println!("response status code = {}", rest.response_status_code());
println!("response status text = {}", rest.response_status_text());
println!("response header: {}", rest.response_header());
// Read the response body (which should be error text or HTML)
if let Ok(err_response_body) = rest.read_resp_body_string() {
println!("response body: {}", err_response_body);
}
return;
}
// Read the response body, which should contain the file data.
jpg_bytes = rest.read_resp_body_binary();
if !rest.last_method_success() {
println!("{}", rest.last_error_text());
return;
}
// Save jpgBytes to a file so we can examine the file to verify that it worked.
let fac = chilkat::FileAccess::new();
let _ = fac.write_entire_file("qa_output/penguins_out.jpg", &jpg_bytes);
println!("File downloaded.");