add put
This commit is contained in:
@@ -12,6 +12,7 @@ rust-crypto = "0.2.36"
|
||||
indicatif = "0.13.0"
|
||||
urlencoding = "1.0.0"
|
||||
base64 = "0.11.0"
|
||||
reqwest = "0.9.22"
|
||||
rust_util = { git = "https://github.com/jht5945/rust_util" }
|
||||
|
||||
|
||||
|
||||
19
src/main.rs
19
src/main.rs
@@ -1,19 +1,30 @@
|
||||
pub mod oss_util;
|
||||
|
||||
use std::{
|
||||
fs::File,
|
||||
time::SystemTime,
|
||||
};
|
||||
use oss_util::{
|
||||
OSS_VERB_GET,
|
||||
OSSClient
|
||||
};
|
||||
use rust_util::{
|
||||
XResult,
|
||||
};
|
||||
|
||||
fn main() {
|
||||
fn main() -> XResult<()> {
|
||||
println!("Hello, world!");
|
||||
|
||||
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(())
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ use crypto::{
|
||||
};
|
||||
|
||||
pub const OSS_VERB_GET: &str = "GET";
|
||||
pub const OSS_VERB_PUT: &str = "PUT";
|
||||
|
||||
// https://help.aliyun.com/document_detail/31952.html
|
||||
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 {
|
||||
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=");
|
||||
|
||||
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()));
|
||||
signed_url.push_str(&urlencoding::encode(signature.as_str()));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user