Skip to content

Commit

Permalink
[examples] Sync SDK examples from awsdocs/aws-doc-sdk-examples
Browse files Browse the repository at this point in the history
Includes commit(s):
  b7635b1ab88285a744547c4aa33b6ba8bf0a1c57
  9465b36910cdc1962dadfe53af2ce31098a7abd8
  a48be99cf3e1e920698bbbd4fe5c22c4f9bef279
  d73bd946755cb53a0984af323e6bc0817bf5c9f6
  a29f4e9d999254adf8b3594ad8904e781ab01f1b
  e68209ef191d825e7b19dd5b0ea84a980df54922
  8595338924aae24aa9815e26d8b536a231b52e78
  8d51b94f33b95667b76a2232762560304a28b6e5
  9f08f276537a38dc094727f1ad0b0ceacbb1dc7b

Co-authored-by: David Souther <[email protected]>
Co-authored-by: Eric Shepherd <[email protected]>
Co-authored-by: John DiSanti <[email protected]>
  • Loading branch information
4 people committed Nov 16, 2023
1 parent b6063d3 commit 6f2a0fa
Show file tree
Hide file tree
Showing 98 changed files with 923 additions and 319 deletions.
5 changes: 3 additions & 2 deletions examples/cross_service/detect_faces/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

use aws_config::meta::region::RegionProviderChain;
use aws_config::BehaviorVersion;
use aws_sdk_s3::config::Region;
use clap::Parser;
use std::error::Error;
Expand Down Expand Up @@ -205,13 +206,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
println!();
}

let s3_shared_config = aws_config::from_env()
let s3_shared_config = aws_config::defaults(BehaviorVersion::latest())
.region(s3_region_provider)
.load()
.await;
let s3_client = aws_sdk_s3::Client::new(&s3_shared_config);

let rek_shared_config = aws_config::from_env()
let rek_shared_config = aws_config::defaults(BehaviorVersion::latest())
.region(rek_region_provider)
.load()
.await;
Expand Down
7 changes: 4 additions & 3 deletions examples/cross_service/detect_labels/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
extern crate exif;

use aws_config::meta::region::RegionProviderChain;
use aws_config::BehaviorVersion;
use aws_sdk_dynamodb::config::Region;
use aws_sdk_dynamodb::types::AttributeValue;
use clap::Parser;
Expand Down Expand Up @@ -279,7 +280,7 @@ async fn main() -> Result<(), exif::Error> {
println!();
}

let s3_shared_config = aws_config::from_env()
let s3_shared_config = aws_config::defaults(BehaviorVersion::latest())
.region(s3_region_provider)
.load()
.await;
Expand All @@ -289,15 +290,15 @@ async fn main() -> Result<(), exif::Error> {

let edata = get_exif_data(&filename);

let rek_shared_config = aws_config::from_env()
let rek_shared_config = aws_config::defaults(BehaviorVersion::latest())
.region(rek_region_provider)
.load()
.await;
let rek_client = aws_sdk_rekognition::Client::new(&rek_shared_config);

let labels = get_label_data(&rek_client, &bucket, &filename).await;

let dynamo_shared_config = aws_config::from_env()
let dynamo_shared_config = aws_config::defaults(BehaviorVersion::latest())
.region(dynamo_region_provider)
.load()
.await;
Expand Down
4 changes: 2 additions & 2 deletions examples/cross_service/photo_asset_management/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use aws_config::SdkConfig;
use aws_config::{BehaviorVersion, SdkConfig};

#[cfg(not(debug_assertions))]
pub fn init_tracing_subscriber() {
Expand Down Expand Up @@ -62,7 +62,7 @@ impl Common {
}

pub async fn load_from_env() -> Self {
let sdk_config = aws_config::load_from_env().await;
let sdk_config = aws_config::load_defaults(BehaviorVersion::latest()).await;
// PAM environment is declared in the cdk, in lib/backend/lambdas.ts
let storage_bucket =
std::env::var("STORAGE_BUCKET_NAME").expect("storage bucket in environment");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ pub async fn handler(
#[cfg(test)]
mod test {
use super::prepare_update_expression;
use aws_config::SdkConfig;
use aws_config::{BehaviorVersion, SdkConfig};

#[tokio::test]
async fn test_prepare_update_statement() {
Expand All @@ -136,7 +136,11 @@ mod test {
.name("label")
.build();

let client = aws_sdk_dynamodb::Client::new(&SdkConfig::builder().build());
let client = aws_sdk_dynamodb::Client::new(
&SdkConfig::builder()
.behavior_version(BehaviorVersion::latest())
.build(),
);
let update = client.update_item();
let update = prepare_update_expression(update, &object, &label);

Expand Down
7 changes: 4 additions & 3 deletions examples/cross_service/photo_asset_management/src/uploader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ impl<'a> ZipUpload<'a> {

let mut byte_count = 0_usize;
while let Some(bytes) = object.body.try_next().await? {
let bytes = self.zip.write(&bytes)?;
byte_count += bytes;
tracing::trace!("Intermediate read of {bytes} (total {byte_count})");
let bytes_len = bytes.len();
self.zip.write_all(&bytes)?;
byte_count += bytes_len;
tracing::trace!("Intermediate read of {bytes_len} (total {byte_count})");
}

Ok(())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use aws_config::BehaviorVersion;
use aws_sdk_s3::Client;

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
let s3_client = Client::new(&aws_config::load_from_env().await);
let s3_client = Client::new(&aws_config::load_defaults(BehaviorVersion::latest()).await);
let part = 0;
let bucket: String = "bucket".to_string();
let key: String = "key".to_string();
Expand Down
3 changes: 2 additions & 1 deletion examples/cross_service/rest_ses/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//! Main that loads environments & prepares clients, and hands them to `startup`.
use std::net::TcpListener;

use aws_config::BehaviorVersion;
use rest_ses::client::{RdsClient, SesClient};
use rest_ses::configuration::{get_settings, init_environment};
use rest_ses::startup::run;
Expand All @@ -30,7 +31,7 @@ async fn main() -> std::io::Result<()> {
init_subscriber(subscriber);

// AWS Settings (Region & role) come from the environment.
let config = aws_config::from_env().load().await;
let config = aws_config::load_defaults(BehaviorVersion::latest()).await;
let rds = RdsClient::new(&settings.rds, &config);
let ses = SesClient::new(&settings.ses, &config);

Expand Down
6 changes: 4 additions & 2 deletions examples/cross_service/rest_ses/tests/api/startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* SPDX-License-Identifier: Apache-2.0.
*/

use aws_config::BehaviorVersion;
use once_cell::sync::Lazy;
use rest_ses::{
client::{RdsClient, SesClient},
Expand All @@ -27,14 +28,15 @@ static TRACING: Lazy<Environment> = Lazy::new(|| {
/// Spawn the app against a MockServer resolved backend.
pub async fn spawn_app_mocked() -> (String, MockServer) {
let mock_server = MockServer::builder().start().await;
let config_loader = aws_config::from_env().endpoint_url(mock_server.uri());
let config_loader =
aws_config::defaults(BehaviorVersion::latest()).endpoint_url(mock_server.uri());
let (app, _) = prep_app(config_loader).await;
(app, mock_server)
}

/// Spawn the app using production AWS credentials.
pub async fn spawn_app() -> (String, RdsClient) {
prep_app(aws_config::from_env()).await
prep_app(aws_config::defaults(BehaviorVersion::latest())).await
}

/// Prepare the application for testing.
Expand Down
7 changes: 4 additions & 3 deletions examples/cross_service/telephone/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use anyhow::{Context, Result};
use aws_config::meta::region::RegionProviderChain;
use aws_config::BehaviorVersion;
use aws_sdk_polly::config::Region;
use aws_sdk_polly::types::{OutputFormat, VoiceId};
use aws_sdk_transcribe::types::{LanguageCode, Media, MediaFormat, TranscriptionJobStatus};
Expand Down Expand Up @@ -269,15 +270,15 @@ async fn main() -> Result<(), anyhow::Error> {
}

// Create configurations for each service client.
let polly_shared_config = aws_config::from_env()
let polly_shared_config = aws_config::defaults(BehaviorVersion::latest())
.region(polly_region_provider)
.load()
.await;
let s3_shared_config = aws_config::from_env()
let s3_shared_config = aws_config::defaults(BehaviorVersion::latest())
.region(s3_region_provider)
.load()
.await;
let transcribe_shared_config = aws_config::from_env()
let transcribe_shared_config = aws_config::defaults(BehaviorVersion::latest())
.region(transcribe_region_provider)
.load()
.await;
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/apigateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ publish = false

[dependencies]
thiserror = "1.0"
aws-config= { version = "0.100.0", path = "../../../sdk/aws-config" }
aws-config= { version = "0.100.0", path = "../../../sdk/aws-config", features = ["behavior-version-latest"] }
aws-sdk-apigateway= { version = "0.37.0", path = "../../../sdk/apigateway" }
aws-smithy-types-convert= { version = "0.58.0", path = "../../../sdk/aws-smithy-types-convert", features = ["convert-chrono"] }

Expand Down
2 changes: 1 addition & 1 deletion examples/examples/apigatewaymanagement/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ publish = false

[dependencies]
http = "0.2.5"
aws-config= { version = "0.100.0", path = "../../../sdk/aws-config" }
aws-config= { version = "0.100.0", path = "../../../sdk/aws-config", features = ["behavior-version-latest"] }
aws-sdk-apigatewaymanagement= { version = "0.37.0", path = "../../../sdk/apigatewaymanagement" }

[dependencies.tokio]
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/applicationautoscaling/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
publish = false

[dependencies]
aws-config= { version = "0.100.0", path = "../../../sdk/aws-config" }
aws-config= { version = "0.100.0", path = "../../../sdk/aws-config", features = ["behavior-version-latest"] }
aws-sdk-applicationautoscaling= { version = "0.37.0", path = "../../../sdk/applicationautoscaling" }

[dependencies.tokio]
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/aurora/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ inquire = "0.6.2"
mockall = "0.11.4"
secrecy = "0.8.0"
tracing = "0.1.37"
aws-config= { version = "0.100.0", path = "../../../sdk/aws-config" }
aws-config= { version = "0.100.0", path = "../../../sdk/aws-config", features = ["behavior-version-latest"] }
aws-smithy-types= { version = "0.100.0", path = "../../../sdk/aws-smithy-types" }
aws-smithy-runtime-api= { version = "0.100.0", path = "../../../sdk/aws-smithy-runtime-api" }
aws-sdk-rds= { version = "0.37.0", path = "../../../sdk/rds" }
Expand Down
26 changes: 13 additions & 13 deletions examples/examples/aurora/src/aurora_scenario/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use aws_sdk_rds::{
OrderableDbInstanceOption,
},
};
use aws_smithy_runtime_api::client::orchestrator::HttpResponse;
use aws_smithy_runtime_api::http::{Response, StatusCode};
use aws_smithy_types::body::SdkBody;
use mockall::predicate::eq;
use secrecy::ExposeSecret;
Expand Down Expand Up @@ -103,7 +103,7 @@ async fn test_scenario_set_engine_param_group_exists() {
CreateDBClusterParameterGroupError::DbParameterGroupAlreadyExistsFault(
DbParameterGroupAlreadyExistsFault::builder().build(),
),
HttpResponse::new(SdkBody::empty()),
Response::new(StatusCode::try_from(400).unwrap(), SdkBody::empty()),
))
});

Expand Down Expand Up @@ -173,7 +173,7 @@ async fn test_scenario_get_engines_failed() {
ErrorKind::Other,
"describe_db_engine_versions error",
))),
HttpResponse::new(SdkBody::empty()),
Response::new(StatusCode::try_from(400).unwrap(), SdkBody::empty()),
))
});

Expand Down Expand Up @@ -244,7 +244,7 @@ async fn test_scenario_get_instance_classes_error() {
ErrorKind::Other,
"describe_orderable_db_instance_options_error",
))),
HttpResponse::new(SdkBody::empty()),
Response::new(StatusCode::try_from(400).unwrap(), SdkBody::empty()),
))
});

Expand Down Expand Up @@ -327,7 +327,7 @@ async fn test_scenario_get_cluster_error() {
ErrorKind::Other,
"describe_db_clusters_error",
))),
HttpResponse::new(SdkBody::empty()),
Response::new(StatusCode::try_from(400).unwrap(), SdkBody::empty()),
))
});

Expand Down Expand Up @@ -419,7 +419,7 @@ async fn test_scenario_cluster_parameters_error() {
ErrorKind::Other,
"describe_db_cluster_parameters_error",
))),
HttpResponse::new(SdkBody::empty()),
Response::new(StatusCode::try_from(400).unwrap(), SdkBody::empty()),
))
});

Expand Down Expand Up @@ -478,7 +478,7 @@ async fn test_scenario_update_auto_increment_error() {
ErrorKind::Other,
"modify_db_cluster_parameter_group_error",
))),
HttpResponse::new(SdkBody::empty()),
Response::new(StatusCode::try_from(400).unwrap(), SdkBody::empty()),
))
});

Expand Down Expand Up @@ -602,7 +602,7 @@ async fn test_start_cluster_and_instance_cluster_create_error() {
ErrorKind::Other,
"create db cluster error",
))),
HttpResponse::new(SdkBody::empty()),
Response::new(StatusCode::try_from(400).unwrap(), SdkBody::empty()),
))
});

Expand Down Expand Up @@ -667,7 +667,7 @@ async fn test_start_cluster_and_instance_instance_create_error() {
ErrorKind::Other,
"create db instance error",
))),
HttpResponse::new(SdkBody::empty()),
Response::new(StatusCode::try_from(400).unwrap(), SdkBody::empty()),
))
});

Expand Down Expand Up @@ -733,7 +733,7 @@ async fn test_start_cluster_and_instance_wait_hiccup() {
ErrorKind::Other,
"describe cluster error",
))),
HttpResponse::new(SdkBody::empty()),
Response::new(StatusCode::try_from(400).unwrap(), SdkBody::empty()),
))
})
.with(eq("RustSDKCodeExamplesDBCluster"))
Expand Down Expand Up @@ -892,7 +892,7 @@ async fn test_scenario_clean_up_errors() {
ErrorKind::Other,
"describe db instances error",
))),
HttpResponse::new(SdkBody::empty()),
Response::new(StatusCode::try_from(400).unwrap(), SdkBody::empty()),
))
});

Expand Down Expand Up @@ -923,7 +923,7 @@ async fn test_scenario_clean_up_errors() {
ErrorKind::Other,
"describe db clusters error",
))),
HttpResponse::new(SdkBody::empty()),
Response::new(StatusCode::try_from(400).unwrap(), SdkBody::empty()),
))
});

Expand Down Expand Up @@ -1000,7 +1000,7 @@ async fn test_scenario_snapshot_error() {
ErrorKind::Other,
"create snapshot error",
))),
HttpResponse::new(SdkBody::empty()),
Response::new(StatusCode::try_from(400).unwrap(), SdkBody::empty()),
))
});

Expand Down
39 changes: 39 additions & 0 deletions examples/examples/aurora/src/bin/hello-world.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use aws_sdk_rds::Client;

#[derive(Debug)]
struct Error(String);
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::error::Error for Error {}

#[tokio::main]
async fn main() -> Result<(), Error> {
tracing_subscriber::fmt::init();
let sdk_config = aws_config::from_env().load().await;
let client = Client::new(&sdk_config);

let describe_db_clusters_output = client
.describe_db_clusters()
.send()
.await
.map_err(|e| Error(e.to_string()))?;
println!(
"Found {} clusters:",
describe_db_clusters_output.db_clusters().len()
);
for cluster in describe_db_clusters_output.db_clusters() {
let name = cluster.database_name().unwrap_or("Unknown");
let engine = cluster.engine().unwrap_or("Unknown");
let id = cluster.db_cluster_identifier().unwrap_or("Unknown");
let class = cluster.db_cluster_instance_class().unwrap_or("Unknown");
println!("\tDatabase: {name}",);
println!("\t Engine: {engine}",);
println!("\t ID: {id}",);
println!("\tInstance: {class}",);
}

Ok(())
}
2 changes: 1 addition & 1 deletion examples/examples/auto-scaling/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ publish = false
anyhow = "1.0.75"
tracing = "0.1.37"
tokio-stream = "0.1.14"
aws-config= { version = "0.100.0", path = "../../../sdk/aws-config" }
aws-config= { version = "0.100.0", path = "../../../sdk/aws-config", features = ["behavior-version-latest"] }
aws-sdk-autoscaling= { version = "0.37.0", path = "../../../sdk/autoscaling" }
aws-sdk-ec2= { version = "0.37.0", path = "../../../sdk/ec2" }
aws-types= { version = "0.100.0", path = "../../../sdk/aws-types" }
Expand Down
Loading

0 comments on commit 6f2a0fa

Please sign in to comment.