From d102a95f1d6a5a26444aa0c30eff8e7284e6b249 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Thu, 9 Jan 2020 01:53:07 +0800 Subject: [PATCH] v0.2.0 --- Cargo.toml | 6 +++--- README.md | 11 +++++++++-- src/lib.rs | 38 +++++++++++++++++++++----------------- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 90ac9a0..af2445a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "oss" -version = "0.1.2" +version = "0.2.0" authors = ["Hatter Jiang "] edition = "2018" description = "Simple Alibaba Cloud OSS Client in Rust" @@ -15,6 +15,6 @@ repository = "https://git.hatter.ink/hatter/simple-oss" rust-crypto = "0.2.36" urlencoding = "1.0.0" base64 = "0.11.0" -reqwest = "0.9.22" +reqwest = "0.10.0" rust_util="0.2.0" -json = "0.11.14" +json = "0.12.0" diff --git a/README.md b/README.md index adaf660..8031444 100644 --- a/README.md +++ b/README.md @@ -5,5 +5,12 @@ Simple Alibaba Cloud OSS Client in Rust ```rust let oss_client = OSSClient::new("", "", ""); -oss_cleint.put_file_content("", "helloworld.txt", "hello world!")?; -``` \ No newline at end of file +oss_cleint.put_file_content("", "helloworld.txt", "hello world!").await?; +``` + + +#### Changelog + +* v0.2.0 + * Use `async/await` by `reqwest v0.10.0` + diff --git a/src/lib.rs b/src/lib.rs index 3a0d77e..30372bc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ use std::{ + io::Read, fs::{ self, File, @@ -99,49 +100,52 @@ impl<'a> OSSClient<'a> { Ok(Self::new(endpoint, access_key_id, access_key_secret)) } - pub fn put_file(&self, bucket_name: &str, key: &str, expire_in_seconds: u64, file: File) -> XResult { + /// Put file will read full file content to memory and send with HTTP protocol + pub async fn put_file(&self, bucket_name: &str, key: &str, expire_in_seconds: u64, file: File) -> XResult { + let put_url = self.generate_signed_put_url(bucket_name, key, expire_in_seconds); let client = reqwest::Client::new(); - Ok(client.put(&self.generate_signed_put_url(bucket_name, key, expire_in_seconds)).body(file).send()?) + let mut v = Vec::new(); + let mut file = file; + file.read_to_end(&mut v)?; + Ok(client.put(&put_url).body(v).send().await?) } - pub fn delete_file(&self, bucket_name: &str, key: &str) -> XResult { + pub async fn delete_file(&self, bucket_name: &str, key: &str) -> XResult { let delete_url = self.generate_signed_delete_url(bucket_name, key, 30_u64); let client = reqwest::Client::new(); - Ok(client.delete(&delete_url).send()?) + Ok(client.delete(&delete_url).send().await?) } - pub fn get_file_content(&self, bucket_name: &str, key: &str) -> XResult> { + pub async fn get_file_content(&self, bucket_name: &str, key: &str) -> XResult> { let get_url = self.generate_signed_get_url(bucket_name, key, 30_u64); - let mut response = reqwest::get(&get_url)?; + let response = reqwest::get(&get_url).await?; match response.status().as_u16() { 404_u16 => Ok(None), - 200_u16 => Ok(Some(response.text()?)), + 200_u16 => Ok(Some(response.text().await?)), _ => Err(new_box_ioerror(&format!("Error in read: {}/{}, returns: {:?}", bucket_name, key, response))), } } - pub fn get_file_content_bytes(&self, bucket_name: &str, key: &str) -> XResult>> { + pub async fn get_file_content_bytes(&self, bucket_name: &str, key: &str) -> XResult>> { let get_url = self.generate_signed_get_url(bucket_name, key, 30_u64); - let mut response = reqwest::get(&get_url)?; + let response = reqwest::get(&get_url).await?; match response.status().as_u16() { 404_u16 => Ok(None), 200_u16 => { - let mut buf: Vec = vec![]; - response.copy_to(&mut buf)?; - Ok(Some(buf)) + Ok(Some(response.bytes().await?.as_ref().to_vec())) }, - _ => Err(new_box_ioerror(&format!("Error in read: {}/{}, returns: {:?}", bucket_name, key, response))), + _ => Err(new_box_ioerror(&format!("Error in read: {}/{}, returns: {:?}", bucket_name, key, response)) as Box), } } - pub fn put_file_content(&self, bucket_name: &str, key: &str, content: &str) -> XResult { - self.put_file_content_bytes(bucket_name, key, content.as_bytes().to_vec()) + pub async fn put_file_content(&self, bucket_name: &str, key: &str, content: &str) -> XResult { + self.put_file_content_bytes(bucket_name, key, content.as_bytes().to_vec()).await } - pub fn put_file_content_bytes(&self, bucket_name: &str, key: &str, content_bytes: Vec) -> XResult { + pub async fn put_file_content_bytes(&self, bucket_name: &str, key: &str, content_bytes: Vec) -> XResult { let put_url = self.generate_signed_put_url(bucket_name, key, 30_u64); let client = reqwest::Client::new(); - Ok(client.put(&put_url).body(content_bytes).send()?) + Ok(client.put(&put_url).body(content_bytes).send().await?) } pub fn generate_signed_put_url(&self, bucket_name: &str, key: &str, expire_in_seconds: u64) -> String {