This commit is contained in:
2019-11-28 00:51:05 +08:00
parent b0ec510880
commit 730b3b7728
3 changed files with 22 additions and 4 deletions

View File

@@ -12,6 +12,7 @@ rust-crypto = "0.2.36"
indicatif = "0.13.0" indicatif = "0.13.0"
urlencoding = "1.0.0" urlencoding = "1.0.0"
base64 = "0.11.0" base64 = "0.11.0"
reqwest = "0.9.22"
rust_util = { git = "https://github.com/jht5945/rust_util" } rust_util = { git = "https://github.com/jht5945/rust_util" }

View File

@@ -1,19 +1,30 @@
pub mod oss_util; pub mod oss_util;
use std::{ use std::{
fs::File,
time::SystemTime, time::SystemTime,
}; };
use oss_util::{ use oss_util::{
OSS_VERB_GET,
OSSClient OSSClient
}; };
use rust_util::{
XResult,
};
fn main() { fn main() -> XResult<()> {
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());
let oss_client = OSSClient::new("shanghai.aliyuncs.com", "aaa", "bbb"); let oss_client = OSSClient::new("oss-cn-shanghai.aliyuncs.com", "***", "***");
let oss_put_url = oss_client.generate_signed_put_url("hatterbucket", "abc_2.txt", 1000_u64);
println!("{}", oss_client.generate_signed_url(OSS_VERB_GET, "aaaa", "b.txt", 1_u64, true)); let client = reqwest::Client::new();
let f = File::open("README.md")?;
let resp = client.put(&oss_put_url)
.body(f)
.send()?;
println!("RESP: {:?}", resp);
Ok(())
} }

View File

@@ -11,6 +11,7 @@ use crypto::{
}; };
pub const OSS_VERB_GET: &str = "GET"; pub const OSS_VERB_GET: &str = "GET";
pub const OSS_VERB_PUT: &str = "PUT";
// https://help.aliyun.com/document_detail/31952.html // https://help.aliyun.com/document_detail/31952.html
pub struct OSSClient<'a> { pub struct OSSClient<'a> {
@@ -28,6 +29,10 @@ impl<'a> OSSClient<'a> {
} }
} }
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)
}
pub fn generate_signed_get_url(&self, bucket_name: &str, key: &str, expire_in_seconds: u64) -> String { 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) self.generate_signed_url(OSS_VERB_GET, bucket_name, key, expire_in_seconds, true)
} }
@@ -51,6 +56,7 @@ impl<'a> OSSClient<'a> {
signed_url.push_str("&Signature="); signed_url.push_str("&Signature=");
let to_be_signed = get_to_be_signed(verb, expire_secs, bucket_name, key); 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())); 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.push_str(&urlencoding::encode(signature.as_str()));