Skip to content

Commit 142090f

Browse files
committed
shared s3 client - does not work
1 parent 0f6e2a2 commit 142090f

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

examples/basic-s3-test/Cargo.toml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[package]
2+
name = "basic-s3-test"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# Starting in Rust 1.62 you can use `cargo add` to add dependencies
7+
# to your project.
8+
#
9+
# If you're using an older Rust version,
10+
# download cargo-edit(https://github.com/killercup/cargo-edit#installation)
11+
# to install the `add` subcommand.
12+
#
13+
# Running `cargo add DEPENDENCY_NAME` will
14+
# add the latest version of a dependency to the list,
15+
# and it will keep the alphabetic ordering for you.
16+
17+
[dependencies]
18+
aws_lambda_events = "0.7.2"
19+
json = "0.12.4"
20+
lambda_runtime = { path = "../../lambda-runtime" }
21+
serde = "1.0.136"
22+
serde_json = "1.0.93"
23+
tokio = { version = "1", features = ["macros"] }
24+
tracing = { version = "0.1", features = ["log"] }
25+
tracing-subscriber = { version = "0.3", default-features = false, features = ["ansi", "fmt"] }
26+
aws-config = "0.54.1"
27+
aws-sdk-s3 = "0.24.0"
28+
thumbnailer = "0.4.0"
29+
mime = "0.3.16"
30+
async-global-executor = "2.3.1"
31+
aws-smithy-http = "0.54.4"

examples/basic-s3-test/src/main.rs

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
2+
use aws_config::meta::region::RegionProviderChain;
3+
use aws_lambda_events::{event::s3::S3Event};
4+
use aws_sdk_s3::{types::ByteStream, Client};
5+
use aws_smithy_http::body::SdkBody;
6+
use lambda_runtime::{run, service_fn, Error, LambdaEvent};
7+
use serde::Serialize;
8+
9+
#[derive(Serialize)]
10+
struct Response {
11+
req_id: String,
12+
msg: String,
13+
}
14+
15+
struct SharedClient {
16+
client: &'static Client,
17+
}
18+
19+
impl SharedClient {
20+
fn new(client: &'static Client) -> Self {
21+
Self { client }
22+
}
23+
24+
pub async fn put_file_x(&self) -> Response {
25+
let stream = ByteStream::new(SdkBody::from("hello"));
26+
let _ = self.put_file("bucket", "key", stream).await;
27+
28+
Response { req_id: "1".to_string(), msg: "msg".to_string()}
29+
}
30+
31+
pub async fn put_file(&self, bucket: &str, key: &str, bytes: ByteStream) {
32+
let _ = self
33+
.client
34+
.put_object()
35+
.bucket(bucket)
36+
.key(key)
37+
.body(bytes)
38+
.send()
39+
.await;
40+
}
41+
}
42+
43+
async fn get_client() -> Client {
44+
let region_provider = RegionProviderChain::default_provider().or_else("us-east-2");
45+
let config = aws_config::from_env().region(region_provider).load().await;
46+
let client = Client::new(&config);
47+
48+
return client;
49+
}
50+
51+
#[tokio::main]
52+
async fn main() -> Result<(), Error> {
53+
let s3_client = get_client().await;
54+
let client = move || &SharedClient::new(&s3_client);
55+
56+
let client_ref = client();
57+
58+
run(service_fn(
59+
move |_event: LambdaEvent<S3Event>| async move {
60+
Ok::<Response, Error>(client_ref.put_file_x().await)
61+
}
62+
))
63+
.await?;
64+
Ok(())
65+
}
66+

0 commit comments

Comments
 (0)