Rust
Rust
Upload Binary File from Memory
See more Google Drive Examples
This example demonstrates how to upload a binary file from memory. It uploads the file to a subdirectory of our choosing.Chilkat Rust Downloads
let _ = true;
// This example 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.
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);
// -------------------------------------------------------------------------
// A multipart upload to Google Drive needs a multipart/related Content-Type
let _ = rest.add_header("Content-Type", "multipart/related");
// Specify each part of the request.
// The 1st part is JSON with information about the file.
rest.set_part_selector("1");
let _ = rest.add_header("Content-Type", "application/json; charset=UTF-8");
// Construct the JSON that will contain the metadata about the file data to be uploaded...
let json = chilkat::JsonObject::new();
let _ = json.append_string("name", "hedgehogs.jpg");
let _ = json.append_string("description", "A cute photo of two hedgehogs.");
let _ = json.append_string("mimeType", "image/jpeg");
// To place the file in a folder, we must add a parents[] array to the JSON
// and add the folder 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 find the file ID for the folder "testFolder/abc/123"
let gd_cache = chilkat::Cache::new();
gd_cache.set_level(0);
gd_cache.add_root("C:/ckCache/googleDrive");
let Ok(folder_id) = gd_cache.fetch_text("testFolder/abc/123") else {
println!("Filepath not found in cache.");
return;
};
let parents = chilkat::JsonArray::new();
let _ = json.append_array2("parents", &parents);
let _ = parents.add_string_at(-1, &folder_id);
let _ = rest.set_multipart_body_string(&json.emit().unwrap_or_default());
// The 2nd part is the file content, which will contain the binary image data.
rest.set_part_selector("2");
let _ = rest.add_header("Content-Type", "image/jpeg");
let fac = chilkat::FileAccess::new();
// Read a JPG file from the local filesystem.
jpg_bytes = fac.read_entire_file("qa_data/jpg/hedgehogs.jpg");
// Add the bytes to our upload
let _ = rest.set_multipart_body_binary(&jpg_bytes);
let Ok(json_response) = rest.full_request_multipart("POST", "/upload/drive/v3/files?uploadType=multipart") else {
println!("{}", rest.last_error_text());
return;
};
// A successful response will have a status code equal to 200.
if rest.response_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());
println!("response JSON: {}", json_response);
return;
}
// Show the JSON response.
let _ = json.load(&json_response);
// Show the full JSON response.
json.set_emit_compact(false);
println!("{}", json.emit().unwrap_or_default());
// A successful response looks like this:
// {
// "kind": "drive#file",
// "id": "0B53Q6OSTWYoldmJ0Z3ZqT2x5MFk",
// "name": "hedgehogs.jpg",
// "mimeType": "image/jpeg"
// }
// Get the fileId:
println!("fileId: {}", json.string_of("id").unwrap_or_default());