add OSSClient
This commit is contained in:
68
src/main.rs
68
src/main.rs
@@ -12,46 +12,56 @@ use crypto::{
|
|||||||
|
|
||||||
pub const OSS_VERB_GET: &str = "GET";
|
pub const OSS_VERB_GET: &str = "GET";
|
||||||
|
|
||||||
// TODO impl
|
// https://help.aliyun.com/document_detail/31952.html
|
||||||
pub struct OSSConfig {
|
pub struct OSSClient {
|
||||||
pub endpoint: String,
|
pub endpoint: String,
|
||||||
pub access_key_id: String,
|
pub access_key_id: String,
|
||||||
pub access_key_secret: String,
|
pub access_key_secret: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl OSSClient {
|
||||||
|
pub fn new(endpoint: &str, access_key_id: &str, access_key_secret: &str) -> OSSClient {
|
||||||
|
OSSClient {
|
||||||
|
endpoint: endpoint.to_string(),
|
||||||
|
access_key_id: access_key_id.to_string(),
|
||||||
|
access_key_secret: access_key_secret.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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::new();
|
||||||
|
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.as_str());
|
||||||
|
signed_url.push_str("/");
|
||||||
|
signed_url.push_str(key);
|
||||||
|
|
||||||
|
let current_secs = get_current_secs();
|
||||||
|
let expire_secs = current_secs + expire_in_seconds;
|
||||||
|
|
||||||
|
signed_url.push_str("?Expires=");
|
||||||
|
signed_url.push_str(expire_secs.to_string().as_str());
|
||||||
|
signed_url.push_str("&OSSAccessKeyId=");
|
||||||
|
signed_url.push_str(&urlencoding::encode(self.access_key_id.as_str()));
|
||||||
|
signed_url.push_str("&Signature=");
|
||||||
|
|
||||||
|
let to_be_signed = get_to_be_signed(verb, expire_secs, bucket_name, key);
|
||||||
|
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()));
|
||||||
|
|
||||||
|
signed_url
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
println!("Hello, world!");
|
||||||
|
|
||||||
println!("{}", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs());
|
println!("{}", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs());
|
||||||
|
|
||||||
println!("{}", generate_oss_signed_url("shanghai.aliyuncs.com", "aaa", "bbb", "aaaa", "b.txt", 1_u64, true));
|
let oss_client = OSSClient::new("shanghai.aliyuncs.com", "aaa", "bbb");
|
||||||
}
|
|
||||||
|
|
||||||
// https://help.aliyun.com/document_detail/31952.html
|
println!("{}", oss_client.generate_signed_url(OSS_VERB_GET, "aaaa", "b.txt", 1_u64, true));
|
||||||
fn generate_oss_signed_url(endpoint: &str, access_key_id: &str, access_key_secret: &str,
|
|
||||||
bucket_name: &str, key: &str, expire_in_seconds: u64, is_https: bool) -> String {
|
|
||||||
let mut signed_url = String::new();
|
|
||||||
signed_url.push_str(if is_https { "https://" } else { "http://" });
|
|
||||||
signed_url.push_str(bucket_name);
|
|
||||||
signed_url.push_str(".");
|
|
||||||
signed_url.push_str(endpoint);
|
|
||||||
signed_url.push_str("/");
|
|
||||||
signed_url.push_str(key);
|
|
||||||
|
|
||||||
let current_secs = get_current_secs();
|
|
||||||
let expire_secs = current_secs + expire_in_seconds;
|
|
||||||
|
|
||||||
signed_url.push_str("?Expires=");
|
|
||||||
signed_url.push_str(expire_secs.to_string().as_str());
|
|
||||||
signed_url.push_str("&OSSAccessKeyId=");
|
|
||||||
signed_url.push_str(&urlencoding::encode(access_key_id));
|
|
||||||
signed_url.push_str("&Signature=");
|
|
||||||
|
|
||||||
let to_be_signed = get_to_be_signed(OSS_VERB_GET, expire_secs, bucket_name, key);
|
|
||||||
let signature = to_base64(calc_hmac_sha1(access_key_secret.as_bytes(), to_be_signed.as_bytes()));
|
|
||||||
signed_url.push_str(&urlencoding::encode(signature.as_str()));
|
|
||||||
|
|
||||||
signed_url
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_base64(mac_result: MacResult) -> String {
|
fn to_base64(mac_result: MacResult) -> String {
|
||||||
|
|||||||
Reference in New Issue
Block a user