use consts

This commit is contained in:
2020-04-04 22:47:32 +08:00
parent 926d557ab0
commit ce5e884841
2 changed files with 57 additions and 49 deletions

View File

@@ -21,6 +21,14 @@ pub const OSS_VERB_GET: &str = "GET";
pub const OSS_VERB_PUT: &str = "PUT";
pub const OSS_VERB_DELETE: &str = "DELETE";
const HTTP_SS: &str = "http://";
const HTTPS_SS: &str = "https://";
const HTTP_SC_SUCCESS: u16 = 200;
const HTTP_SC_NOT_FOUND: u16 = 404;
const INTERNAL_DEFAULT_VALID_IN_SECS: u64 = 30;
// https://help.aliyun.com/document_detail/31952.html
pub struct OSSClient {
pub endpoint: String,
@@ -31,9 +39,9 @@ pub struct OSSClient {
impl OSSClient {
pub fn new(endpoint: &str, access_key_id: &str, access_key_secret: &str) -> OSSClient {
OSSClient {
endpoint: endpoint.to_owned(),
access_key_id: access_key_id.to_owned(),
access_key_secret: access_key_secret.to_owned(),
endpoint: endpoint.into(),
access_key_id: access_key_id.into(),
access_key_secret: access_key_secret.into(),
}
}
@@ -43,23 +51,23 @@ impl OSSClient {
}
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 delete_url = self.generate_signed_delete_url(bucket_name, key, INTERNAL_DEFAULT_VALID_IN_SECS);
let client = reqwest::Client::new();
Ok(client.delete(&delete_url).send()?)
}
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);
let get_url = self.generate_signed_get_url(bucket_name, key, INTERNAL_DEFAULT_VALID_IN_SECS);
let mut response = reqwest::get(&get_url)?;
match response.status().as_u16() {
404_u16 => Ok(None),
200_u16 => Ok(Some(response.text()?)),
HTTP_SC_NOT_FOUND => Ok(None),
HTTP_SC_SUCCESS => 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> {
let put_url = self.generate_signed_put_url(bucket_name, key, 30_u64);
let put_url = self.generate_signed_put_url(bucket_name, key, INTERNAL_DEFAULT_VALID_IN_SECS);
let client = reqwest::Client::new();
Ok(client.put(&put_url).body(content.as_bytes().to_vec()).send()?)
}
@@ -78,7 +86,7 @@ impl OSSClient {
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(iff!(is_https, "https://", "http://"));
signed_url.push_str(iff!(is_https, HTTPS_SS, HTTP_SS));
let endpoint = &remove_endpoint_http_or_s(&self.endpoint);
signed_url.push_str(&format!("{}.{}/{}", bucket_name, endpoint, key));
@@ -103,7 +111,7 @@ impl OSSClient {
// https://endpoint, or http://endpoint -> endpoint
fn remove_endpoint_http_or_s(endpoint: &str) -> String {
let mut endpoint = endpoint.to_owned();
for prefix in vec!["http://", "https://"] {
for prefix in vec![HTTP_SS, HTTPS_SS] {
if endpoint.starts_with(prefix) {
endpoint = endpoint.chars().skip(prefix.chars().count()).collect::<String>()
}