Skip to content

Commit

Permalink
Use environment for no_env example
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidSouther committed Nov 7, 2023
1 parent 50fe1a8 commit 17228b2
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 22 deletions.
Empty file.
Empty file.
44 changes: 26 additions & 18 deletions rust_dev_preview/examples/sdk-config/src/bin/no_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

#![allow(clippy::result_large_err)]

// File overview: This example shows how to create an SdkConfig without using the
// default AWS environment configuration.

use aws_config::meta::region::RegionProviderChain;
use aws_credential_types::provider::{ProvideCredentials, SharedCredentialsProvider};
use aws_sdk_s3::config::retry::RetryConfig;
use aws_sdk_s3::config::Credentials;
use aws_sdk_s3::{config::Region, meta::PKG_VERSION, Client, Error};
use clap::Parser;

// TODO(you): When running these examples, create or edit these files & update them with your values.
const STATIC_ACCESS_KEY_ID: &str = include_str!("./testing/NO_ENV_STATIC_ACCESS_KEY_ID");
const STATIC_SECRET_ACCESS_KEY: &str = include_str!("./testing/NO_ENV_STATIC_ACCESS_KEY_ID");

#[derive(Debug, Parser)]
struct Opt {
/// The AWS Region.
Expand All @@ -41,26 +39,34 @@ async fn show_num_buckets(client: &Client) -> Result<(), Error> {
Ok(())
}

// WARNING: This example is for demonstration purposes only. Your code should always
// load credentials in a secure fashion.
/// A Credentials provider that uses non-standard environment variables `MY_ID`
/// and `MY_KEY` instead of `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`. This
/// could alternatively load credentials from another secure source of environment
/// secrets, if traditional AWS SDK for Rust credentials providers do not suit
/// your use case.
///
/// WARNING: This example is for demonstration purposes only. Your code should always
/// load credentials in a secure fashion.
#[derive(Debug)]
struct StaticCredentials {
access_key_id: &'static str,
secret_access_key: &'static str,
access_key_id: String,
secret_access_key: String,
}

impl StaticCredentials {
pub fn new(access_key_id: &'static str, secret_access_key: &'static str) -> Self {
pub fn new() -> Self {
let access_key_id = std::env::var("MY_ID").expect("access key id");
let secret_access_key = std::env::var("MY_KEY").expect("secret access key");
Self {
access_key_id,
secret_access_key,
access_key_id: access_key_id.trim().to_string(),
secret_access_key: secret_access_key.trim().to_string(),
}
}

async fn load_credentials(&self) -> aws_credential_types::provider::Result {
Ok(Credentials::new(
self.access_key_id,
self.secret_access_key,
self.access_key_id.clone(),
self.secret_access_key.clone(),
None,
None,
"StaticCredentials",
Expand All @@ -87,6 +93,11 @@ impl ProvideCredentials for StaticCredentials {
/// If the environment variable is not set, defaults to **us-west-2**.
/// * `[-t TRIES]` - The number of times to (re)try the request.
/// * `[-v]` - Whether to display additional information.
///
/// # Environment
///
/// * MY_ID - an AWS IAM Access Key ID to use for this request.
/// * MY_KEY - an AWS IAM Secret Access Key to use for this request.
#[tokio::main]
async fn main() -> Result<(), Error> {
tracing_subscriber::fmt::init();
Expand Down Expand Up @@ -115,10 +126,7 @@ async fn main() -> Result<(), Error> {

let shared_config = aws_config::SdkConfig::builder()
.region(region)
.credentials_provider(SharedCredentialsProvider::new(StaticCredentials::new(
STATIC_ACCESS_KEY_ID,
STATIC_SECRET_ACCESS_KEY,
)))
.credentials_provider(SharedCredentialsProvider::new(StaticCredentials::new()))
// Set max attempts.
// If tries is 1, there are no retries.
.retry_config(RetryConfig::standard().with_max_attempts(tries))
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 17228b2

Please sign in to comment.