Adding some code to upload object
This commit is contained in:
+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,15 @@
|
|||||||
|
|
||||||
|
/*
|
||||||
|
pub async fn init_config(url: &str, region: String) -> Result<aws_config::SdkConfig, std::io::Error> {
|
||||||
|
let r = region.as_str();
|
||||||
|
let config = aws_config::from_env().endpoint_url(url).region(r).load().await;
|
||||||
|
|
||||||
|
Ok(config)
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
pub struct Config {
|
||||||
|
pub url: String,
|
||||||
|
pub bucket: String,
|
||||||
|
pub region: String,
|
||||||
|
}
|
||||||
+64
@@ -1,3 +1,67 @@
|
|||||||
|
pub mod config;
|
||||||
|
|
||||||
|
|
||||||
|
// use aws_sdk_s3::config::*;
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct Data {
|
||||||
|
pub filepath: String,
|
||||||
|
pub raw_data: Vec<u8>
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub struct Labyrinth {
|
||||||
|
pub config: config::Config,
|
||||||
|
}
|
||||||
|
|
||||||
|
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::from_env().endpoint_url(&self.config.url).region("").load().await;
|
||||||
|
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 tokio::fs::read(&data.filepath).await {
|
||||||
|
Ok(content) => content,
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("Error: {err:?}");
|
||||||
|
Vec::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} 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) => {
|
||||||
|
println!("Response: {response:?}");
|
||||||
|
println!("Uploaded");
|
||||||
|
|
||||||
|
Ok(response)
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("Error: {err:?}");
|
||||||
|
// aws_sdk_s3::error::SdkError<aws_sdk_s3::operation::put_object::PutObjectError, >
|
||||||
|
Err(Error::Info(err.to_string()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn add(left: u64, right: u64) -> u64 {
|
pub fn add(left: u64, right: u64) -> u64 {
|
||||||
left + right
|
left + right
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user