Created
December 22, 2025 20:49
-
-
Save darko-mesaros/9d27070d46ab784904bdef1e2259357a to your computer and use it in GitHub Desktop.
Configuring the AWS SDK for Rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use tracing_subscriber::EnvFilter; | |
| use clap::Parser; | |
| use aws_config::{Region, meta::region::RegionProviderChain}; | |
| #[derive(Parser)] | |
| struct Opt { | |
| region: Option<String> | |
| } | |
| #[tokio::main] | |
| async fn main() { | |
| // Configure tracing: | |
| tracing_subscriber::fmt() | |
| .with_env_filter( | |
| EnvFilter::from_default_env() | |
| .add_directive(tracing::Level::INFO.into()) | |
| ) | |
| .with_writer(std::io::stderr) | |
| .with_ansi(true) | |
| .init(); | |
| // Parsing command line parameters | |
| let Opt { | |
| region | |
| } = Opt::parse(); | |
| // Configure the region provider chain | |
| let region_provider = RegionProviderChain::first_try(region.map(Region::new)) | |
| .or_default_provider() // Env -> Profile -> EC2 IMDSv2 | |
| .or_else(Region::new("us-east-1")); | |
| // Configure the SDK: | |
| let sdk_config = aws_config::from_env() // Uses the BehaviorVersion::latest | |
| .region(region_provider) | |
| .profile_name("personal") | |
| .endpoint_url("http://localhost:4566") // For LocalStack | |
| .load().await; | |
| // Configure the client | |
| let s3_client = aws_sdk_s3::Client::new(&sdk_config); | |
| // Get the current region used: | |
| tracing::info!("Region selected: {:?}", sdk_config.region().unwrap()); | |
| // Running any SDK call so the SDK would emit what credentials are used | |
| let buckets = s3_client.list_buckets().send().await.unwrap(); | |
| println!("Buckets: {:#?}", buckets.buckets()); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment