-
Notifications
You must be signed in to change notification settings - Fork 244
/
use-localstack.rs
77 lines (62 loc) · 2.29 KB
/
use-localstack.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
// snippet-start:[localstack.rust.use-localstack]
use aws_smithy_http::endpoint::Endpoint;
use http::Uri;
use std::error::Error;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
tracing_subscriber::fmt::init();
let shared_config = aws_config::from_env().load().await;
let sqs_client = sqs_client(&shared_config);
let s3_client = s3_client(&shared_config);
let resp = s3_client.list_buckets().send().await?;
let buckets = resp.buckets().unwrap_or_default();
let num_buckets = buckets.len();
println!("Buckets:");
for bucket in buckets {
println!(" {}", bucket.name().as_deref().unwrap_or_default());
}
println!();
println!("Found {} buckets.", num_buckets);
println!();
let repl = sqs_client.list_queues().send().await?;
let queues = repl.queue_urls().unwrap_or_default();
let num_queues = queues.len();
println!("Queue URLs:");
for queue in queues {
println!(" {}", queue);
}
println!();
println!("Found {} queues.", num_queues);
println!();
if use_localstack() {
println!("Using the local stack.");
}
Ok(())
}
/// If LOCALSTACK environment variable is true, use LocalStack endpoints.
/// You can use your own method for determining whether to use LocalStack endpoints.
fn use_localstack() -> bool {
std::env::var("LOCALSTACK").unwrap_or_default() == "true"
}
fn localstack_endpoint() -> Endpoint {
Endpoint::immutable(Uri::from_static("http://localhost:4566/"))
}
fn sqs_client(conf: &aws_types::SdkConfig) -> aws_sdk_sqs::Client {
let mut sqs_config_builder = aws_sdk_sqs::config::Builder::from(conf);
if use_localstack() {
sqs_config_builder = sqs_config_builder.endpoint_resolver(localstack_endpoint())
}
aws_sdk_sqs::Client::from_conf(sqs_config_builder.build())
}
fn s3_client(conf: &aws_types::SdkConfig) -> aws_sdk_s3::Client {
let mut s3_config_builder = aws_sdk_s3::config::Builder::from(conf);
if use_localstack() {
s3_config_builder = s3_config_builder.endpoint_resolver(localstack_endpoint());
}
aws_sdk_s3::Client::from_conf(s3_config_builder.build())
}
// snippet-end:[localstack.rust.use-localstack]