This commit is contained in:
2020-01-09 01:53:07 +08:00
parent b58af4400d
commit d102a95f1d
3 changed files with 33 additions and 22 deletions

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "oss" name = "oss"
version = "0.1.2" version = "0.2.0"
authors = ["Hatter Jiang <jht5945@gmail.com>"] authors = ["Hatter Jiang <jht5945@gmail.com>"]
edition = "2018" edition = "2018"
description = "Simple Alibaba Cloud OSS Client in Rust" 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" rust-crypto = "0.2.36"
urlencoding = "1.0.0" urlencoding = "1.0.0"
base64 = "0.11.0" base64 = "0.11.0"
reqwest = "0.9.22" reqwest = "0.10.0"
rust_util="0.2.0" rust_util="0.2.0"
json = "0.11.14" json = "0.12.0"

View File

@@ -5,5 +5,12 @@ Simple Alibaba Cloud OSS Client in Rust
```rust ```rust
let oss_client = OSSClient::new("<endpoint>", "<access_key_id>", "<access_key_secret>"); let oss_client = OSSClient::new("<endpoint>", "<access_key_id>", "<access_key_secret>");
oss_cleint.put_file_content("<bucket>", "helloworld.txt", "hello world!")?; oss_cleint.put_file_content("<bucket>", "helloworld.txt", "hello world!").await?;
``` ```
#### Changelog
* v0.2.0
* Use `async/await` by `reqwest v0.10.0`

View File

@@ -1,4 +1,5 @@
use std::{ use std::{
io::Read,
fs::{ fs::{
self, self,
File, File,
@@ -99,49 +100,52 @@ impl<'a> OSSClient<'a> {
Ok(Self::new(endpoint, access_key_id, access_key_secret)) 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<Response> { /// 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<Response> {
let put_url = self.generate_signed_put_url(bucket_name, key, expire_in_seconds);
let client = reqwest::Client::new(); 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<Response> { pub async fn delete_file(&self, bucket_name: &str, key: &str) -> XResult<Response> {
let delete_url = self.generate_signed_delete_url(bucket_name, key, 30_u64); let delete_url = self.generate_signed_delete_url(bucket_name, key, 30_u64);
let client = reqwest::Client::new(); 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<Option<String>> { pub async fn get_file_content(&self, bucket_name: &str, key: &str) -> XResult<Option<String>> {
let get_url = self.generate_signed_get_url(bucket_name, key, 30_u64); 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() { match response.status().as_u16() {
404_u16 => Ok(None), 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))), _ => 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<Option<Vec<u8>>> { pub async fn get_file_content_bytes(&self, bucket_name: &str, key: &str) -> XResult<Option<Vec<u8>>> {
let get_url = self.generate_signed_get_url(bucket_name, key, 30_u64); 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() { match response.status().as_u16() {
404_u16 => Ok(None), 404_u16 => Ok(None),
200_u16 => { 200_u16 => {
let mut buf: Vec<u8> = vec![]; Ok(Some(response.bytes().await?.as_ref().to_vec()))
response.copy_to(&mut buf)?;
Ok(Some(buf))
}, },
_ => 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<dyn std::error::Error>),
} }
} }
pub fn put_file_content(&self, bucket_name: &str, key: &str, content: &str) -> XResult<Response> { pub async fn put_file_content(&self, bucket_name: &str, key: &str, content: &str) -> XResult<Response> {
self.put_file_content_bytes(bucket_name, key, content.as_bytes().to_vec()) 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<u8>) -> XResult<Response> { pub async fn put_file_content_bytes(&self, bucket_name: &str, key: &str, content_bytes: Vec<u8>) -> XResult<Response> {
let put_url = self.generate_signed_put_url(bucket_name, key, 30_u64); let put_url = self.generate_signed_put_url(bucket_name, key, 30_u64);
let client = reqwest::Client::new(); 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 { pub fn generate_signed_put_url(&self, bucket_name: &str, key: &str, expire_in_seconds: u64) -> String {