add delete_file,get_file_content,put_file_content

This commit is contained in:
2019-12-07 00:54:43 +08:00
parent 46c1a37d99
commit 25ad3ca457
2 changed files with 23 additions and 3 deletions

View File

@@ -31,7 +31,7 @@ fn main() -> XResult<()> {
println!("Hello, world!");
println!("{}", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs());
zip_util::zip_file("hello.txt", "aa.zip")?;
// zip_util::zip_file("hello.txt", "aa.zip")?;
// let openpgp_client = OpenPGPTool::from_file("sample.gpg")?;
// openpgp_client.encrypt_file("a", "b.asc", true)?;

View File

@@ -15,6 +15,7 @@ use reqwest::Response;
pub const OSS_VERB_GET: &str = "GET";
pub const OSS_VERB_PUT: &str = "PUT";
pub const OSS_VERB_DELETE: &str = "DELETE";
// https://help.aliyun.com/document_detail/31952.html
pub struct OSSClient<'a> {
@@ -37,6 +38,23 @@ impl<'a> OSSClient<'a> {
Ok(client.put(&self.generate_signed_put_url(bucket_name, key, expire_in_seconds)).body(file).send()?)
}
pub 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 client = reqwest::Client::new();
Ok(client.delete(&delete_url).send()?)
}
pub fn get_file_content(&self, bucket_name: &str, key: &str) -> XResult<String> {
let get_url = self.generate_signed_get_url(bucket_name, key, 30_u64);
Ok(reqwest::get(&get_url)?.text()?)
}
pub fn put_file_content(&self, bucket_name: &str, key: &str, content: &str) -> XResult<Response> {
let put_url = self.generate_signed_put_url(bucket_name, key, 30_u64);
let client = reqwest::Client::new();
Ok(client.put(&put_url).body(content.as_bytes().to_vec()).send()?)
}
pub fn generate_signed_put_url(&self, bucket_name: &str, key: &str, expire_in_seconds: u64) -> String {
self.generate_signed_url(OSS_VERB_PUT, bucket_name, key, expire_in_seconds, true)
}
@@ -45,6 +63,10 @@ impl<'a> OSSClient<'a> {
self.generate_signed_url(OSS_VERB_GET, bucket_name, key, expire_in_seconds, true)
}
pub fn generate_signed_delete_url(&self, bucket_name: &str, key: &str, expire_in_seconds: u64) -> String {
self.generate_signed_url(OSS_VERB_DELETE, bucket_name, key, expire_in_seconds, true)
}
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://" });
@@ -101,7 +123,5 @@ fn get_current_secs() -> u64 {
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs()
}
// TODO read/write content from/to url
// use sync meta.txt file ?
// record valid files ...