zero4rs 2.0.0

zero4rs is a powerful, pragmatic, and extremely fast web framework for Rust
Documentation
use aws_config::BehaviorVersion;
use aws_sdk_s3::config::{Credentials, Region};
use aws_sdk_s3::operation::get_object::GetObjectOutput;
use aws_sdk_s3::operation::list_objects_v2::ListObjectsV2Output;
use aws_sdk_s3::Config;
use std::fs::File;
use std::io::Write;
use time::OffsetDateTime;

use zero4rs::commons;

#[::tokio::main]
async fn main() -> Result<(), anyhow::Error> {
    Ok(())
}

pub fn get_client(access_key: &str, secret_key: &str, region: &str) -> aws_sdk_s3::Client {
    let config = Config::builder()
        .behavior_version(BehaviorVersion::latest())
        .credentials_provider(Credentials::new(access_key, secret_key, None, None, ""))
        .region(Region::new(region.to_owned()))
        .build();

    aws_sdk_s3::Client::from_conf(config)
}

pub async fn download_file(
    client: &aws_sdk_s3::Client,
    bucket: &str,
    key: &str,
) -> Result<String, anyhow::Error> {
    let filename = commons::file_name(key);
    let destination = std::path::Path::new(&commons::temp_dir())
        .join(&filename)
        .display()
        .to_string();

    let now = OffsetDateTime::now_utc();

    let mut object: GetObjectOutput = client.get_object().bucket(bucket).key(key).send().await?;

    let mut file = match File::create(&destination) {
        Ok(f) => f,
        Err(e) => {
            anyhow::bail!(
                "write-temp-file-error: destination={}, err={:?}",
                &destination,
                e
            );
        }
    };

    let content_length = object.content_length.unwrap_or(0);

    while let Some(bytes) = object.body.try_next().await? {
        file.write_all(&bytes)?;
    }

    println!(
        "get_object: Region={:?}, bucket={}, key={}, destination={}, content_length={}, duration={}",
        client.config().region(),
        bucket,
        key,
        destination,
        content_length, OffsetDateTime::now_utc() - now
    );

    Ok(destination)
}

pub async fn list_objects(
    client: &aws_sdk_s3::Client,
    bucket: &str,
    prefix: &str,
) -> Result<Vec<String>, anyhow::Error> {
    let response: ListObjectsV2Output = client
        .list_objects_v2()
        .prefix(prefix)
        .bucket(bucket)
        .max_keys(1000000)
        .send()
        .await?;

    let mut keys = vec![];

    if let Some(contents) = response.contents {
        for object in contents {
            if let Some(key) = object.key() {
                keys.push(key.to_string());
            }
        }
    }

    println!(
        "list_objects: Region={:?}, bucket={}, Keys={:?}",
        client.config().region(),
        bucket,
        keys
    );

    Ok(keys)
}