update get content returns Option

This commit is contained in:
2019-12-08 22:51:34 +08:00
parent 8ac0b56bc1
commit dc1ab83d28
2 changed files with 23 additions and 12 deletions

View File

@@ -32,6 +32,13 @@ fn main() -> XResult<()> {
println!("Hello, world!");
println!("{}", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs());
let oss_client = OSSClient::new("oss-cn-shanghai.aliyuncs.com", "", "");
let c = oss_client.get_file_content("hatterbucket", "Check.java")?;
println!("XXXXX: {:?}", c);
// println!("XXXXX: {}", );
// zip_util::zip_file("hello.txt", "aa.zip")?;
// let openpgp_client = OpenPGPTool::from_file("sample.gpg")?;
@@ -54,9 +61,11 @@ fn main() -> XResult<()> {
// TODO call it!
pub fn process_oss_files(oss_client: &OSSClient, bucket_name: &str, path: &str, meta_file_name: &str, new_file: &str, limit: usize) -> XResult<()> {
// TODO not found?
let meta_file_key = &format!("{}/{}", path, meta_file_name);
let meta_file_content = oss_client.get_file_content(bucket_name, meta_file_key)?;
let meta_file_content = match oss_client.get_file_content(bucket_name, meta_file_key)? {
None => "[]".to_string(),
Some(c) => c,
};
let (removed_file, new_meta_file_content) = process_new_backup_file(&meta_file_content, new_file, limit)?;
oss_client.put_file_content(bucket_name, meta_file_key, &new_meta_file_content)?;
if !removed_file.is_empty() {

View File

@@ -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()));