1
0
mirror of https://github.com/actix/examples synced 2024-12-12 14:13:11 +01:00

Merge pull request #285 from JohnTitor/s3-cleanup

Clean-up s3 example
This commit is contained in:
Yuki Okushi 2020-04-03 15:11:00 +09:00 committed by GitHub
commit 0f1568bd3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 41 deletions

View File

@ -16,5 +16,4 @@ rusoto_core = "0.43.0"
bytes = { version = "0.5", features = ["serde"] } 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_json = "1.0"
serde-value = "0.6.0"
dotenv = "0.15.0" dotenv = "0.15.0"

View File

@ -1,38 +1,29 @@
use std::io::Write;
extern crate dotenv;
use actix_multipart::Multipart; use actix_multipart::Multipart;
use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer}; use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer};
use dotenv::dotenv; use dotenv::dotenv;
use futures::StreamExt; use serde::{Deserialize, Serialize};
mod utils;
use std::borrow::BorrowMut; use std::borrow::BorrowMut;
use std::env; use std::env;
use utils::upload::{ use utils::upload::{save_file as upload_save_file, split_payload, UploadFile};
delete_object, save_file as upload_save_file, split_payload, UplodFile,
}; mod utils;
extern crate rusoto_core;
extern crate rusoto_s3;
mod model {
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize, Debug)] #[derive(Deserialize, Serialize, Debug)]
pub struct InpAdd { pub struct InpAdd {
pub text: String, pub text: String,
pub number: i32, pub number: i32,
} }
}
async fn save_file(mut payload: Multipart) -> Result<HttpResponse, Error> { async fn save_file(mut payload: Multipart) -> Result<HttpResponse, Error> {
let pl = split_payload(payload.borrow_mut()).await; let pl = split_payload(payload.borrow_mut()).await;
println!("bytes={:#?}", pl.0); 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!("converter_struct={:#?}", inp_info);
println!("tmpfiles={:#?}", pl.1); println!("tmpfiles={:#?}", pl.1);
//make key //make key
let s3_upload_key = format!("projects/{}/", "posts_id"); let s3_upload_key = format!("projects/{}/", "posts_id");
//create tmp file and upload s3 and remove tmp file //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(); upload_save_file(pl.1, s3_upload_key).await.unwrap();
println!("upload_files={:#?}", upload_files); println!("upload_files={:#?}", upload_files);
Ok(HttpResponse::Ok().into()) Ok(HttpResponse::Ok().into())
@ -84,16 +75,16 @@ fn index() -> HttpResponse {
#[actix_rt::main] #[actix_rt::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
dotenv().ok(); dotenv().ok();
let AWS_ACCESS_KEY_ID = let aws_access_key_id =
env::var("AWS_ACCESS_KEY_ID").expect("DATABASE_URL must be set"); env::var("AWS_ACCESS_KEY_ID").expect("AWS_ACCESS_KEY_ID must be set");
let AWS_SECRET_ACCESS_KEY = let aws_secret_access_key =
env::var("AWS_SECRET_ACCESS_KEY").expect("AWS_SECRET_ACCESS_KEY must be set"); 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"); env::var("AWS_S3_BUCKET_NAME").expect("AWS_S3_BUCKET_NAME must be set");
println!("{}", AWS_ACCESS_KEY_ID); println!("{}", aws_access_key_id);
println!("{}", AWS_SECRET_ACCESS_KEY); println!("{}", aws_secret_access_key);
println!("{}", AWS_S3_BUCKET_NAME); println!("{}", aws_s3_bucket_name);
std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info"); std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info");
std::fs::create_dir_all("./tmp").unwrap(); std::fs::create_dir_all("./tmp").unwrap();

View File

@ -1,9 +1,10 @@
use crate::rusoto_s3::S3;
use rusoto_core::Region; use rusoto_core::Region;
use rusoto_s3::S3;
use rusoto_s3::{DeleteObjectRequest, PutObjectRequest, S3Client}; use rusoto_s3::{DeleteObjectRequest, PutObjectRequest, S3Client};
use std::io::Read; use std::io::Read;
pub struct Client { pub struct Client {
#[allow(dead_code)]
region: Region, region: Region,
s3: S3Client, s3: S3Client,
bucket_name: String, bucket_name: String,
@ -33,7 +34,7 @@ impl Client {
pub async fn put_object(&self, localfilepath: &str, key: &str) -> String { pub async fn put_object(&self, localfilepath: &str, key: &str) -> String {
let mut file = std::fs::File::open(localfilepath).unwrap(); let mut file = std::fs::File::open(localfilepath).unwrap();
let mut contents: Vec<u8> = Vec::new(); let mut contents: Vec<u8> = Vec::new();
file.read_to_end(&mut contents); let _ = file.read_to_end(&mut contents);
let put_request = PutObjectRequest { let put_request = PutObjectRequest {
bucket: self.bucket_name.to_owned(), bucket: self.bucket_name.to_owned(),
key: key.to_owned(), key: key.to_owned(),

View File

@ -4,20 +4,19 @@ use actix_web::{web, Error};
use bytes::Bytes; use bytes::Bytes;
use futures::StreamExt; use futures::StreamExt;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::{Map as serdeMap, Value};
use std::convert::From; use std::convert::From;
use std::io::Write; use std::io::Write;
#[derive(Deserialize, Serialize, Debug, Clone)] #[derive(Deserialize, Serialize, Debug, Clone)]
pub struct UplodFile { pub struct UploadFile {
pub filename: String, pub filename: String,
pub key: String, pub key: String,
pub url: String, pub url: String,
} }
impl From<Tmpfile> for UplodFile { impl From<Tmpfile> for UploadFile {
fn from(tmp_file: Tmpfile) -> Self { fn from(tmp_file: Tmpfile) -> Self {
UplodFile { UploadFile {
filename: tmp_file.name, filename: tmp_file.name,
key: tmp_file.s3_key, key: tmp_file.s3_key,
url: tmp_file.s3_url, 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( pub async fn save_file(
tmp_files: Vec<Tmpfile>, tmp_files: Vec<Tmpfile>,
s3_upload_key: String, s3_upload_key: String,
) -> Result<Vec<UplodFile>, Error> { ) -> Result<Vec<UploadFile>, Error> {
let mut arr: Vec<UplodFile> = Vec::new(); let mut arr: Vec<UploadFile> = Vec::with_capacity(tmp_files.len());
let mut iter = tmp_files.iter();
while let Some(item) = iter.next() { for item in tmp_files {
let mut tmp_file: Tmpfile = item.clone(); let mut tmp_file: Tmpfile = item.clone();
tmp_file tmp_file
.s3_upload_and_tmp_remove(s3_upload_key.clone()) .s3_upload_and_tmp_remove(s3_upload_key.clone())
.await; .await;
arr.push(UplodFile::from(tmp_file)); arr.push(UploadFile::from(tmp_file));
} }
Ok(arr) 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 { for key in list {
Client::new().delete_object(key).await; Client::new().delete_object(key).await;
} }