Skip to content

Commit fb836a7

Browse files
committed
1 parent fe8b12b commit fb836a7

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

.cargo/config.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[target.wasm32-wasip2]
2-
runner = "wasmtime -Shttp"
2+
runner = "wasmtime run -Shttp --env AWS_ACCESS_KEY_ID --env AWS_SECRET_ACCESS_KEY --dir .::."

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ authors = [
7070
[workspace.dependencies]
7171
anyhow = "1"
7272
async-task = "4.7"
73+
aws-config = { version = "1.8.8", default-features = false }
74+
aws-sdk-s3 = { version = "1.108.0", default-features = false }
7375
aws-smithy-async = { version = "1.2.6", default-features = false }
7476
aws-smithy-types = { version = "1.3.3", default-features = false }
7577
aws-smithy-runtime-api = { version = "1.9.1", default-features = false }

aws/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@ aws-smithy-runtime-api = { workspace = true, features = ["client", "http-1x"] }
1818
http-body-util.workspace = true
1919
sync_wrapper = { workspace = true, features = ["futures"] }
2020
wstd.workspace = true
21+
22+
[dev-dependencies]
23+
aws-config.workspace = true
24+
aws-sdk-s3.workspace = true
25+
clap.workspace = true

aws/examples/s3.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
use anyhow::Result;
2+
use clap::{Parser, Subcommand};
3+
4+
use aws_config::{BehaviorVersion, Region};
5+
use aws_sdk_s3::Client;
6+
7+
#[derive(Debug, Parser)]
8+
#[command(version, about, long_about = None)]
9+
struct Opts {
10+
/// The AWS Region.
11+
#[arg(short, long)]
12+
region: String,
13+
/// The name of the bucket.
14+
#[arg(short, long)]
15+
bucket: String,
16+
17+
#[command(subcommand)]
18+
command: Option<Command>,
19+
}
20+
21+
#[derive(Subcommand, Debug)]
22+
enum Command {
23+
List,
24+
Get {
25+
key: String,
26+
#[arg(short, long)]
27+
out: Option<String>,
28+
},
29+
}
30+
31+
#[wstd::main]
32+
async fn main() -> Result<()> {
33+
let opts = Opts::parse();
34+
let config = aws_config::defaults(BehaviorVersion::latest())
35+
.region(Region::new(opts.region.clone()))
36+
.sleep_impl(wstd_aws::sleep_impl())
37+
.http_client(wstd_aws::http_client())
38+
.load()
39+
.await;
40+
41+
let client = Client::new(&config);
42+
43+
match opts.command.as_ref().unwrap_or(&Command::List) {
44+
Command::List => list(&opts, &client).await,
45+
Command::Get { key, out } => {
46+
let contents = get(&opts, &client, &key).await?;
47+
let output: &str = if let Some(out) = out {
48+
out.as_str()
49+
} else {
50+
key.as_str()
51+
};
52+
std::fs::write(output, contents)?;
53+
Ok(())
54+
}
55+
}
56+
}
57+
58+
async fn list(opts: &Opts, client: &Client) -> Result<()> {
59+
let mut listing = client
60+
.list_objects_v2()
61+
.bucket(opts.bucket.clone())
62+
.into_paginator()
63+
.send();
64+
65+
println!("key\tetag\tlast_modified\tstorage_class");
66+
while let Some(res) = listing.next().await {
67+
let object = res?;
68+
for item in object.contents() {
69+
println!(
70+
"{}\t{}\t{}\t{}",
71+
item.key().unwrap_or_default(),
72+
item.e_tag().unwrap_or_default(),
73+
item.last_modified()
74+
.map(|lm| format!("{lm}"))
75+
.unwrap_or_default(),
76+
item.storage_class()
77+
.map(|sc| format!("{sc}"))
78+
.unwrap_or_default(),
79+
);
80+
}
81+
}
82+
Ok(())
83+
}
84+
85+
async fn get(opts: &Opts, client: &Client, key: &str) -> Result<Vec<u8>> {
86+
let object = client
87+
.get_object()
88+
.bucket(opts.bucket.clone())
89+
.key(key)
90+
.send()
91+
.await?;
92+
let data = object.body.collect().await?;
93+
Ok(data.to_vec())
94+
}

0 commit comments

Comments
 (0)