mirror of
https://github.com/actix/examples
synced 2024-12-03 18:22:14 +01:00
commit
0f1568bd3c
@ -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"
|
@ -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 {
|
||||
mod utils;
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
pub struct InpAdd {
|
||||
pub text: String,
|
||||
pub number: i32,
|
||||
}
|
||||
}
|
||||
|
||||
async fn save_file(mut payload: Multipart) -> Result<HttpResponse, Error> {
|
||||
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<UplodFile> =
|
||||
let upload_files: Vec<UploadFile> =
|
||||
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();
|
||||
|
@ -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<u8> = 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(),
|
||||
|
@ -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<Tmpfile> for UplodFile {
|
||||
impl From<Tmpfile> 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<Tmpfil
|
||||
pub async fn save_file(
|
||||
tmp_files: Vec<Tmpfile>,
|
||||
s3_upload_key: String,
|
||||
) -> Result<Vec<UplodFile>, Error> {
|
||||
let mut arr: Vec<UplodFile> = Vec::new();
|
||||
let mut iter = tmp_files.iter();
|
||||
) -> Result<Vec<UploadFile>, Error> {
|
||||
let mut arr: Vec<UploadFile> = 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<String>) {
|
||||
#[allow(unused)]
|
||||
pub async fn delete_object(list: Vec<String>) {
|
||||
for key in list {
|
||||
Client::new().delete_object(key).await;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user