Parse region from xml resp for get_bucket_location

This commit is contained in:
Aditya Manthramurthy 2019-05-30 15:13:35 -07:00
parent f310d96d37
commit d91cdcf02c
4 changed files with 17 additions and 27 deletions

View File

@ -5,10 +5,11 @@ authors = ["Aditya Manthramurthy <aditya.mmy@gmail.com>"]
edition = "2018" edition = "2018"
[dependencies] [dependencies]
bytes = "0.4.12"
futures = "0.1.27"
http = "0.1.17"
hyper = "0.12.28" hyper = "0.12.28"
hyper-tls = "0.3.2"
ring = "0.14.6" ring = "0.14.6"
time = "0.1.42" time = "0.1.42"
http = "0.1.17" xml-rs = "0.8.0"
hyper-tls = "0.3.2"
futures = "0.1.27"
bytes = "0.4.12"

View File

@ -15,10 +15,10 @@ fn get_local_default_server() -> minio::Client {
fn main() { fn main() {
rt::run(rt::lazy(|| { rt::run(rt::lazy(|| {
let c = get_local_default_server(); // let c = get_local_default_server();
// let c = minio::Client::get_play_client(); let c = minio::Client::get_play_client();
c.get_bucket_location("txp") c.get_bucket_location("txp")
.map(|res| println!("{}", res)) .map(|res| println!("{}", res.to_string()))
.map_err(|err| println!("{:?}", err)) .map_err(|err| println!("{:?}", err))
})); }));
} }

View File

@ -1,5 +1,7 @@
mod api; mod api;
mod sign; mod sign;
mod types;
mod xml;
use bytes::Bytes; use bytes::Bytes;
use futures::future::{self, Future}; use futures::future::{self, Future};
@ -10,10 +12,10 @@ use hyper::{body::Body, client, header, header::HeaderMap, Method, Request, Resp
use hyper_tls::HttpsConnector; use hyper_tls::HttpsConnector;
use std::collections::HashMap; use std::collections::HashMap;
use std::env; use std::env;
use std::sync::Arc;
use std::{string, string::String}; use std::{string, string::String};
use time; use time;
use time::Tm; use time::Tm;
use types::{Err, Region};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Credentials { pub struct Credentials {
@ -40,19 +42,6 @@ impl Credentials {
} }
} }
pub type Region = String;
#[derive(Debug)]
pub enum Err {
InvalidUrl(String),
InvalidEnv(String),
HttpErr(http::Error),
HyperErr(hyper::Error),
FailStatusCodeErr(hyper::StatusCode, Bytes),
Utf8DecodingErr(string::FromUtf8Error),
RawSvcErr(hyper::StatusCode, Response<Body>),
}
#[derive(Clone)] #[derive(Clone)]
enum ConnClient { enum ConnClient {
HttpCC(client::Client<client::HttpConnector, Body>), HttpCC(client::Client<client::HttpConnector, Body>),
@ -87,7 +76,7 @@ impl Client {
} else { } else {
Ok(Client { Ok(Client {
server: s.clone(), server: s.clone(),
region: String::from(""), region: Region::empty(),
conn_client: if s.scheme_str() == Some("http") { conn_client: if s.scheme_str() == Some("http") {
ConnClient::HttpCC(client::Client::new()) ConnClient::HttpCC(client::Client::new())
} else { } else {
@ -128,7 +117,7 @@ impl Client {
pub fn get_play_client() -> Client { pub fn get_play_client() -> Client {
Client { Client {
server: "https://play.min.io:9000".parse::<Uri>().unwrap(), server: "https://play.min.io:9000".parse::<Uri>().unwrap(),
region: String::from(""), region: Region::empty(),
conn_client: { conn_client: {
let https = HttpsConnector::new(4).unwrap(); let https = HttpsConnector::new(4).unwrap();
ConnClient::HttpsCC(client::Client::builder().build::<_, hyper::Body>(https)) ConnClient::HttpsCC(client::Client::builder().build::<_, hyper::Body>(https))
@ -140,7 +129,7 @@ impl Client {
} }
} }
pub fn get_bucket_location(&self, b: &str) -> impl Future<Item = String, Error = Err> { pub fn get_bucket_location(&self, b: &str) -> impl Future<Item = Region, Error = Err> {
let mut qp = HashMap::new(); let mut qp = HashMap::new();
qp.insert("location".to_string(), None); qp.insert("location".to_string(), None);
let mut hmap = HeaderMap::new(); let mut hmap = HeaderMap::new();
@ -170,7 +159,7 @@ impl Client {
resp.into_body() resp.into_body()
.concat2() .concat2()
.map_err(|err| Err::HyperErr(err)) .map_err(|err| Err::HyperErr(err))
.and_then(move |chunk| b2s(chunk.into_bytes())) .and_then(move |chunk| xml::parse_bucket_location(chunk.into_bytes()))
}) })
} }
} }

View File

@ -17,7 +17,7 @@ fn aws_format_date(t: &Tm) -> String {
fn mk_scope(t: &Tm, r: &minio::Region) -> String { fn mk_scope(t: &Tm, r: &minio::Region) -> String {
let scope_time = t.strftime("%Y%m%d").unwrap().to_string(); let scope_time = t.strftime("%Y%m%d").unwrap().to_string();
format!("{}/{}/s3/aws4_request", scope_time, r) format!("{}/{}/s3/aws4_request", scope_time, r.to_string())
} }
// Returns list of SORTED headers that will be signed. TODO: verify // Returns list of SORTED headers that will be signed. TODO: verify
@ -162,7 +162,7 @@ pub fn sign_v4(r: &minio::S3Req, c: &minio::Client) -> Vec<(HeaderName, HeaderVa
println!("canonicalreq: {}", cr); println!("canonicalreq: {}", cr);
let s2s = string_to_sign(&r.ts, &scope, &cr); let s2s = string_to_sign(&r.ts, &scope, &cr);
println!("s2s: {}", s2s); println!("s2s: {}", s2s);
let skey = get_signing_key(&r.ts, &c.region, &creds.secret_key); let skey = get_signing_key(&r.ts, &c.region.to_string(), &creds.secret_key);
println!("skey: {:?}", skey); println!("skey: {:?}", skey);
let signature = compute_sign(&s2s, &skey); let signature = compute_sign(&s2s, &skey);
println!("sign: {}", signature); println!("sign: {}", signature);