Rust
Rust
Bunny Sign URL and then Download using Signed URL
See more Bunny CDN Examples
Shows how to sign a URL for BunnyCDN Token Authentication and then use it to download.Chilkat Rust Downloads
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let my_security_key = "e7ea8d73-8fa0-44ef-a2cc-91526f7df5ed".to_string();
let url = "https://test.b-cdn.net/sample-pdf-with-images.pdf".to_string();
// Extract the URL components.
let url_obj = chilkat::Url::new();
let _ = url_obj.parse_url(&url);
let mut url_scheme = "https".to_string();
if !url_obj.ssl() {
url_scheme = "http".to_string();
}
let url_host = url_obj.host();
let url_path = url_obj.path();
// Calculate an expiration time 1 hour from the current date/time.
let exp_time = chilkat::DateTime::new();
let _ = exp_time.set_from_current_system_time();
let _ = exp_time.add_seconds(3600);
let expires = exp_time.get_as_unix_time_str(false).unwrap_or_default();
println!("Expires = {}", expires);
// Create the string to hash
let sb_to_hash = chilkat::StringBuilder::new();
let _ = sb_to_hash.append(&my_security_key);
let _ = sb_to_hash.append(&url_path);
let _ = sb_to_hash.append(&expires);
// Base64Url encoding is the same as base64, except "-" is used instead of "+",
// "_" is used instead of "/", and no "=" padding is added.
let token = sb_to_hash.get_hash("sha256", "base64Url", "utf-8").unwrap_or_default();
let sb_signed_url = chilkat::StringBuilder::new();
let _ = sb_signed_url.append(&url_scheme);
let _ = sb_signed_url.append("://");
let _ = sb_signed_url.append(&url_host);
let _ = sb_signed_url.append(&url_path);
let _ = sb_signed_url.append("?token=");
let _ = sb_signed_url.append(&token);
let _ = sb_signed_url.append("&expires=");
let _ = sb_signed_url.append(&expires);
let signed_url = sb_signed_url.get_as_string().unwrap_or_default();
println!("Signed URL: {}", signed_url);
// Use the signed URL to download the file.
let http = chilkat::Http::new();
if http.download(&signed_url, "c:/aaworkarea/sample.pdf").is_err() {
println!("{}", http.last_error_text());
} else {
println!("Success.");
}