From b58af4400d2122a93ec3497a1254d252c8244fb6 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 5 Jan 2020 12:57:36 +0800 Subject: [PATCH] add from_file, from_json --- Cargo.toml | 3 ++- README.md | 8 ++++++- src/lib.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ef8b56b..90ac9a0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "oss" -version = "0.1.1" +version = "0.1.2" authors = ["Hatter Jiang "] edition = "2018" description = "Simple Alibaba Cloud OSS Client in Rust" @@ -17,3 +17,4 @@ urlencoding = "1.0.0" base64 = "0.11.0" reqwest = "0.9.22" rust_util="0.2.0" +json = "0.11.14" diff --git a/README.md b/README.md index d832c5c..adaf660 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,9 @@ # simple-oss -Simple Alibaba Cloud OSS Client in Rust \ No newline at end of file +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 diff --git a/src/lib.rs b/src/lib.rs index 7733173..3a0d77e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,14 @@ use std::{ - fs::File, + fs::{ + self, + File, + }, + env, + path::PathBuf, + io::{ + Error, + ErrorKind, + }, }; use crypto::{ mac::{ @@ -45,12 +54,51 @@ impl<'a> OSSClient<'a> { /// Consider support STS! pub fn new(endpoint: &'a str, access_key_id: &'a str, access_key_secret: &'a str) -> OSSClient<'a> { OSSClient { - endpoint: endpoint, - access_key_id: access_key_id, - access_key_secret: access_key_secret, + endpoint, + access_key_id, + access_key_secret, } } + /// New OSSClient from JSON file + pub fn from_file(f: &str) -> XResult { + let f_path_buf = if f.starts_with("~/") { + let home = PathBuf::from(env::var("HOME")?); + home.join(f.chars().skip(2).collect::()) + } else { + PathBuf::from(f) + }; + let f_content = fs::read_to_string(f_path_buf)?; + Self::from_json(&f_content) + } + + /// New OSSClient from JSON + /// + /// JSON sample: + /// ```json + /// { + /// "endpoint": "", + /// "accessKeyId": "", + /// "accessKeySecret": "" + /// } + /// ``` + pub fn from_json(json: &str) -> XResult { + let json_value = json::parse(json)?; + if !json_value.is_object() { + 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()); + + 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)) + } + pub fn put_file(&self, bucket_name: &str, key: &str, expire_in_seconds: u64, file: File) -> XResult { let client = reqwest::Client::new(); Ok(client.put(&self.generate_signed_put_url(bucket_name, key, expire_in_seconds)).body(file).send()?) @@ -128,6 +176,11 @@ 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 {