|
| 1 | +//! Example s3 client running on `wstd` via `wstd_aws` |
| 2 | +//! |
| 3 | +//! This example is a wasi cli command. It accepts command line arguments |
| 4 | +//! with the subcommand `list` to list a bucket's contents, and `get <key>` |
| 5 | +//! to get an object from a bucket and write it to the filesystem. |
| 6 | +//! |
| 7 | +//! This example *must be compiled in release mode* - in debug mode, the aws |
| 8 | +//! sdk's generated code will overflow the maximum permitted wasm locals in |
| 9 | +//! a single function. |
| 10 | +//! |
| 11 | +//! Compile it with: |
| 12 | +//! |
| 13 | +//! ```sh |
| 14 | +//! cargo build -p wstd-aws --target wasm32-wasip2 --release --examples |
| 15 | +//! ``` |
| 16 | +//! |
| 17 | +//! When running this example, you will need AWS credentials provided in environment |
| 18 | +//! variables. |
| 19 | +//! |
| 20 | +//! Run it with: |
| 21 | +//! ```sh |
| 22 | +//! wasmtime run -Shttp \ |
| 23 | +//! --env AWS_ACCESS_KEY_ID \ |
| 24 | +//! --env AWS_SECRET_ACCESS_KEY \ |
| 25 | +//! --env AWS_SESSION_TOKEN \ |
| 26 | +//! --dir .::. \ |
| 27 | +//! target/wasm22-wasip2/release/examples/s3.wasm |
| 28 | +//! ``` |
| 29 | +//! |
| 30 | +//! or alternatively run it with: |
| 31 | +//! ```sh |
| 32 | +//! cargo run --target wasm32-wasip2 -p wstd-aws --example s3 |
| 33 | +//! ``` |
| 34 | +//! |
| 35 | +//! which uses the wasmtime cli, as above, via configiration found in this |
| 36 | +//! workspace's `.cargo/config`. |
| 37 | +//! |
| 38 | +//! By default, this script accesses the `wstd-example-bucket` in `us-west-2`. |
| 39 | +//! To change the bucket or region, use the `--bucket` and `--region` cli |
| 40 | +//! flags before the subcommand. |
| 41 | +
|
| 42 | +use anyhow::Result; |
| 43 | +use clap::{Parser, Subcommand}; |
| 44 | + |
| 45 | +use aws_config::{BehaviorVersion, Region}; |
| 46 | +use aws_sdk_s3::Client; |
| 47 | + |
| 48 | +#[derive(Debug, Parser)] |
| 49 | +#[command(version, about, long_about = None)] |
| 50 | +struct Opts { |
| 51 | + /// The AWS Region. Defaults to us-west-2 if not provided. |
| 52 | + #[arg(short, long)] |
| 53 | + region: Option<String>, |
| 54 | + /// The name of the bucket. Defaults to wstd-example-bucket if not |
| 55 | + /// provided. |
| 56 | + #[arg(short, long)] |
| 57 | + bucket: Option<String>, |
| 58 | + |
| 59 | + #[command(subcommand)] |
| 60 | + command: Option<Command>, |
| 61 | +} |
| 62 | + |
| 63 | +#[derive(Subcommand, Debug)] |
| 64 | +enum Command { |
| 65 | + List, |
| 66 | + Get { |
| 67 | + key: String, |
| 68 | + #[arg(short, long)] |
| 69 | + out: Option<String>, |
| 70 | + }, |
| 71 | +} |
| 72 | + |
| 73 | +#[wstd::main] |
| 74 | +async fn main() -> Result<()> { |
| 75 | + let opts = Opts::parse(); |
| 76 | + let region = opts |
| 77 | + .region |
| 78 | + .clone() |
| 79 | + .unwrap_or_else(|| "us-west-2".to_owned()); |
| 80 | + let bucket = opts |
| 81 | + .bucket |
| 82 | + .clone() |
| 83 | + .unwrap_or_else(|| "wstd-example-bucket".to_owned()); |
| 84 | + |
| 85 | + let config = aws_config::defaults(BehaviorVersion::latest()) |
| 86 | + .region(Region::new(region)) |
| 87 | + .sleep_impl(wstd_aws::sleep_impl()) |
| 88 | + .http_client(wstd_aws::http_client()) |
| 89 | + .load() |
| 90 | + .await; |
| 91 | + |
| 92 | + let client = Client::new(&config); |
| 93 | + |
| 94 | + match opts.command.as_ref().unwrap_or(&Command::List) { |
| 95 | + Command::List => { |
| 96 | + let output = list(&bucket, &client).await?; |
| 97 | + print!("{}", output); |
| 98 | + } |
| 99 | + Command::Get { key, out } => { |
| 100 | + let contents = get(&bucket, &client, key).await?; |
| 101 | + let output: &str = if let Some(out) = out { |
| 102 | + out.as_str() |
| 103 | + } else { |
| 104 | + key.as_str() |
| 105 | + }; |
| 106 | + std::fs::write(output, contents)?; |
| 107 | + } |
| 108 | + } |
| 109 | + Ok(()) |
| 110 | +} |
| 111 | + |
| 112 | +async fn list(bucket: &str, client: &Client) -> Result<String> { |
| 113 | + let mut listing = client |
| 114 | + .list_objects_v2() |
| 115 | + .bucket(bucket.to_owned()) |
| 116 | + .into_paginator() |
| 117 | + .send(); |
| 118 | + |
| 119 | + let mut output = String::new(); |
| 120 | + output += "key\tetag\tlast_modified\tstorage_class\n"; |
| 121 | + while let Some(res) = listing.next().await { |
| 122 | + let object = res?; |
| 123 | + for item in object.contents() { |
| 124 | + output += &format!( |
| 125 | + "{}\t{}\t{}\t{}\n", |
| 126 | + item.key().unwrap_or_default(), |
| 127 | + item.e_tag().unwrap_or_default(), |
| 128 | + item.last_modified() |
| 129 | + .map(|lm| format!("{lm}")) |
| 130 | + .unwrap_or_default(), |
| 131 | + item.storage_class() |
| 132 | + .map(|sc| format!("{sc}")) |
| 133 | + .unwrap_or_default(), |
| 134 | + ); |
| 135 | + } |
| 136 | + } |
| 137 | + Ok(output) |
| 138 | +} |
| 139 | + |
| 140 | +async fn get(bucket: &str, client: &Client, key: &str) -> Result<Vec<u8>> { |
| 141 | + let object = client |
| 142 | + .get_object() |
| 143 | + .bucket(bucket.to_owned()) |
| 144 | + .key(key) |
| 145 | + .send() |
| 146 | + .await?; |
| 147 | + let data = object.body.collect().await?; |
| 148 | + Ok(data.to_vec()) |
| 149 | +} |
0 commit comments