Skip to content

Commit

Permalink
Add S3 Example, rename s3preview => s3 (#413)
Browse files Browse the repository at this point in the history
* Add S3 Example

* Rename s3preview back to s3
  • Loading branch information
rcoh authored May 25, 2021
1 parent 64fe38c commit b18421d
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 75 deletions.
16 changes: 16 additions & 0 deletions aws/sdk/examples/s3-helloworld/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "s3-helloworld"
version = "0.1.0"
authors = ["Russell Cohen <rcoh@amazon.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
s3 = { package = "aws-sdk-s3", path = "../../build/aws-sdk/s3" }
smithy-http = { path = "../../build/aws-sdk/smithy-http" }
tokio = { version = "1", features = ["full"] }
tracing-subscriber = "0.2.18"

[profile.dev]
split-debuginfo = "unpacked"
40 changes: 40 additions & 0 deletions aws/sdk/examples/s3-helloworld/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use s3::Region;
use smithy_http::byte_stream::ByteStream;
use std::error::Error;
use std::path::Path;
use tracing_subscriber::fmt::format::FmtSpan;
use tracing_subscriber::fmt::SubscriberBuilder;

// Change these to your bucket & key
const BUCKET: &str = "demo-bucket";
const KEY: &str = "demo-object";

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
SubscriberBuilder::default()
.with_env_filter("info")
.with_span_events(FmtSpan::CLOSE)
.init();
let conf = s3::Config::builder()
.region(Region::new("us-east-2"))
.build();
let client = s3::Client::from_conf(conf);
let resp = client.list_buckets().send().await?;
for bucket in resp.buckets.unwrap_or_default() {
println!("bucket: {:?}", bucket.name.expect("buckets have names"))
}
let body = ByteStream::from_path(Path::new("Cargo.toml")).await?;
let resp = client
.put_object()
.bucket(BUCKET)
.key(KEY)
.body(body)
.send();
let resp = resp.await?;
println!("Upload success. Version: {:?}", resp.version_id);

let resp = client.get_object().bucket(BUCKET).key(KEY).send().await?;
let data = resp.body.collect().await?;
println!("data: {:?}", data.into_bytes());
Ok(())
}
Loading

0 comments on commit b18421d

Please sign in to comment.