Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created July 31, 2022 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/eb5dd7bc059ed10142d5c8674a6405b6 to your computer and use it in GitHub Desktop.
Save rust-play/eb5dd7bc059ed10142d5c8674a6405b6 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use aws_sdk_account::Credentials;
use aws_sdk_dynamodb::{Client, Endpoint, Error, Region, model::{Select, AttributeValue}, middleware::RetryConfig};
use http::Uri;
use tokio_stream::StreamExt;
#[macro_use]
extern crate lazy_static;
/// Lists your tables in DynamDB local.
#[tokio::main]
async fn main() -> Result<(), Error> {
// Select a profile by setting the `AWS_PROFILE` environment variable.
let dbaccesskey = "you-db-key";
let dbsecretaccesskey = "your-seccret-key";
let db_creds = aws_sdk_dynamodb::Credentials::new(dbaccesskey, dbsecretaccesskey, None, None, "AWS");
let mut dynamodb_local_config = aws_sdk_dynamodb::config::Builder::new()
// .retry_config(RetryConfig::new().with_max_attempts(2))
.credentials_provider(db_creds)
.region(Region::new("ap-south-1"));
dynamodb_local_config.set_sleep_impl(None);
let client = Client::from_conf(dynamodb_local_config.build());
let resp = (client).list_tables().send().await?;
println!(":Tables: ");
let names = resp.table_names().unwrap_or_default();
println!("------------------------------------------------");
// let _ = add_item(&client, "totp-auth").await;
// println!("------------------------------------------------");
// let _ = list_items(&client, "totp-auth").await;
// println!("------------------------------------------------");
let _ = list_items(&client, "totp-auth").await;
println!();
println!(":Found {} tables", names.len());
Ok(())
}
async fn list_items(client: &Client, table: &str) -> Result<(), Error> {
let items: Result<Vec<_>, _> = client
.scan()
.table_name(table)
.into_paginator()
.items()
.send()
.collect()
.await;
println!("Items in table:");
for item in items? {
println!(" {:?}", item);
}
Ok(())
}
async fn add_item( client: &Client, table: &str) -> Result<(), Error> {
let first_av = AttributeValue::S("josh@t.com".into());
let last_av = AttributeValue::S("8hj^d7sa*DG".into());
let request = client
.put_item()
.table_name(table)
.item("user_email", first_av)
.item("secret", last_av);
// println!("Executing request [{:?}] to add item...", request);
request.send().await?;
println!("Added user ");
Ok(())
}
async fn query_item(client: &Client) -> bool {
let value = "user@email.com".to_string();
let key = "user_email".to_string();
let user_av = AttributeValue::S(value.to_string());
match client
.query()
.table_name("table name".to_string())
.key_condition_expression("#key = :value".to_string())
.expression_attribute_names("#key".to_string(), key)
.expression_attribute_values(":value".to_string(), user_av)
.select(Select::AllAttributes)
.send()
.await
{
Ok(resp) => {
if resp.count > 0 {
let t = resp.items.unwrap();
for i in t.iter() {
println!(" {:?}", i);
}
true
} else {
println!("Did not find a match.");
false
}
}
Err(e) => {
println!("Got an error querying table:");
println!("{}", e);
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment