Skip to content

Commit

Permalink
Merge pull request #73 from yanganto/async-api
Browse files Browse the repository at this point in the history
using async api for download and upload
  • Loading branch information
yanganto authored Feb 11, 2021
2 parents 3c1b74b + 2e76985 commit d106315
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 4 deletions.
12 changes: 10 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "s3rs"
version = "0.4.5"
version = "0.4.7"
authors = ["Antonio Yang <[email protected]>"]
description = "A s3 cli client with multi configs with diffent provider"
keywords = ["S3", "Amazon", "CEPH", "AWS"]
Expand Down Expand Up @@ -34,9 +34,17 @@ serde_json = "1.0"
regex = "0.2"
quick-xml = "0.12"
colored = "1.6"
s3handler = "0.6.6"
s3handler = "0.7.1"
clap = "2.33"
hex = "0.4.2"
blake2-rfc = "0.2"
rand = "0.7"
humansize = "1.1"
tokio = { version = "0.2", optional = true }

[features]
default = ["async"]
async = [
"s3handler/tokio-async",
"tokio"
]
56 changes: 56 additions & 0 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
#[cfg(feature = "async")]
use std::path::Path;
#[cfg(feature = "async")]
use tokio::runtime::Runtime;
use regex::Regex;
use humansize::{FileSize, file_size_opts};

use crate::logger::change_log_type;
use colored::{self, *};
#[cfg(feature = "async")]
use s3handler::{
S3Object,
none_blocking::primitives::S3Pool
};

pub mod secret;

Expand Down Expand Up @@ -206,6 +215,30 @@ pub fn do_command(handler: &mut s3handler::Handler, s3_type: &String, command: &
}
};
} else if command.starts_with("put") {
#[cfg(feature = "async")]
{
let mut rt = Runtime::new().unwrap();
let file_path = command.split_whitespace().nth(1).unwrap_or("");
let mut s3_object: S3Object = command.split_whitespace().nth(2).unwrap_or("").into();
if s3_object.key.is_none() {
s3_object.key = Some(
Path::new(file_path)
.file_name()
.map(|s|format!("/{}",s.to_string_lossy())).unwrap_or_else(||"/".to_string())
)
}
let s3_pool = S3Pool::from(&*handler);
rt.block_on(async {
match s3_pool
.resource(s3_object)
.upload_file(file_path).await {
Err(e) => println!("{}", e),
Ok(_) => println!("upload completed"),
};
});
}

#[cfg(not(feature = "async"))]
match handler.put(
command.split_whitespace().nth(1).unwrap_or(""),
command.split_whitespace().nth(2).unwrap_or(""),
Expand All @@ -214,6 +247,29 @@ pub fn do_command(handler: &mut s3handler::Handler, s3_type: &String, command: &
Ok(_) => println!("upload completed"),
};
} else if command.starts_with("get") {
#[cfg(feature = "async")]
{
let s3_pool = S3Pool::from(&*handler);
let s3_object: S3Object = command.split_whitespace().nth(1).unwrap_or("").into();
if s3_object.key.is_none() {
println!("please specify the object you want to download");
return;
}

let mut rt = Runtime::new().unwrap();
rt.block_on(
async {
match s3_pool
.resource(s3_object)
.download_file(command.split_whitespace().nth(2).unwrap_or("")).await {
Err(e) => println!("{}", e),
Ok(_) => println!("download completed"),
};
}
);
}

#[cfg(not(feature = "async"))]
match handler.get(
command.split_whitespace().nth(1).unwrap_or(""),
command.split_whitespace().nth(2),
Expand Down
3 changes: 2 additions & 1 deletion test_command.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ set timeout 120

spawn dd if=/dev/urandom bs=1024 count=7000 of=/tmp/7M
spawn cargo build
spawn cp README.md test

expect $prompt
spawn target/debug/s3rs --config=$config ls
Expand Down Expand Up @@ -65,4 +66,4 @@ expect $prompt
spawn md5sum /tmp/7M /tmp/7

interact

spawn rm -f test
12 changes: 11 additions & 1 deletion test_shell.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ set timeout 180

spawn rm -f /tmp/test
spawn rm -f /tmp/test-orig
spawn cp README.md test
spawn dd if=/dev/urandom bs=1024 count=11264 of=/tmp/test-orig
spawn cargo run

Expand All @@ -18,9 +19,15 @@ send $item\r
expect -re $prompt
send ls\r

expect -re $prompt
send "log debug\r"

expect -re $prompt
send "put test s3://$bucket\r"

expect -re $prompt
send "log error\r"

expect -re $prompt
send "ls s3://$bucket\r"

Expand Down Expand Up @@ -78,7 +85,10 @@ send "get s3://$bucket/test-orig /tmp/test\r"
expect -re $prompt
send "exit\r"

send "rm -f test\r"

expect "cya~"
spawn rm -f test
spawn md5sum /tmp/test-orig /tmp/test

interact
spawn rm -f test

0 comments on commit d106315

Please sign in to comment.