-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added DynamoDB PutItem code example * Added DynamoDB CreateTable, PutItem, and DeleteItem code examples * Added DynamoDB ListTables, ListItems, and DeleteTable code examples * Updated DynamoDB CreateTable code example * Updated DynamoDB code examples after running clippy * Updated DynamoDB code examples with latest changes * Updated DynamoDB code examples Co-authored-by: Russell Cohen <[email protected]>
- Loading branch information
Showing
12 changed files
with
625 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "dynamodb-put-item" | ||
version = "0.1.0" | ||
authors = ["Doug Schwartz <[email protected]>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
dynamodb = { package = "aws-sdk-dynamodb", path = "../../build/aws-sdk/dynamodb" } | ||
tokio = { version = "1", features = ["full"] } | ||
structopt = { version = "0.3", default-features = false } | ||
tracing-subscriber = { version = "0.2.16", features = ["fmt"] } | ||
aws-types = { path = "../../build/aws-sdk/aws-types" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
|
||
use std::process; | ||
|
||
use dynamodb::model::AttributeValue; | ||
use dynamodb::{Client, Config, Region}; | ||
|
||
use aws_types::region::{EnvironmentProvider, ProvideRegion}; | ||
|
||
use structopt::StructOpt; | ||
use tracing_subscriber::fmt::format::FmtSpan; | ||
use tracing_subscriber::fmt::SubscriberBuilder; | ||
|
||
#[derive(Debug, StructOpt)] | ||
struct Opt { | ||
/// The permission type of the user, standard_user or admin | ||
#[structopt(short, long)] | ||
p_type: String, | ||
|
||
/// The user's age | ||
#[structopt(short, long)] | ||
age: String, | ||
|
||
/// The user's username | ||
#[structopt(short, long)] | ||
username: String, | ||
|
||
/// The user's first name | ||
#[structopt(short, long)] | ||
first: String, | ||
|
||
/// The user's last name | ||
#[structopt(short, long)] | ||
last: String, | ||
|
||
/// The table name | ||
#[structopt(short, long)] | ||
table: String, | ||
|
||
/// The region | ||
#[structopt(short, long)] | ||
region: Option<String>, | ||
|
||
/// Activate verbose mode | ||
#[structopt(short, long)] | ||
verbose: bool, | ||
} | ||
|
||
/// Adds an item to an Amazon DynamoDB table. | ||
/// The table schema must use one of username, p_type, age, first, or last as the primary key. | ||
/// # Arguments | ||
/// | ||
/// * `-t TABLE` - The name of the table. | ||
/// * `-u USERNAME` - The username of the new table item. | ||
/// * `-p PERMISSION-TYPE` - The type of user, either "standard_user" or "admin". | ||
/// * `-a AGE` - The age of the user. | ||
/// * `-f FIRST` - The first name of the user. | ||
/// * `-l LAST` - The last name of the user. | ||
/// * `[-r REGION]` - The region in which the table is created. | ||
/// If not supplied, uses the value of the **AWS_DEFAULT_REGION** environment variable. | ||
/// If the environment variable is not set, defaults to **us-west-2**. | ||
/// * `[-v]` - Whether to display additional information. | ||
#[tokio::main] | ||
async fn main() { | ||
let Opt { | ||
table, | ||
username, | ||
p_type, | ||
age, | ||
first, | ||
last, | ||
region, | ||
verbose, | ||
} = Opt::from_args(); | ||
|
||
if p_type != "standard_user" && p_type != "admin" { | ||
println!("\n{} is not a valid permission type", p_type); | ||
println!("You must specify a permission type value of 'admin' or 'standard_user':"); | ||
println!("-p PERMISSION-TYPE\n"); | ||
process::exit(1); | ||
} | ||
|
||
let region = EnvironmentProvider::new() | ||
.region() | ||
.or_else(|| region.as_ref().map(|region| Region::new(region.clone()))) | ||
.unwrap_or_else(|| Region::new("us-west-2")); | ||
|
||
if verbose { | ||
println!("DynamoDB client version: {}\n", dynamodb::PKG_VERSION); | ||
println!("Region: {:?}", ®ion); | ||
println!("Table: {}", table); | ||
println!("User: {}", username); | ||
println!("Type: {}", p_type); | ||
println!("Age: {}", age); | ||
println!("First: {}", first); | ||
println!("Last: {}\n", last); | ||
|
||
SubscriberBuilder::default() | ||
.with_env_filter("info") | ||
.with_span_events(FmtSpan::CLOSE) | ||
.init(); | ||
} | ||
|
||
let config = Config::builder().region(region).build(); | ||
|
||
let client = Client::from_conf(config); | ||
|
||
let user_av = AttributeValue::S(String::from(&username)); | ||
let type_av = AttributeValue::S(String::from(&p_type)); | ||
let age_av = AttributeValue::S(String::from(&age)); | ||
let first_av = AttributeValue::S(String::from(&first)); | ||
let last_av = AttributeValue::S(String::from(&last)); | ||
|
||
match client | ||
.put_item() | ||
.table_name(table) | ||
.item("username", user_av) | ||
.item("account_type", type_av) | ||
.item("age", age_av) | ||
.item("first_name", first_av) | ||
.item("last_name", last_av) | ||
.send() | ||
.await | ||
{ | ||
Ok(_) => println!( | ||
"Added user {}, {} {}, age {} as {} user", | ||
username, first, last, age, p_type | ||
), | ||
Err(e) => { | ||
println!("Got an error adding item:"); | ||
println!("{}", e); | ||
process::exit(1); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "dynamodb-create-table" | ||
version = "0.1.0" | ||
authors = ["Doug Schwartz <[email protected]>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
dynamodb = { package = "aws-sdk-dynamodb", path = "../../build/aws-sdk/dynamodb" } | ||
tokio = { version = "1", features = ["full"] } | ||
structopt = { version = "0.3", default-features = false } | ||
tracing-subscriber = { version = "0.2.16", features = ["fmt"] } | ||
aws-types = { path = "../../build/aws-sdk/aws-types" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
|
||
use std::process; | ||
|
||
use dynamodb::model::{ | ||
AttributeDefinition, KeySchemaElement, KeyType, ProvisionedThroughput, ScalarAttributeType, | ||
}; | ||
use dynamodb::{Client, Config, Region}; | ||
|
||
use aws_types::region::{EnvironmentProvider, ProvideRegion}; | ||
|
||
use structopt::StructOpt; | ||
use tracing_subscriber::fmt::format::FmtSpan; | ||
use tracing_subscriber::fmt::SubscriberBuilder; | ||
|
||
#[derive(Debug, StructOpt)] | ||
struct Opt { | ||
/// The region | ||
#[structopt(short, long)] | ||
region: Option<String>, | ||
|
||
/// The table name | ||
#[structopt(short, long)] | ||
table: String, | ||
|
||
/// The region | ||
#[structopt(short, long)] | ||
key: String, | ||
|
||
/// Activate verbose mode | ||
#[structopt(short, long)] | ||
verbose: bool, | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let Opt { | ||
table, | ||
key, | ||
region, | ||
verbose, | ||
} = Opt::from_args(); | ||
|
||
let region = EnvironmentProvider::new() | ||
.region() | ||
.or_else(|| region.as_ref().map(|region| Region::new(region.clone()))) | ||
.unwrap_or_else(|| Region::new("us-west-2")); | ||
|
||
if verbose { | ||
println!("DynamoDB client version: {}\n", dynamodb::PKG_VERSION); | ||
println!("Region: {:?}", ®ion); | ||
println!("Table: {}", table); | ||
println!("Key: {}\n", key); | ||
|
||
SubscriberBuilder::default() | ||
.with_env_filter("info") | ||
.with_span_events(FmtSpan::CLOSE) | ||
.init(); | ||
} | ||
|
||
let config = Config::builder().region(region).build(); | ||
let client = Client::from_conf(config); | ||
|
||
let ad = AttributeDefinition::builder() | ||
.attribute_name(String::from(&key)) | ||
.attribute_type(ScalarAttributeType::S) | ||
.build(); | ||
|
||
let ks = KeySchemaElement::builder() | ||
.attribute_name(String::from(&key)) | ||
.key_type(KeyType::Hash) | ||
.build(); | ||
|
||
let pt = ProvisionedThroughput::builder() | ||
.read_capacity_units(10) | ||
.write_capacity_units(5) | ||
.build(); | ||
|
||
match client | ||
.create_table() | ||
.table_name(String::from(&table)) | ||
.key_schema(ks) | ||
.attribute_definitions(ad) | ||
.provisioned_throughput(pt) | ||
.send() | ||
.await | ||
{ | ||
Ok(_) => println!("Added table {} with key {}", table, key), | ||
Err(e) => { | ||
println!("Got an error creating table:"); | ||
println!("{}", e); | ||
process::exit(1); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "dynamodb-delete-item" | ||
version = "0.1.0" | ||
authors = ["Doug Schwartz <[email protected]>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
dynamodb = { package = "aws-sdk-dynamodb", path = "../../build/aws-sdk/dynamodb" } | ||
tokio = { version = "1", features = ["full"] } | ||
structopt = { version = "0.3", default-features = false } | ||
tracing-subscriber = { version = "0.2.16", features = ["fmt"] } | ||
aws-types = { path = "../../build/aws-sdk/aws-types" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
|
||
use std::process; | ||
|
||
use dynamodb::model::AttributeValue; | ||
use dynamodb::{Client, Config, Region}; | ||
|
||
use aws_types::region::{EnvironmentProvider, ProvideRegion}; | ||
|
||
use structopt::StructOpt; | ||
use tracing_subscriber::fmt::format::FmtSpan; | ||
use tracing_subscriber::fmt::SubscriberBuilder; | ||
|
||
#[derive(Debug, StructOpt)] | ||
struct Opt { | ||
/// The region | ||
#[structopt(short, long)] | ||
region: Option<String>, | ||
|
||
/// The table name | ||
#[structopt(short, long)] | ||
table: String, | ||
|
||
/// The key for the item in the table | ||
#[structopt(short, long)] | ||
key: String, | ||
|
||
/// The value of the item to delete from the table | ||
#[structopt(short, long)] | ||
value: String, | ||
|
||
/// Whether to display additional information | ||
#[structopt(short, long)] | ||
info: bool, | ||
} | ||
|
||
/// Deletes an item from an Amazon DynamoDB table. | ||
/// The table schema must use the key as the primary key. | ||
/// # Arguments | ||
/// | ||
/// * `-t TABLE` - The name of the table. | ||
/// * `-k KEY` - The table's primary key. | ||
/// * `-v VALUE` - The value of the item's primary key. | ||
/// * `[-r REGION]` - The region in which the table is created. | ||
/// If not supplied, uses the value of the **AWS_DEFAULT_REGION** environment variable. | ||
/// If the environment variable is not set, defaults to **us-west-2**. | ||
/// * `[-i]` - Whether to display additional information. | ||
#[tokio::main] | ||
async fn main() { | ||
let Opt { | ||
info, | ||
key, | ||
region, | ||
table, | ||
value, | ||
} = Opt::from_args(); | ||
|
||
let region = EnvironmentProvider::new() | ||
.region() | ||
.or_else(|| region.as_ref().map(|region| Region::new(region.clone()))) | ||
.unwrap_or_else(|| Region::new("us-west-2")); | ||
|
||
if info { | ||
println!("DynamoDB client version: {}", dynamodb::PKG_VERSION); | ||
println!("Region: {:?}", ®ion); | ||
println!("Table: {}", table); | ||
println!("Key: {}", key); | ||
|
||
SubscriberBuilder::default() | ||
.with_env_filter("info") | ||
.with_span_events(FmtSpan::CLOSE) | ||
.init(); | ||
} | ||
|
||
let config = Config::builder().region(region).build(); | ||
|
||
let client = Client::from_conf(config); | ||
|
||
match client | ||
.delete_item() | ||
.table_name(table) | ||
.key(key, AttributeValue::S(value)) | ||
.send() | ||
.await | ||
{ | ||
Ok(_) => println!("Deleted item from table"), | ||
Err(e) => { | ||
println!("Got an error deleting item from table:"); | ||
println!("{}", e); | ||
process::exit(1); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "dynamodb-delete-table" | ||
version = "0.1.0" | ||
authors = ["Doug Schwartz <[email protected]>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
dynamodb = { package = "aws-sdk-dynamodb", path = "../../build/aws-sdk/dynamodb" } | ||
tokio = { version = "1", features = ["full"] } | ||
structopt = { version = "0.3", default-features = false } | ||
tracing-subscriber = { version = "0.2.16", features = ["fmt"] } | ||
aws-types = { path = "../../build/aws-sdk/aws-types" } |
Oops, something went wrong.