update get content returns Option
This commit is contained in:
@@ -10,8 +10,10 @@ use crypto::{
|
||||
hmac::Hmac,
|
||||
sha1::Sha1,
|
||||
};
|
||||
use rust_util::XResult;
|
||||
use reqwest::Response;
|
||||
use reqwest::{
|
||||
Response,
|
||||
};
|
||||
use rust_util::*;
|
||||
|
||||
pub const OSS_VERB_GET: &str = "GET";
|
||||
pub const OSS_VERB_PUT: &str = "PUT";
|
||||
@@ -44,9 +46,14 @@ impl<'a> OSSClient<'a> {
|
||||
Ok(client.delete(&delete_url).send()?)
|
||||
}
|
||||
|
||||
pub fn get_file_content(&self, bucket_name: &str, key: &str) -> XResult<String> {
|
||||
pub 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);
|
||||
Ok(reqwest::get(&get_url)?.text()?)
|
||||
let mut response = reqwest::get(&get_url)?;
|
||||
match response.status().as_u16() {
|
||||
404_u16 => Ok(None),
|
||||
200_u16 => Ok(Some(response.text()?)),
|
||||
_ => Err(new_box_ioerror(&format!("Error in read: {}/{}, returns: {:?}", bucket_name, key, response))),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn put_file_content(&self, bucket_name: &str, key: &str, content: &str) -> XResult<Response> {
|
||||
@@ -70,11 +77,7 @@ impl<'a> OSSClient<'a> {
|
||||
pub fn generate_signed_url(&self, verb: &str, bucket_name: &str, key: &str, expire_in_seconds: u64, is_https: bool) -> String {
|
||||
let mut signed_url = String::with_capacity(1024);
|
||||
signed_url.push_str(if is_https { "https://" } else { "http://" });
|
||||
signed_url.push_str(bucket_name);
|
||||
signed_url.push_str(".");
|
||||
signed_url.push_str(self.endpoint);
|
||||
signed_url.push_str("/");
|
||||
signed_url.push_str(key);
|
||||
signed_url.push_str(&format!("{}.{}/{}", bucket_name, self.endpoint, key));
|
||||
|
||||
let current_secs = get_current_secs();
|
||||
let expire_secs = current_secs + expire_in_seconds;
|
||||
@@ -86,7 +89,6 @@ impl<'a> OSSClient<'a> {
|
||||
signed_url.push_str("&Signature=");
|
||||
|
||||
let to_be_signed = get_to_be_signed(verb, expire_secs, bucket_name, key);
|
||||
println!("{}", &to_be_signed);
|
||||
let signature = to_base64(calc_hmac_sha1(self.access_key_secret.as_bytes(), to_be_signed.as_bytes()));
|
||||
signed_url.push_str(&urlencoding::encode(signature.as_str()));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user