Skip to content

Commit

Permalink
Feature/105 implement funcitons (#106)
Browse files Browse the repository at this point in the history
* feat: added a dynamo client

* feat: updated s3 client

* feat: added a shared crate

* chore: added .gitignore

* feat: add a traits into the shared trait

* docs: document

* feat: updated s3 client

* feat: added a dynamo db client
  • Loading branch information
hitohata authored Nov 14, 2024
1 parent 0fddcbf commit 1cbd8df
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 15 deletions.
29 changes: 29 additions & 0 deletions crates/aws_clients/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion crates/aws_clients/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ edition = "2021"
tokio = { version = "1", features = ["sync"] }
aws-sdk-s3 = { version = "1", features = ["behavior-version-latest"] }
aws-config = { version = "1", features = ["behavior-version-latest"] }
time_file_name = { path = "../time_file_name" }
time_file_name = { path = "../time_file_name" }
shared = { path = "../shared" }
aws-sdk-dynamodb = "1.54.0"
28 changes: 28 additions & 0 deletions crates/aws_clients/src/dynamodb/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
pub mod client {
use shared::traits::GetFileListTrait;

pub struct DynamoDbClient {}

pub trait DynamoClientTrait : GetFileListTrait {}

impl GetFileListTrait for DynamoDbClient {
async fn get_years() -> Result<Vec<String>, String> {
Ok(vec![])
}

async fn get_month(years: usize) -> Result<Vec<String>, String> {

Check failure on line 13 in crates/aws_clients/src/dynamodb/mod.rs

View workflow job for this annotation

GitHub Actions / build a web API document

unused variable: `years`
todo!()
}

async fn get_days(year: usize, month: usize) -> Result<Vec<String>, String> {

Check failure on line 17 in crates/aws_clients/src/dynamodb/mod.rs

View workflow job for this annotation

GitHub Actions / build a web API document

unused variable: `year`

Check failure on line 17 in crates/aws_clients/src/dynamodb/mod.rs

View workflow job for this annotation

GitHub Actions / build a web API document

unused variable: `month`
todo!()
}

async fn get_objects(year: usize, month: usize, day: usize) -> Result<Vec<String>, String>{

Check failure on line 21 in crates/aws_clients/src/dynamodb/mod.rs

View workflow job for this annotation

GitHub Actions / build a web API document

unused variable: `year`

Check failure on line 21 in crates/aws_clients/src/dynamodb/mod.rs

View workflow job for this annotation

GitHub Actions / build a web API document

unused variable: `month`

Check failure on line 21 in crates/aws_clients/src/dynamodb/mod.rs

View workflow job for this annotation

GitHub Actions / build a web API document

unused variable: `day`
todo!()
}
}

impl DynamoClientTrait for DynamoDbClient {}

}
11 changes: 11 additions & 0 deletions crates/aws_clients/src/environment_values/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod clients {
use tokio::sync::OnceCell;

static S3_CLIENT: OnceCell<aws_sdk_s3::Client> = OnceCell::const_new();
static DYNAMODB_CLIENT: OnceCell<aws_sdk_dynamodb::Client> = OnceCell::const_new();

Check failure on line 18 in crates/aws_clients/src/environment_values/mod.rs

View workflow job for this annotation

GitHub Actions / build a web API document

static `DYNAMODB_CLIENT` is never used

/// The s3 client
pub async fn s3_client() -> &'static aws_sdk_s3::Client {
Expand All @@ -25,4 +26,14 @@ pub mod clients {
})
.await
}

/// The DynamoDB client
pub async fn dynamodb_client() -> &'static aws_sdk_dynamodb::Client {

Check failure on line 31 in crates/aws_clients/src/environment_values/mod.rs

View workflow job for this annotation

GitHub Actions / build a web API document

function `dynamodb_client` is never used
DYNAMODB_CLIENT
.get_or_init(|| async {
let config = aws_config::load_from_env().await;
aws_sdk_dynamodb::Client::new(&config)
})
.await
}
}
1 change: 1 addition & 0 deletions crates/aws_clients/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mod environment_values;
pub mod s3;
pub mod dynamodb;
28 changes: 18 additions & 10 deletions crates/aws_clients/src/s3/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
pub mod client {
use std::future::Future;
use crate::environment_values::clients::s3_client;
use crate::environment_values::lambda_environment_values::standard_bucked_name;
use aws_sdk_s3::operation::list_objects_v2::ListObjectsV2Output;
use aws_sdk_s3::presigning::PresigningConfig;
use std::time::Duration;
use shared::traits::GetFileListTrait;
use time_file_name::FilePath;

/// The expiring time for the s3 pre-signed URL
Expand All @@ -12,9 +14,15 @@ pub mod client {
/// The client for the standard bucket
pub struct StandardS3Client {}

impl StandardS3Client {
// return years
pub async fn get_years() -> Result<Vec<String>, String> {
pub trait StandardS3ClientTrait : GetFileListTrait {
fn generate_pre_signed_url_for_video(
date_time: &str,
extension: &str,
) -> impl Future<Output=Result<String, String>> + Send;
}

impl GetFileListTrait for StandardS3Client {
async fn get_years() -> Result<Vec<String>, String> {
let result = s3_client()
.await
.list_objects_v2()
Expand All @@ -31,8 +39,7 @@ pub mod client {
Ok(retrieve_prefixes(&output))
}

// return months
pub async fn get_month(years: usize) -> Result<Vec<String>, String> {
async fn get_month(years: usize) -> Result<Vec<String>, String> {
let result = s3_client()
.await
.list_objects_v2()
Expand All @@ -58,8 +65,7 @@ pub mod client {
Ok(months)
}

/// get days form bucket
pub async fn get_days(year: usize, month: usize) -> Result<Vec<String>, String> {
async fn get_days(year: usize, month: usize) -> Result<Vec<String>, String> {
let result = s3_client()
.await
.list_objects_v2()
Expand All @@ -85,8 +91,7 @@ pub mod client {
Ok(days)
}

/// get objects
pub async fn get_objects(
async fn get_objects(
year: usize,
month: usize,
day: usize,
Expand Down Expand Up @@ -122,10 +127,13 @@ pub mod client {
Ok(objects)
}

}

impl StandardS3ClientTrait for StandardS3Client {
/// get a date time as an argument and return the [s3 pre-signed URL](https://docs.aws.amazon.com/AmazonS3/latest/userguide/ShareObjectPreSignedURL.html)
/// The expiring time is 3600 sec
/// The date time in the argument must be ISO
pub async fn generate_pre_signed_url_for_video(
async fn generate_pre_signed_url_for_video(
date_time: &str,
extension: &str,
) -> Result<String, String> {
Expand Down
2 changes: 2 additions & 0 deletions crates/shared/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
target
7 changes: 7 additions & 0 deletions crates/shared/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions crates/shared/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "shared"
version = "0.1.0"
edition = "2021"

[dependencies]
26 changes: 26 additions & 0 deletions crates/shared/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//! This is a shared crate.
//! This crate contains general things that are used in the whole system
pub mod traits {
use std::future::Future;

/// The searching is shared in the DB and bucket
/// This trait defines basic access patterns
pub trait GetFileListTrait {
/// get years list
fn get_years() -> impl Future<Output = Result<Vec<String>, String>> + Send;
/// get months list
fn get_month(years: usize) -> impl Future<Output = Result<Vec<String>, String>> + Send;
/// get days list
fn get_days(
year: usize,
month: usize,
) -> impl Future<Output = Result<Vec<String>, String>> + Send;
/// get objects list
fn get_objects(
year: usize,
month: usize,
day: usize,
) -> impl Future<Output = Result<Vec<String>, String>> + Send;
}
}
37 changes: 33 additions & 4 deletions lambdas/web-api-app/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1cbd8df

Please sign in to comment.