Compare commits
2
Commits
9f0d26e775
...
a29a9fa9f7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a29a9fa9f7
|
||
|
|
ceeec1d2cb
|
+1
-1
@@ -6,7 +6,7 @@ license = "MIT"
|
|||||||
description = "Library for managing soaricarus storage"
|
description = "Library for managing soaricarus storage"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
aws-config = { version = "1.9.0" }
|
aws-config = { version = "1.9.0", features = ["behavior-version-latest"] }
|
||||||
aws-sdk-s3 = { version = "1.138.1" }
|
aws-sdk-s3 = { version = "1.138.1" }
|
||||||
time = "0.3.53"
|
time = "0.3.53"
|
||||||
tokio = { version = "1.53.0", features = ["full"] }
|
tokio = { version = "1.53.0", features = ["full"] }
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
pub struct Config {
|
||||||
|
pub url: String,
|
||||||
|
pub bucket: String,
|
||||||
|
pub region: String,
|
||||||
|
}
|
||||||
+49
-9
@@ -1,14 +1,54 @@
|
|||||||
pub fn add(left: u64, right: u64) -> u64 {
|
pub mod config;
|
||||||
left + right
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct Data {
|
||||||
|
pub filepath: String,
|
||||||
|
pub raw_data: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
pub struct Labyrinth {
|
||||||
mod tests {
|
pub config: config::Config,
|
||||||
use super::*;
|
}
|
||||||
|
|
||||||
#[test]
|
pub async fn load_data(filepath: &str) -> Result<Vec<u8>, std::io::Error> {
|
||||||
fn it_works() {
|
tokio::fs::read(filepath).await
|
||||||
let result = add(2, 2);
|
}
|
||||||
assert_eq!(result, 4);
|
|
||||||
|
pub enum Error {
|
||||||
|
Info(String),
|
||||||
|
SError(aws_sdk_s3::operation::put_object::PutObjectError),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Labyrinth {
|
||||||
|
pub async fn upload(
|
||||||
|
&self,
|
||||||
|
file_key: &str,
|
||||||
|
data: &Data,
|
||||||
|
) -> Result<aws_sdk_s3::operation::put_object::PutObjectOutput, Error> {
|
||||||
|
let config = aws_config::load_from_env().await;
|
||||||
|
let client = aws_sdk_s3::Client::new(&config);
|
||||||
|
|
||||||
|
let data_content = if data.raw_data.is_empty() {
|
||||||
|
match load_data(&data.filepath).await {
|
||||||
|
Ok(content) => content,
|
||||||
|
Err(err) => return Err(Error::Info(err.to_string())),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
data.raw_data.to_owned()
|
||||||
|
};
|
||||||
|
|
||||||
|
let body = aws_sdk_s3::primitives::SdkBody::from(data_content);
|
||||||
|
let b_stream = aws_sdk_s3::primitives::ByteStream::from(body);
|
||||||
|
match client
|
||||||
|
.put_object()
|
||||||
|
.bucket(self.config.bucket.clone())
|
||||||
|
.key(file_key)
|
||||||
|
.body(b_stream)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(response) => Ok(response),
|
||||||
|
Err(err) => Err(Error::Info(err.to_string())),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user