From 787ca083d991b4759e57590ddb6e34eb36915d6c Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sat, 30 May 2020 19:38:47 +0800 Subject: [PATCH] v0.3.0 --- Cargo.toml | 2 +- README.md | 2 ++ src/lib.rs | 53 +++++++++++++++++++---------------------------------- 3 files changed, 22 insertions(+), 35 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index af2445a..acdef75 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "oss" -version = "0.2.0" +version = "0.3.0" authors = ["Hatter Jiang "] edition = "2018" description = "Simple Alibaba Cloud OSS Client in Rust" diff --git a/README.md b/README.md index 8031444..0783585 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,8 @@ oss_cleint.put_file_content("", "helloworld.txt", "hello world!").await? #### Changelog +* v0.3.0 + * Do not use `'a` lifecycle * v0.2.0 * Use `async/await` by `reqwest v0.10.0` diff --git a/src/lib.rs b/src/lib.rs index 30372bc..d15cbd3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,27 +1,16 @@ use std::{ io::Read, - fs::{ - self, - File, - }, + fs::{ self, File, }, env, path::PathBuf, - io::{ - Error, - ErrorKind, - }, + io::{ Error, ErrorKind, }, }; use crypto::{ - mac::{ - Mac, - MacResult, - }, + mac::{ Mac, MacResult, }, hmac::Hmac, sha1::Sha1, }; -use reqwest::{ - Response, -}; +use reqwest::Response; use rust_util::{ iff, XResult, @@ -40,24 +29,25 @@ pub const OSS_VERB_DELETE: &str = "DELETE"; /// ```rust /// let oss_client = OSSClient::new("AK", "SK"); /// ``` -pub struct OSSClient<'a> { - pub endpoint: &'a str, - pub access_key_id: &'a str, - pub access_key_secret: &'a str, +#[derive(Clone, Debug)] +pub struct OSSClient { + endpoint: String, + access_key_id: String, + access_key_secret: String, } /// OSS Client implemention -impl<'a> OSSClient<'a> { +impl OSSClient { /// New OSSClient /// /// Use access_key_id and access_key_secret to create a OSSClient /// Consider support STS! - pub fn new(endpoint: &'a str, access_key_id: &'a str, access_key_secret: &'a str) -> OSSClient<'a> { + pub fn new(endpoint: &str, access_key_id: &str, access_key_secret: &str) -> OSSClient { OSSClient { - endpoint, - access_key_id, - access_key_secret, + endpoint: endpoint.into(), + access_key_id: access_key_id.into(), + access_key_secret: access_key_secret.into(), } } @@ -89,14 +79,14 @@ impl<'a> OSSClient<'a> { return Err(Box::new(Error::new(ErrorKind::Other, format!("JSON format erorr: {}", json)))); } - let endpoint = Self::string_to_a_str(json_value["endpoint"].as_str().unwrap_or_default()); - let access_key_id = Self::string_to_a_str(json_value["accessKeyId"].as_str().unwrap_or_default()); - let access_key_secret = Self::string_to_a_str(json_value["accessKeySecret"].as_str().unwrap_or_default()); + let endpoint = json_value["endpoint"].as_str().unwrap_or_default(); + let access_key_id = json_value["accessKeyId"].as_str().unwrap_or_default(); + let access_key_secret = json_value["accessKeySecret"].as_str().unwrap_or_default(); if endpoint.is_empty() || access_key_id.is_empty() || access_key_secret.is_empty() { return Err(Box::new(Error::new(ErrorKind::Other,"Endpoint, access_key_id or access_key_secret cannot be empty"))); } - + Ok(Self::new(endpoint, access_key_id, access_key_secret)) } @@ -171,7 +161,7 @@ impl<'a> OSSClient<'a> { signed_url.push_str("?Expires="); signed_url.push_str(expire_secs.to_string().as_str()); signed_url.push_str("&OSSAccessKeyId="); - signed_url.push_str(&urlencoding::encode(self.access_key_id)); + signed_url.push_str(&urlencoding::encode(&self.access_key_id)); signed_url.push_str("&Signature="); let to_be_signed = get_to_be_signed(verb, expire_secs, bucket_name, key); @@ -180,11 +170,6 @@ impl<'a> OSSClient<'a> { signed_url } - - // SAFE? may these codes cause memory leak? - fn string_to_a_str(s: &str) -> &'a str { - Box::leak(s.to_owned().into_boxed_str()) - } } fn get_to_be_signed(verb: &str, expire_secs: u64, bucket_name: &str, key: &str) -> String {