add oss_util.rs
This commit is contained in:
89
src/main.rs
89
src/main.rs
@@ -1,63 +1,13 @@
|
|||||||
|
pub mod oss_util;
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
time::SystemTime,
|
time::SystemTime,
|
||||||
};
|
};
|
||||||
use crypto::{
|
use oss_util::{
|
||||||
mac::{
|
OSS_VERB_GET,
|
||||||
Mac,
|
OSSClient
|
||||||
MacResult,
|
|
||||||
},
|
|
||||||
hmac::Hmac,
|
|
||||||
sha1::Sha1,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const OSS_VERB_GET: &str = "GET";
|
|
||||||
|
|
||||||
// https://help.aliyun.com/document_detail/31952.html
|
|
||||||
pub struct OSSClient<'a> {
|
|
||||||
pub endpoint: &'a str,
|
|
||||||
pub access_key_id: &'a str,
|
|
||||||
pub access_key_secret: &'a str,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> OSSClient<'a> {
|
|
||||||
pub fn new(endpoint: &'a str, access_key_id: &'a str, access_key_secret: &'a str) -> OSSClient<'a> {
|
|
||||||
OSSClient {
|
|
||||||
endpoint: endpoint,
|
|
||||||
access_key_id: access_key_id,
|
|
||||||
access_key_secret: access_key_secret,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn generate_signed_get_url(&self, bucket_name: &str, key: &str, expire_in_seconds: u64) -> String {
|
|
||||||
self.generate_signed_url(OSS_VERB_GET, 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::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);
|
|
||||||
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));
|
|
||||||
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!");
|
||||||
|
|
||||||
@@ -67,32 +17,3 @@ fn main() {
|
|||||||
|
|
||||||
println!("{}", oss_client.generate_signed_url(OSS_VERB_GET, "aaaa", "b.txt", 1_u64, true));
|
println!("{}", oss_client.generate_signed_url(OSS_VERB_GET, "aaaa", "b.txt", 1_u64, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_base64(mac_result: MacResult) -> String {
|
|
||||||
base64::encode(mac_result.code())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn calc_hmac_sha1(key: &[u8], message: &[u8]) -> MacResult {
|
|
||||||
let mut hmac = Hmac::new(Sha1::new(), key);
|
|
||||||
hmac.input(message);
|
|
||||||
hmac.result()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_to_be_signed(verb: &str, expire_secs: u64, bucket_name: &str, key: &str) -> String {
|
|
||||||
let mut to_be_signed = String::new();
|
|
||||||
to_be_signed.push_str(verb);
|
|
||||||
to_be_signed.push_str("\n");
|
|
||||||
to_be_signed.push_str("\n");
|
|
||||||
to_be_signed.push_str("\n");
|
|
||||||
to_be_signed.push_str(expire_secs.to_string().as_str());
|
|
||||||
to_be_signed.push_str("\n");
|
|
||||||
to_be_signed.push_str("/");
|
|
||||||
to_be_signed.push_str(bucket_name);
|
|
||||||
to_be_signed.push_str("/");
|
|
||||||
to_be_signed.push_str(key);
|
|
||||||
to_be_signed
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_current_secs() -> u64 {
|
|
||||||
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs()
|
|
||||||
}
|
|
||||||
|
|||||||
89
src/oss_util.rs
Normal file
89
src/oss_util.rs
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
use std::{
|
||||||
|
time::SystemTime,
|
||||||
|
};
|
||||||
|
use crypto::{
|
||||||
|
mac::{
|
||||||
|
Mac,
|
||||||
|
MacResult,
|
||||||
|
},
|
||||||
|
hmac::Hmac,
|
||||||
|
sha1::Sha1,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const OSS_VERB_GET: &str = "GET";
|
||||||
|
|
||||||
|
// https://help.aliyun.com/document_detail/31952.html
|
||||||
|
pub struct OSSClient<'a> {
|
||||||
|
pub endpoint: &'a str,
|
||||||
|
pub access_key_id: &'a str,
|
||||||
|
pub access_key_secret: &'a str,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> OSSClient<'a> {
|
||||||
|
pub fn new(endpoint: &'a str, access_key_id: &'a str, access_key_secret: &'a str) -> OSSClient<'a> {
|
||||||
|
OSSClient {
|
||||||
|
endpoint: endpoint,
|
||||||
|
access_key_id: access_key_id,
|
||||||
|
access_key_secret: access_key_secret,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn generate_signed_get_url(&self, bucket_name: &str, key: &str, expire_in_seconds: u64) -> String {
|
||||||
|
self.generate_signed_url(OSS_VERB_GET, 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://" });
|
||||||
|
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);
|
||||||
|
|
||||||
|
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));
|
||||||
|
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 get_to_be_signed(verb: &str, expire_secs: u64, bucket_name: &str, key: &str) -> String {
|
||||||
|
let mut to_be_signed = String::with_capacity(512);
|
||||||
|
to_be_signed.push_str(verb);
|
||||||
|
to_be_signed.push_str("\n");
|
||||||
|
to_be_signed.push_str("\n");
|
||||||
|
to_be_signed.push_str("\n");
|
||||||
|
to_be_signed.push_str(expire_secs.to_string().as_str());
|
||||||
|
to_be_signed.push_str("\n");
|
||||||
|
to_be_signed.push_str("/");
|
||||||
|
to_be_signed.push_str(bucket_name);
|
||||||
|
to_be_signed.push_str("/");
|
||||||
|
to_be_signed.push_str(key);
|
||||||
|
to_be_signed
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_base64(mac_result: MacResult) -> String {
|
||||||
|
base64::encode(mac_result.code())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn calc_hmac_sha1(key: &[u8], message: &[u8]) -> MacResult {
|
||||||
|
let mut hmac = Hmac::new(Sha1::new(), key);
|
||||||
|
hmac.input(message);
|
||||||
|
hmac.result()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_current_secs() -> u64 {
|
||||||
|
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs()
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user