mirror of
https://github.com/minio/minio-rs.git
synced 2025-12-06 23:36:52 +08:00
Remove lifetime parameter from client (#48)
This change lets the Client struct take ownership of the Provider trait object so that we are remove the lifetime parameter from the Client. This change simplifies usage of the Client object. Without this it is difficult to pass the Client object to a thread.
This commit is contained in:
parent
526b2a81ab
commit
4676ae8a57
@ -677,7 +677,7 @@ impl<'a> PutObjectArgs<'a> {
|
|||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```no_run
|
||||||
/// use minio::s3::args::*;
|
/// use minio::s3::args::*;
|
||||||
/// use std::fs::File;
|
/// use std::fs::File;
|
||||||
/// let filename = "asiaphotos-2015.zip";
|
/// let filename = "asiaphotos-2015.zip";
|
||||||
@ -2632,7 +2632,7 @@ impl<'a> UploadObjectArgs<'a> {
|
|||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```no_run
|
||||||
/// use minio::s3::args::*;
|
/// use minio::s3::args::*;
|
||||||
/// let args = UploadObjectArgs::new("my-bucket", "my-object", "asiaphotos-2015.zip").unwrap();
|
/// let args = UploadObjectArgs::new("my-bucket", "my-object", "asiaphotos-2015.zip").unwrap();
|
||||||
/// ```
|
/// ```
|
||||||
|
|||||||
@ -40,6 +40,7 @@ use std::collections::{HashMap, VecDeque};
|
|||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
use std::sync::Arc;
|
||||||
use xmltree::Element;
|
use xmltree::Element;
|
||||||
|
|
||||||
fn url_decode(
|
fn url_decode(
|
||||||
@ -202,19 +203,19 @@ fn parse_list_objects_common_prefixes(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default)]
|
|
||||||
/// Simple Storage Service (aka S3) client to perform bucket and object operations.
|
/// Simple Storage Service (aka S3) client to perform bucket and object operations.
|
||||||
///
|
///
|
||||||
/// If credential provider is passed, all S3 operation requests are signed using AWS Signature
|
/// If credential provider is passed, all S3 operation requests are signed using
|
||||||
/// Version 4; else they are performed anonymously.
|
/// AWS Signature Version 4; else they are performed anonymously.
|
||||||
pub struct Client<'a> {
|
#[derive(Clone, Debug, Default)]
|
||||||
|
pub struct Client {
|
||||||
client: reqwest::Client,
|
client: reqwest::Client,
|
||||||
base_url: BaseUrl,
|
base_url: BaseUrl,
|
||||||
provider: Option<&'a (dyn Provider + Send + Sync)>,
|
provider: Option<Arc<Box<(dyn Provider + Send + Sync + 'static)>>>,
|
||||||
region_map: DashMap<String, String>,
|
region_map: DashMap<String, String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Client<'a> {
|
impl Client {
|
||||||
/// Returns a S3 client with given base URL.
|
/// Returns a S3 client with given base URL.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
@ -229,11 +230,11 @@ impl<'a> Client<'a> {
|
|||||||
/// "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG",
|
/// "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG",
|
||||||
/// None,
|
/// None,
|
||||||
/// );
|
/// );
|
||||||
/// let client = Client::new(base_url.clone(), Some(&static_provider), None, None).unwrap();
|
/// let client = Client::new(base_url.clone(), Some(Box::new(static_provider)), None, None).unwrap();
|
||||||
/// ```
|
/// ```
|
||||||
pub fn new(
|
pub fn new(
|
||||||
base_url: BaseUrl,
|
base_url: BaseUrl,
|
||||||
provider: Option<&(dyn Provider + Send + Sync)>,
|
provider: Option<Box<(dyn Provider + Send + Sync + 'static)>>,
|
||||||
ssl_cert_file: Option<String>,
|
ssl_cert_file: Option<String>,
|
||||||
ignore_cert_check: Option<bool>,
|
ignore_cert_check: Option<bool>,
|
||||||
) -> Result<Client, Error> {
|
) -> Result<Client, Error> {
|
||||||
@ -263,7 +264,7 @@ impl<'a> Client<'a> {
|
|||||||
Ok(Client {
|
Ok(Client {
|
||||||
client,
|
client,
|
||||||
base_url,
|
base_url,
|
||||||
provider,
|
provider: provider.map(|v| Arc::new(v)),
|
||||||
region_map: DashMap::new(),
|
region_map: DashMap::new(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -317,7 +318,7 @@ impl<'a> Client<'a> {
|
|||||||
let date = utc_now();
|
let date = utc_now();
|
||||||
headers.insert(String::from("x-amz-date"), to_amz_date(date));
|
headers.insert(String::from("x-amz-date"), to_amz_date(date));
|
||||||
|
|
||||||
if let Some(p) = self.provider {
|
if let Some(p) = &self.provider {
|
||||||
let creds = p.fetch();
|
let creds = p.fetch();
|
||||||
if creds.session_token.is_some() {
|
if creds.session_token.is_some() {
|
||||||
headers.insert(
|
headers.insert(
|
||||||
@ -797,7 +798,7 @@ impl<'a> Client<'a> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn calculate_part_count(
|
async fn calculate_part_count<'a>(
|
||||||
&self,
|
&self,
|
||||||
sources: &'a mut Vec<ComposeSource<'_>>,
|
sources: &'a mut Vec<ComposeSource<'_>>,
|
||||||
) -> Result<u16, Error> {
|
) -> Result<u16, Error> {
|
||||||
@ -2205,7 +2206,7 @@ impl<'a> Client<'a> {
|
|||||||
Some(args.object),
|
Some(args.object),
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
if let Some(p) = self.provider {
|
if let Some(p) = &self.provider {
|
||||||
let creds = p.fetch();
|
let creds = p.fetch();
|
||||||
if let Some(t) = creds.session_token {
|
if let Some(t) = creds.session_token {
|
||||||
query_params.insert(String::from("X-Amz-Security-Token"), t);
|
query_params.insert(String::from("X-Amz-Security-Token"), t);
|
||||||
@ -2251,7 +2252,7 @@ impl<'a> Client<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let region = self.get_region(policy.bucket, policy.region).await?;
|
let region = self.get_region(policy.bucket, policy.region).await?;
|
||||||
let creds = self.provider.unwrap().fetch();
|
let creds = self.provider.as_ref().unwrap().fetch();
|
||||||
policy.form_data(
|
policy.form_data(
|
||||||
creds.access_key,
|
creds.access_key,
|
||||||
creds.secret_key,
|
creds.secret_key,
|
||||||
|
|||||||
@ -73,30 +73,30 @@ fn rand_object_name() -> String {
|
|||||||
Alphanumeric.sample_string(&mut rand::thread_rng(), 8)
|
Alphanumeric.sample_string(&mut rand::thread_rng(), 8)
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ClientTest<'a> {
|
struct ClientTest {
|
||||||
base_url: BaseUrl,
|
base_url: BaseUrl,
|
||||||
access_key: String,
|
access_key: String,
|
||||||
secret_key: String,
|
secret_key: String,
|
||||||
ignore_cert_check: Option<bool>,
|
ignore_cert_check: Option<bool>,
|
||||||
ssl_cert_file: Option<String>,
|
ssl_cert_file: Option<String>,
|
||||||
client: Client<'a>,
|
client: Client,
|
||||||
test_bucket: String,
|
test_bucket: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ClientTest<'_> {
|
impl ClientTest {
|
||||||
const SQS_ARN: &str = "arn:minio:sqs::miniojavatest:webhook";
|
const SQS_ARN: &str = "arn:minio:sqs::miniojavatest:webhook";
|
||||||
|
|
||||||
fn new(
|
fn new(
|
||||||
base_url: BaseUrl,
|
base_url: BaseUrl,
|
||||||
access_key: String,
|
access_key: String,
|
||||||
secret_key: String,
|
secret_key: String,
|
||||||
static_provider: &'a StaticProvider,
|
static_provider: StaticProvider,
|
||||||
ignore_cert_check: Option<bool>,
|
ignore_cert_check: Option<bool>,
|
||||||
ssl_cert_file: Option<String>,
|
ssl_cert_file: Option<String>,
|
||||||
) -> ClientTest<'a> {
|
) -> ClientTest {
|
||||||
let client = Client::new(
|
let client = Client::new(
|
||||||
base_url.clone(),
|
base_url.clone(),
|
||||||
Some(static_provider),
|
Some(Box::new(static_provider)),
|
||||||
ssl_cert_file.as_ref().cloned(),
|
ssl_cert_file.as_ref().cloned(),
|
||||||
ignore_cert_check,
|
ignore_cert_check,
|
||||||
)
|
)
|
||||||
@ -539,7 +539,7 @@ impl<'a> ClientTest<'_> {
|
|||||||
let static_provider = StaticProvider::new(&access_key, &secret_key, None);
|
let static_provider = StaticProvider::new(&access_key, &secret_key, None);
|
||||||
let client = Client::new(
|
let client = Client::new(
|
||||||
base_url,
|
base_url,
|
||||||
Some(&static_provider),
|
Some(Box::new(static_provider)),
|
||||||
ssl_cert_file,
|
ssl_cert_file,
|
||||||
ignore_cert_check,
|
ignore_cert_check,
|
||||||
)
|
)
|
||||||
@ -1166,7 +1166,7 @@ async fn s3_tests() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|||||||
base_url,
|
base_url,
|
||||||
access_key,
|
access_key,
|
||||||
secret_key,
|
secret_key,
|
||||||
&static_provider,
|
static_provider,
|
||||||
Some(ignore_cert_check),
|
Some(ignore_cert_check),
|
||||||
ssl_cert_file,
|
ssl_cert_file,
|
||||||
);
|
);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user