use azure_core::{error::ErrorKind, http::Url};
use azure_identity::AzureCliCredential;
use azure_storage_blob::{models::StorageErrorCode, BlobServiceClient, StorageError};
use std::env;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let account = env::var("AZURE_STORAGE_ACCOUNT_NAME")
.expect("Set AZURE_STORAGE_ACCOUNT_NAME environment variable");
let service_url = Url::parse(&format!("https://{account}.blob.core.windows.net/"))?;
let container_name = "nonexistent-container";
let blob_name = "nonexistent-blob.txt";
println!("Authenticating with Azure CLI...");
let credential = AzureCliCredential::new(None)?;
let service_client = BlobServiceClient::new(service_url, Some(credential), None)?;
let blob_client = service_client.blob_client(container_name, blob_name);
println!("Attempting to download a blob that doesn't exist...");
let result = blob_client.download(None).await;
match result {
Ok(_) => {
println!("Blob downloaded successfully (unexpected)");
}
Err(error) => {
if matches!(error.kind(), ErrorKind::HttpResponse { .. }) {
let storage_error: StorageError = error.try_into()?;
println!("\n=== StorageError (Display) ===");
println!("{storage_error}");
println!("\n=== Programmatic Access ===");
println!("HTTP Status Code: {}", storage_error.status_code);
if let Some(error_code) = &storage_error.error_code {
match error_code {
StorageErrorCode::BlobNotFound => {
println!("The blob does not exist.");
}
StorageErrorCode::ContainerNotFound => {
println!("The container does not exist.");
}
StorageErrorCode::AuthorizationFailure => {
println!("Authorization failed. Check your permissions.");
}
StorageErrorCode::AuthenticationFailed => {
println!("Authentication failed. Verify your credentials.");
}
_ => {
println!("Other error: {error_code}");
}
}
}
if let Some(request_id) = &storage_error.request_id {
println!("Request ID: {request_id}");
}
} else {
println!("Non-HTTP error occurred: {:?}", error);
}
}
}
Ok(())
}