diff --git a/multipart-s3/Cargo.toml b/multipart-s3/Cargo.toml index 0d89e08..533081a 100644 --- a/multipart-s3/Cargo.toml +++ b/multipart-s3/Cargo.toml @@ -14,7 +14,6 @@ actix-rt = "1.0.0" rusoto_s3 = "0.43.0" rusoto_core = "0.43.0" bytes = { version = "0.5", features = ["serde"] } -serde = { version = "1.0.104", features=["derive"] } +serde = { version = "1.0.104", features = ["derive"] } serde_json = "1.0" -serde-value = "0.6.0" -dotenv = "0.15.0" \ No newline at end of file +dotenv = "0.15.0" diff --git a/multipart-s3/src/main.rs b/multipart-s3/src/main.rs index 31e9db3..7f08f07 100644 --- a/multipart-s3/src/main.rs +++ b/multipart-s3/src/main.rs @@ -1,38 +1,29 @@ -use std::io::Write; -extern crate dotenv; use actix_multipart::Multipart; use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer}; use dotenv::dotenv; -use futures::StreamExt; -mod utils; - +use serde::{Deserialize, Serialize}; use std::borrow::BorrowMut; use std::env; -use utils::upload::{ - delete_object, save_file as upload_save_file, split_payload, UplodFile, -}; -extern crate rusoto_core; -extern crate rusoto_s3; +use utils::upload::{save_file as upload_save_file, split_payload, UploadFile}; -mod model { - use serde::{Deserialize, Serialize}; - #[derive(Deserialize, Serialize, Debug)] - pub struct InpAdd { - pub text: String, - pub number: i32, - } +mod utils; + +#[derive(Deserialize, Serialize, Debug)] +pub struct InpAdd { + pub text: String, + pub number: i32, } async fn save_file(mut payload: Multipart) -> Result { let pl = split_payload(payload.borrow_mut()).await; println!("bytes={:#?}", pl.0); - let mut inp_info: model::InpAdd = serde_json::from_slice(&pl.0).unwrap(); + let inp_info: InpAdd = serde_json::from_slice(&pl.0).unwrap(); println!("converter_struct={:#?}", inp_info); println!("tmpfiles={:#?}", pl.1); //make key let s3_upload_key = format!("projects/{}/", "posts_id"); //create tmp file and upload s3 and remove tmp file - let upload_files: Vec = + let upload_files: Vec = upload_save_file(pl.1, s3_upload_key).await.unwrap(); println!("upload_files={:#?}", upload_files); Ok(HttpResponse::Ok().into()) @@ -84,16 +75,16 @@ fn index() -> HttpResponse { #[actix_rt::main] async fn main() -> std::io::Result<()> { dotenv().ok(); - let AWS_ACCESS_KEY_ID = - env::var("AWS_ACCESS_KEY_ID").expect("DATABASE_URL must be set"); - let AWS_SECRET_ACCESS_KEY = + let aws_access_key_id = + env::var("AWS_ACCESS_KEY_ID").expect("AWS_ACCESS_KEY_ID must be set"); + let aws_secret_access_key = env::var("AWS_SECRET_ACCESS_KEY").expect("AWS_SECRET_ACCESS_KEY must be set"); - let AWS_S3_BUCKET_NAME = + let aws_s3_bucket_name = env::var("AWS_S3_BUCKET_NAME").expect("AWS_S3_BUCKET_NAME must be set"); - println!("{}", AWS_ACCESS_KEY_ID); - println!("{}", AWS_SECRET_ACCESS_KEY); - println!("{}", AWS_S3_BUCKET_NAME); + println!("{}", aws_access_key_id); + println!("{}", aws_secret_access_key); + println!("{}", aws_s3_bucket_name); std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info"); std::fs::create_dir_all("./tmp").unwrap(); diff --git a/multipart-s3/src/utils/s3.rs b/multipart-s3/src/utils/s3.rs index c7cc101..5e2aaee 100644 --- a/multipart-s3/src/utils/s3.rs +++ b/multipart-s3/src/utils/s3.rs @@ -1,9 +1,10 @@ -use crate::rusoto_s3::S3; use rusoto_core::Region; +use rusoto_s3::S3; use rusoto_s3::{DeleteObjectRequest, PutObjectRequest, S3Client}; use std::io::Read; pub struct Client { + #[allow(dead_code)] region: Region, s3: S3Client, bucket_name: String, @@ -33,7 +34,7 @@ impl Client { pub async fn put_object(&self, localfilepath: &str, key: &str) -> String { let mut file = std::fs::File::open(localfilepath).unwrap(); let mut contents: Vec = Vec::new(); - file.read_to_end(&mut contents); + let _ = file.read_to_end(&mut contents); let put_request = PutObjectRequest { bucket: self.bucket_name.to_owned(), key: key.to_owned(), diff --git a/multipart-s3/src/utils/upload.rs b/multipart-s3/src/utils/upload.rs index d5cf896..8bd756f 100644 --- a/multipart-s3/src/utils/upload.rs +++ b/multipart-s3/src/utils/upload.rs @@ -4,20 +4,19 @@ use actix_web::{web, Error}; use bytes::Bytes; use futures::StreamExt; use serde::{Deserialize, Serialize}; -use serde_json::{Map as serdeMap, Value}; use std::convert::From; use std::io::Write; #[derive(Deserialize, Serialize, Debug, Clone)] -pub struct UplodFile { +pub struct UploadFile { pub filename: String, pub key: String, pub url: String, } -impl From for UplodFile { +impl From for UploadFile { fn from(tmp_file: Tmpfile) -> Self { - UplodFile { + UploadFile { filename: tmp_file.name, key: tmp_file.s3_key, url: tmp_file.s3_url, @@ -104,21 +103,21 @@ pub async fn split_payload(payload: &mut Multipart) -> (bytes::Bytes, Vec, s3_upload_key: String, -) -> Result, Error> { - let mut arr: Vec = Vec::new(); - let mut iter = tmp_files.iter(); +) -> Result, Error> { + let mut arr: Vec = Vec::with_capacity(tmp_files.len()); - while let Some(item) = iter.next() { + for item in tmp_files { let mut tmp_file: Tmpfile = item.clone(); tmp_file .s3_upload_and_tmp_remove(s3_upload_key.clone()) .await; - arr.push(UplodFile::from(tmp_file)); + arr.push(UploadFile::from(tmp_file)); } Ok(arr) } -pub async fn delete_object(mut list: Vec) { +#[allow(unused)] +pub async fn delete_object(list: Vec) { for key in list { Client::new().delete_object(key).await; }