Compare commits
6 Commits
b58af4400d
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
f56b984a83
|
|||
|
dd0a07c079
|
|||
|
12074a26d4
|
|||
| ee5a31b9c5 | |||
| 787ca083d9 | |||
| d102a95f1d |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,3 +1,5 @@
|
|||||||
|
.idea/
|
||||||
|
|
||||||
# ---> Rust
|
# ---> Rust
|
||||||
# Generated by Cargo
|
# Generated by Cargo
|
||||||
# will have compiled files and executables
|
# will have compiled files and executables
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "oss"
|
name = "oss"
|
||||||
version = "0.1.2"
|
version = "0.3.3"
|
||||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
description = "Simple Alibaba Cloud OSS Client in Rust"
|
description = "Simple Alibaba Cloud OSS Client in Rust"
|
||||||
@@ -15,6 +15,7 @@ repository = "https://git.hatter.ink/hatter/simple-oss"
|
|||||||
rust-crypto = "0.2.36"
|
rust-crypto = "0.2.36"
|
||||||
urlencoding = "1.0.0"
|
urlencoding = "1.0.0"
|
||||||
base64 = "0.11.0"
|
base64 = "0.11.0"
|
||||||
reqwest = "0.9.22"
|
reqwest = { version = "0.12.0", features = ["stream"] }
|
||||||
rust_util="0.2.0"
|
rust_util = "0.6.3"
|
||||||
json = "0.11.14"
|
json = "0.12.0"
|
||||||
|
tokio-util = "0.7.11"
|
||||||
|
|||||||
11
README.md
11
README.md
@@ -5,5 +5,14 @@ Simple Alibaba Cloud OSS Client in Rust
|
|||||||
|
|
||||||
```rust
|
```rust
|
||||||
let oss_client = OSSClient::new("<endpoint>", "<access_key_id>", "<access_key_secret>");
|
let oss_client = OSSClient::new("<endpoint>", "<access_key_id>", "<access_key_secret>");
|
||||||
oss_cleint.put_file_content("<bucket>", "helloworld.txt", "hello world!")?;
|
oss_cleint.put_file_content("<bucket>", "helloworld.txt", "hello world!").await?;
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
#### Changelog
|
||||||
|
|
||||||
|
* v0.3.0
|
||||||
|
* Do not use `'a` lifecycle
|
||||||
|
* v0.2.0
|
||||||
|
* Use `async/await` by `reqwest v0.10.0`
|
||||||
|
|
||||||
|
|||||||
153
src/lib.rs
153
src/lib.rs
@@ -1,31 +1,19 @@
|
|||||||
use std::{
|
#[macro_use]
|
||||||
fs::{
|
extern crate rust_util;
|
||||||
self,
|
|
||||||
File,
|
|
||||||
},
|
|
||||||
env,
|
|
||||||
path::PathBuf,
|
|
||||||
io::{
|
|
||||||
Error,
|
|
||||||
ErrorKind,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
use crypto::{
|
use crypto::{
|
||||||
mac::{
|
|
||||||
Mac,
|
|
||||||
MacResult,
|
|
||||||
},
|
|
||||||
hmac::Hmac,
|
hmac::Hmac,
|
||||||
|
mac::{Mac, MacResult},
|
||||||
sha1::Sha1,
|
sha1::Sha1,
|
||||||
};
|
};
|
||||||
use reqwest::{
|
use reqwest::{Body, Response};
|
||||||
Response,
|
use rust_util::{new_box_ioerror, util_time::get_current_secs, XResult};
|
||||||
};
|
use std::{
|
||||||
use rust_util::{
|
env,
|
||||||
iff,
|
fs::{self, File},
|
||||||
XResult,
|
io::Read,
|
||||||
new_box_ioerror,
|
io::{Error, ErrorKind},
|
||||||
util_time::get_current_secs,
|
path::PathBuf,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const OSS_VERB_GET: &str = "GET";
|
pub const OSS_VERB_GET: &str = "GET";
|
||||||
@@ -36,27 +24,39 @@ pub const OSS_VERB_DELETE: &str = "DELETE";
|
|||||||
///
|
///
|
||||||
/// Reference URL: https://help.aliyun.com/document_detail/31952.html
|
/// Reference URL: https://help.aliyun.com/document_detail/31952.html
|
||||||
///
|
///
|
||||||
/// ```rust
|
#[derive(Clone, Debug)]
|
||||||
/// let oss_client = OSSClient::new("AK", "SK");
|
pub struct OSSClient {
|
||||||
/// ```
|
endpoint: String,
|
||||||
pub struct OSSClient<'a> {
|
access_key_id: String,
|
||||||
pub endpoint: &'a str,
|
access_key_secret: String,
|
||||||
pub access_key_id: &'a str,
|
security_token: Option<String>,
|
||||||
pub access_key_secret: &'a str,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// OSS Client implemention
|
/// OSS Client implemention
|
||||||
impl<'a> OSSClient<'a> {
|
impl OSSClient {
|
||||||
|
/// New OSSClient
|
||||||
|
///
|
||||||
|
/// Use access_key_id and access_key_secret to create a OSSClient
|
||||||
|
/// Consider support STS!
|
||||||
|
pub fn new(endpoint: &str, access_key_id: &str, access_key_secret: &str) -> OSSClient {
|
||||||
|
OSSClient {
|
||||||
|
endpoint: endpoint.into(),
|
||||||
|
access_key_id: access_key_id.into(),
|
||||||
|
access_key_secret: access_key_secret.into(),
|
||||||
|
security_token: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// New OSSClient
|
/// New OSSClient
|
||||||
///
|
///
|
||||||
/// Use access_key_id and access_key_secret to create a OSSClient
|
/// Use access_key_id and access_key_secret to create a OSSClient
|
||||||
/// Consider support STS!
|
/// Consider support STS!
|
||||||
pub fn new(endpoint: &'a str, access_key_id: &'a str, access_key_secret: &'a str) -> OSSClient<'a> {
|
pub fn new_sts(endpoint: &str, access_key_id: &str, access_key_secret: &str, security_token: &str) -> OSSClient {
|
||||||
OSSClient {
|
OSSClient {
|
||||||
endpoint,
|
endpoint: endpoint.into(),
|
||||||
access_key_id,
|
access_key_id: access_key_id.into(),
|
||||||
access_key_secret,
|
access_key_secret: access_key_secret.into(),
|
||||||
|
security_token: Some(security_token.into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,9 +88,9 @@ impl<'a> OSSClient<'a> {
|
|||||||
return Err(Box::new(Error::new(ErrorKind::Other, format!("JSON format erorr: {}", json))));
|
return Err(Box::new(Error::new(ErrorKind::Other, format!("JSON format erorr: {}", json))));
|
||||||
}
|
}
|
||||||
|
|
||||||
let endpoint = Self::string_to_a_str(json_value["endpoint"].as_str().unwrap_or_default());
|
let endpoint = json_value["endpoint"].as_str().unwrap_or_default();
|
||||||
let access_key_id = Self::string_to_a_str(json_value["accessKeyId"].as_str().unwrap_or_default());
|
let access_key_id = json_value["accessKeyId"].as_str().unwrap_or_default();
|
||||||
let access_key_secret = Self::string_to_a_str(json_value["accessKeySecret"].as_str().unwrap_or_default());
|
let access_key_secret = json_value["accessKeySecret"].as_str().unwrap_or_default();
|
||||||
|
|
||||||
if endpoint.is_empty() || access_key_id.is_empty() || access_key_secret.is_empty() {
|
if endpoint.is_empty() || access_key_id.is_empty() || access_key_secret.is_empty() {
|
||||||
return Err(Box::new(Error::new(ErrorKind::Other, "Endpoint, access_key_id or access_key_secret cannot be empty")));
|
return Err(Box::new(Error::new(ErrorKind::Other, "Endpoint, access_key_id or access_key_secret cannot be empty")));
|
||||||
@@ -99,49 +99,66 @@ impl<'a> OSSClient<'a> {
|
|||||||
Ok(Self::new(endpoint, access_key_id, access_key_secret))
|
Ok(Self::new(endpoint, access_key_id, access_key_secret))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn put_file(&self, bucket_name: &str, key: &str, expire_in_seconds: u64, file: File) -> XResult<Response> {
|
/// Put file will read full file content to memory and send with HTTP protocol
|
||||||
|
pub async fn put_file(&self, bucket_name: &str, key: &str, expire_in_seconds: u64, file: File) -> XResult<Response> {
|
||||||
|
let put_url = self.generate_signed_put_url(bucket_name, key, expire_in_seconds);
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
Ok(client.put(&self.generate_signed_put_url(bucket_name, key, expire_in_seconds)).body(file).send()?)
|
let mut v = vec![];
|
||||||
|
let mut file = file;
|
||||||
|
file.read_to_end(&mut v)?;
|
||||||
|
Ok(client.put(&put_url).body(v).send().await?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete_file(&self, bucket_name: &str, key: &str) -> XResult<Response> {
|
/// Put body to OSS
|
||||||
|
// tokio = { version = "1.0", features = ["full"] }
|
||||||
|
// use tokio::fs::File;
|
||||||
|
// tokio-util = { version = "0.6", features = ["codec"] }
|
||||||
|
// use tokio_util::codec::{BytesCodec, FramedRead};
|
||||||
|
// fn file_to_body(file: File) -> Body {
|
||||||
|
// let stream = FramedRead::new(file, BytesCodec::new());
|
||||||
|
// let body = Body::wrap_stream(stream);
|
||||||
|
// body
|
||||||
|
// }
|
||||||
|
pub async fn put_body(&self, bucket_name: &str, key: &str, expire_in_seconds: u64, body: Body) -> XResult<Response> {
|
||||||
|
let put_url = self.generate_signed_put_url(bucket_name, key, expire_in_seconds);
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
Ok(client.put(&put_url).body(body).send().await?)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async 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, 30_u64);
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
Ok(client.delete(&delete_url).send()?)
|
Ok(client.delete(&delete_url).send().await?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_file_content(&self, bucket_name: &str, key: &str) -> XResult<Option<String>> {
|
pub async 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, 30_u64);
|
||||||
let mut response = reqwest::get(&get_url)?;
|
let response = reqwest::get(&get_url).await?;
|
||||||
match response.status().as_u16() {
|
match response.status().as_u16() {
|
||||||
404_u16 => Ok(None),
|
404_u16 => Ok(None),
|
||||||
200_u16 => Ok(Some(response.text()?)),
|
200_u16 => Ok(Some(response.text().await?)),
|
||||||
_ => Err(new_box_ioerror(&format!("Error in read: {}/{}, returns: {:?}", bucket_name, key, response))),
|
_ => Err(new_box_ioerror(&format!("Error in read: {}/{}, returns: {:?}", bucket_name, key, response))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_file_content_bytes(&self, bucket_name: &str, key: &str) -> XResult<Option<Vec<u8>>> {
|
pub async fn get_file_content_bytes(&self, bucket_name: &str, key: &str) -> XResult<Option<Vec<u8>>> {
|
||||||
let get_url = self.generate_signed_get_url(bucket_name, key, 30_u64);
|
let get_url = self.generate_signed_get_url(bucket_name, key, 30_u64);
|
||||||
let mut response = reqwest::get(&get_url)?;
|
let response = reqwest::get(&get_url).await?;
|
||||||
match response.status().as_u16() {
|
match response.status().as_u16() {
|
||||||
404_u16 => Ok(None),
|
404_u16 => Ok(None),
|
||||||
200_u16 => {
|
200_u16 => Ok(Some(response.bytes().await?.as_ref().to_vec())),
|
||||||
let mut buf: Vec<u8> = vec![];
|
_ => Err(new_box_ioerror(&format!("Error in read: {}/{}, returns: {:?}", bucket_name, key, response)) as Box<dyn std::error::Error>),
|
||||||
response.copy_to(&mut buf)?;
|
|
||||||
Ok(Some(buf))
|
|
||||||
},
|
|
||||||
_ => 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> {
|
pub async fn put_file_content(&self, bucket_name: &str, key: &str, content: &str) -> XResult<Response> {
|
||||||
self.put_file_content_bytes(bucket_name, key, content.as_bytes().to_vec())
|
self.put_file_content_bytes(bucket_name, key, content.as_bytes().to_vec()).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn put_file_content_bytes(&self, bucket_name: &str, key: &str, content_bytes: Vec<u8>) -> XResult<Response> {
|
pub async fn put_file_content_bytes(&self, bucket_name: &str, key: &str, content_bytes: Vec<u8>) -> 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, 30_u64);
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
Ok(client.put(&put_url).body(content_bytes).send()?)
|
Ok(client.put(&put_url).body(content_bytes).send().await?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate_signed_put_url(&self, bucket_name: &str, key: &str, expire_in_seconds: u64) -> String {
|
pub fn generate_signed_put_url(&self, bucket_name: &str, key: &str, expire_in_seconds: u64) -> String {
|
||||||
@@ -167,23 +184,23 @@ impl<'a> OSSClient<'a> {
|
|||||||
signed_url.push_str("?Expires=");
|
signed_url.push_str("?Expires=");
|
||||||
signed_url.push_str(expire_secs.to_string().as_str());
|
signed_url.push_str(expire_secs.to_string().as_str());
|
||||||
signed_url.push_str("&OSSAccessKeyId=");
|
signed_url.push_str("&OSSAccessKeyId=");
|
||||||
signed_url.push_str(&urlencoding::encode(self.access_key_id));
|
signed_url.push_str(&urlencoding::encode(&self.access_key_id));
|
||||||
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, &self.security_token);
|
||||||
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()));
|
||||||
|
|
||||||
|
if let Some(security_token) = &self.security_token {
|
||||||
|
signed_url.push_str("&security-token=");
|
||||||
|
signed_url.push_str(&urlencoding::encode(security_token));
|
||||||
|
}
|
||||||
|
|
||||||
signed_url
|
signed_url
|
||||||
}
|
}
|
||||||
|
|
||||||
// SAFE? may these codes cause memory leak?
|
|
||||||
fn string_to_a_str(s: &str) -> &'a str {
|
|
||||||
Box::leak(s.to_owned().into_boxed_str())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_to_be_signed(verb: &str, expire_secs: u64, bucket_name: &str, key: &str) -> String {
|
fn get_to_be_signed(verb: &str, expire_secs: u64, bucket_name: &str, key: &str, security_token: &Option<String>) -> String {
|
||||||
let mut to_be_signed = String::with_capacity(512);
|
let mut to_be_signed = String::with_capacity(512);
|
||||||
to_be_signed.push_str(verb);
|
to_be_signed.push_str(verb);
|
||||||
to_be_signed.push_str("\n");
|
to_be_signed.push_str("\n");
|
||||||
@@ -195,6 +212,10 @@ fn get_to_be_signed(verb: &str, expire_secs: u64, bucket_name: &str, key: &str)
|
|||||||
to_be_signed.push_str(bucket_name);
|
to_be_signed.push_str(bucket_name);
|
||||||
to_be_signed.push_str("/");
|
to_be_signed.push_str("/");
|
||||||
to_be_signed.push_str(key);
|
to_be_signed.push_str(key);
|
||||||
|
if let Some(security_token) = security_token {
|
||||||
|
to_be_signed.push_str("?security-token=");
|
||||||
|
to_be_signed.push_str(security_token);
|
||||||
|
}
|
||||||
to_be_signed
|
to_be_signed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user