mirror of
https://github.com/minio/minio-rs.git
synced 2025-12-06 15:26:51 +08:00
Add more APIs (#25)
* {set,get,delete}_bucket_notification
* {set,get,delete}_bucket_policy
* {set,get,delete}_bucket_replication
* {set,get,delete}_bucket_tags
* {set,get,delete}_object_lock_config
* {set,get,delete}_object_tags
* {set,get}_bucket_versioning
* {set,get}_object_retention
* get_presigned_object_url
* get_presigned_post_form_data
* {upload,download}_object
Signed-off-by: Bala.FA <bala@minio.io>
This commit is contained in:
parent
5fea81d68d
commit
67d92a3427
64
README.md
64
README.md
@ -1,6 +1,62 @@
|
||||
# MinIO Rust SDK for Amazon S3 Compatible Cloud Storage [](https://slack.min.io)
|
||||
# MinIO Rust SDK for Amazon S3 Compatible Cloud Storage [](https://slack.min.io) [](https://sourcegraph.com/github.com/minio/minio-rs?badge) [](https://github.com/minio/minio-rs/blob/master/LICENSE)
|
||||
|
||||
MinIO Rust SDK is Simple Storage Service (aka S3) client to perform bucket and object operations to any Amazon S3 compatible object storage service.
|
||||
|
||||
For a complete list of APIs and examples, please take a look at the [MinIO Rust Client API Reference](https://minio-rs.min.io/)
|
||||
|
||||
## Example:: file-uploader.rs
|
||||
```rust
|
||||
use minio::s3::args::{BucketExistsArgs, MakeBucketArgs, UploadObjectArgs};
|
||||
use minio::s3::client::Client;
|
||||
use minio::s3::creds::StaticProvider;
|
||||
use minio::s3::http::BaseUrl;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
let mut base_url = BaseUrl::from_string("play.min.io").unwrap();
|
||||
base_url.https = true;
|
||||
|
||||
let static_provider = StaticProvider::new(
|
||||
"Q3AM3UQ867SPQQA43P2F",
|
||||
"zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG",
|
||||
None,
|
||||
);
|
||||
|
||||
let mut client = Client::new(base_url.clone(), Some(static_provider));
|
||||
|
||||
let bucket_name = "asiatrip";
|
||||
|
||||
// Check 'asiatrip' bucket exist or not.
|
||||
let exists = client
|
||||
.bucket_exists(&BucketExistsArgs::new(&bucket_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// Make 'asiatrip' bucket if not exist.
|
||||
if !exist {
|
||||
client
|
||||
.make_bucket(&MakeBucketArgs::new(&bucket_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
// Upload '/home/user/Photos/asiaphotos.zip' as object name
|
||||
// 'asiaphotos-2015.zip' to bucket 'asiatrip'.
|
||||
client
|
||||
.upload_object(
|
||||
&mut UploadObjectArgs::new(
|
||||
&bucket_name,
|
||||
"asiaphotos-2015.zip",
|
||||
"/home/user/Photos/asiaphotos.zip",
|
||||
)
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
println!("'/home/user/Photos/asiaphotos.zip' is successfully uploaded as object 'asiaphotos-2015.zip' to bucket 'asiatrip'.");
|
||||
}
|
||||
```
|
||||
|
||||
## License
|
||||
MinIO Rust SDK is distributed under the terms of the Apache 2.0 license.
|
||||
|
||||
See [LICENSE](LICENSE) for details.
|
||||
This SDK is distributed under the [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0), see [LICENSE](https://github.com/minio/minio-rs/blob/master/LICENSE) for more information.
|
||||
|
||||
611
src/s3/args.rs
611
src/s3/args.rs
@ -14,27 +14,34 @@
|
||||
// limitations under the License.
|
||||
|
||||
use crate::s3::error::Error;
|
||||
use crate::s3::signer::post_presign_v4;
|
||||
use crate::s3::sse::{Sse, SseCustomerKey};
|
||||
use crate::s3::types::{
|
||||
DeleteObject, Directive, Item, LifecycleConfig, NotificationRecords, Part, Retention,
|
||||
SelectRequest, SseConfig,
|
||||
DeleteObject, Directive, Item, LifecycleConfig, NotificationConfig, NotificationRecords,
|
||||
ObjectLockConfig, Part, ReplicationConfig, Retention, RetentionMode, SelectRequest, SseConfig,
|
||||
};
|
||||
use crate::s3::utils::{
|
||||
check_bucket_name, merge, to_http_header_value, to_iso8601utc, urlencode, Multimap, UtcTime,
|
||||
b64encode, check_bucket_name, merge, to_amz_date, to_http_header_value, to_iso8601utc,
|
||||
to_signer_date, urlencode, utc_now, Multimap, UtcTime,
|
||||
};
|
||||
use derivative::Derivative;
|
||||
use hyper::http::Method;
|
||||
use serde_json::json;
|
||||
use serde_json::Value;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub const MIN_PART_SIZE: usize = 5_242_880; // 5 MiB
|
||||
pub const MAX_PART_SIZE: usize = 5_368_709_120; // 5 GiB
|
||||
pub const MAX_OBJECT_SIZE: usize = 5_497_558_138_880; // 5 TiB
|
||||
pub const MAX_MULTIPART_COUNT: u16 = 10_000;
|
||||
pub const DEFAULT_EXPIRY_SECONDS: u32 = 604_800; // 7 days
|
||||
|
||||
fn object_write_args_headers(
|
||||
extra_headers: Option<&Multimap>,
|
||||
headers: Option<&Multimap>,
|
||||
user_metadata: Option<&Multimap>,
|
||||
sse: Option<&dyn Sse>,
|
||||
tags: Option<&std::collections::HashMap<String, String>>,
|
||||
tags: Option<&HashMap<String, String>>,
|
||||
retention: Option<&Retention>,
|
||||
legal_hold: bool,
|
||||
) -> Multimap {
|
||||
@ -397,7 +404,7 @@ pub struct PutObjectApiArgs<'a> {
|
||||
pub headers: Option<&'a Multimap>,
|
||||
pub user_metadata: Option<&'a Multimap>,
|
||||
pub sse: Option<&'a dyn Sse>,
|
||||
pub tags: Option<&'a std::collections::HashMap<String, String>>,
|
||||
pub tags: Option<&'a HashMap<String, String>>,
|
||||
pub retention: Option<&'a Retention>,
|
||||
pub legal_hold: bool,
|
||||
pub data: &'a [u8],
|
||||
@ -458,7 +465,7 @@ pub struct UploadPartArgs<'a> {
|
||||
pub headers: Option<&'a Multimap>,
|
||||
pub user_metadata: Option<&'a Multimap>,
|
||||
pub sse: Option<&'a dyn Sse>,
|
||||
pub tags: Option<&'a std::collections::HashMap<String, String>>,
|
||||
pub tags: Option<&'a HashMap<String, String>>,
|
||||
pub retention: Option<&'a Retention>,
|
||||
pub legal_hold: bool,
|
||||
pub upload_id: &'a str,
|
||||
@ -534,7 +541,7 @@ pub struct PutObjectArgs<'a> {
|
||||
pub headers: Option<&'a Multimap>,
|
||||
pub user_metadata: Option<&'a Multimap>,
|
||||
pub sse: Option<&'a dyn Sse>,
|
||||
pub tags: Option<&'a std::collections::HashMap<String, String>>,
|
||||
pub tags: Option<&'a HashMap<String, String>>,
|
||||
pub retention: Option<&'a Retention>,
|
||||
pub legal_hold: bool,
|
||||
pub object_size: Option<usize>,
|
||||
@ -1090,7 +1097,7 @@ pub struct CopyObjectArgs<'a> {
|
||||
pub headers: Option<&'a Multimap>,
|
||||
pub user_metadata: Option<&'a Multimap>,
|
||||
pub sse: Option<&'a dyn Sse>,
|
||||
pub tags: Option<&'a std::collections::HashMap<String, String>>,
|
||||
pub tags: Option<&'a HashMap<String, String>>,
|
||||
pub retention: Option<&'a Retention>,
|
||||
pub legal_hold: bool,
|
||||
pub source: CopySource<'a>,
|
||||
@ -1297,7 +1304,7 @@ pub struct ComposeObjectArgs<'a> {
|
||||
pub headers: Option<&'a Multimap>,
|
||||
pub user_metadata: Option<&'a Multimap>,
|
||||
pub sse: Option<&'a dyn Sse>,
|
||||
pub tags: Option<&'a std::collections::HashMap<String, String>>,
|
||||
pub tags: Option<&'a HashMap<String, String>>,
|
||||
pub retention: Option<&'a Retention>,
|
||||
pub legal_hold: bool,
|
||||
pub sources: &'a mut Vec<ComposeSource<'a>>,
|
||||
@ -1393,3 +1400,589 @@ pub struct SetBucketLifecycleArgs<'a> {
|
||||
pub bucket: &'a str,
|
||||
pub config: &'a LifecycleConfig,
|
||||
}
|
||||
|
||||
pub type DeleteBucketNotificationArgs<'a> = BucketArgs<'a>;
|
||||
|
||||
pub type GetBucketNotificationArgs<'a> = BucketArgs<'a>;
|
||||
|
||||
pub struct SetBucketNotificationArgs<'a> {
|
||||
pub extra_headers: Option<&'a Multimap>,
|
||||
pub extra_query_params: Option<&'a Multimap>,
|
||||
pub region: Option<&'a str>,
|
||||
pub bucket: &'a str,
|
||||
pub config: &'a NotificationConfig,
|
||||
}
|
||||
|
||||
impl<'a> SetBucketNotificationArgs<'a> {
|
||||
pub fn new(
|
||||
bucket_name: &'a str,
|
||||
config: &'a NotificationConfig,
|
||||
) -> Result<SetBucketNotificationArgs<'a>, Error> {
|
||||
check_bucket_name(bucket_name, true)?;
|
||||
|
||||
Ok(SetBucketNotificationArgs {
|
||||
extra_headers: None,
|
||||
extra_query_params: None,
|
||||
region: None,
|
||||
bucket: bucket_name,
|
||||
config: config,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub type DeleteBucketPolicyArgs<'a> = BucketArgs<'a>;
|
||||
|
||||
pub type GetBucketPolicyArgs<'a> = BucketArgs<'a>;
|
||||
|
||||
pub struct SetBucketPolicyArgs<'a> {
|
||||
pub extra_headers: Option<&'a Multimap>,
|
||||
pub extra_query_params: Option<&'a Multimap>,
|
||||
pub region: Option<&'a str>,
|
||||
pub bucket: &'a str,
|
||||
pub config: &'a str,
|
||||
}
|
||||
|
||||
impl<'a> SetBucketPolicyArgs<'a> {
|
||||
pub fn new(bucket_name: &'a str, config: &'a str) -> Result<SetBucketPolicyArgs<'a>, Error> {
|
||||
check_bucket_name(bucket_name, true)?;
|
||||
|
||||
Ok(SetBucketPolicyArgs {
|
||||
extra_headers: None,
|
||||
extra_query_params: None,
|
||||
region: None,
|
||||
bucket: bucket_name,
|
||||
config: config,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub type DeleteBucketReplicationArgs<'a> = BucketArgs<'a>;
|
||||
|
||||
pub type GetBucketReplicationArgs<'a> = BucketArgs<'a>;
|
||||
|
||||
pub struct SetBucketReplicationArgs<'a> {
|
||||
pub extra_headers: Option<&'a Multimap>,
|
||||
pub extra_query_params: Option<&'a Multimap>,
|
||||
pub region: Option<&'a str>,
|
||||
pub bucket: &'a str,
|
||||
pub config: &'a ReplicationConfig,
|
||||
}
|
||||
|
||||
pub type DeleteBucketTagsArgs<'a> = BucketArgs<'a>;
|
||||
|
||||
pub type GetBucketTagsArgs<'a> = BucketArgs<'a>;
|
||||
|
||||
pub struct SetBucketTagsArgs<'a> {
|
||||
pub extra_headers: Option<&'a Multimap>,
|
||||
pub extra_query_params: Option<&'a Multimap>,
|
||||
pub region: Option<&'a str>,
|
||||
pub bucket: &'a str,
|
||||
pub tags: &'a HashMap<String, String>,
|
||||
}
|
||||
|
||||
impl<'a> SetBucketTagsArgs<'a> {
|
||||
pub fn new(
|
||||
bucket_name: &'a str,
|
||||
tags: &'a HashMap<String, String>,
|
||||
) -> Result<SetBucketTagsArgs<'a>, Error> {
|
||||
check_bucket_name(bucket_name, true)?;
|
||||
|
||||
Ok(SetBucketTagsArgs {
|
||||
extra_headers: None,
|
||||
extra_query_params: None,
|
||||
region: None,
|
||||
bucket: bucket_name,
|
||||
tags: tags,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub type GetBucketVersioningArgs<'a> = BucketArgs<'a>;
|
||||
|
||||
pub struct SetBucketVersioningArgs<'a> {
|
||||
pub extra_headers: Option<&'a Multimap>,
|
||||
pub extra_query_params: Option<&'a Multimap>,
|
||||
pub region: Option<&'a str>,
|
||||
pub bucket: &'a str,
|
||||
pub status: bool,
|
||||
pub mfa_delete: Option<bool>,
|
||||
}
|
||||
|
||||
impl<'a> SetBucketVersioningArgs<'a> {
|
||||
pub fn new(bucket_name: &'a str, status: bool) -> Result<SetBucketVersioningArgs<'a>, Error> {
|
||||
check_bucket_name(bucket_name, true)?;
|
||||
|
||||
Ok(SetBucketVersioningArgs {
|
||||
extra_headers: None,
|
||||
extra_query_params: None,
|
||||
region: None,
|
||||
bucket: bucket_name,
|
||||
status: status,
|
||||
mfa_delete: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub type DeleteObjectLockConfigArgs<'a> = BucketArgs<'a>;
|
||||
|
||||
pub type GetObjectLockConfigArgs<'a> = BucketArgs<'a>;
|
||||
|
||||
pub struct SetObjectLockConfigArgs<'a> {
|
||||
pub extra_headers: Option<&'a Multimap>,
|
||||
pub extra_query_params: Option<&'a Multimap>,
|
||||
pub region: Option<&'a str>,
|
||||
pub bucket: &'a str,
|
||||
pub config: &'a ObjectLockConfig,
|
||||
}
|
||||
|
||||
impl<'a> SetObjectLockConfigArgs<'a> {
|
||||
pub fn new(
|
||||
bucket_name: &'a str,
|
||||
config: &'a ObjectLockConfig,
|
||||
) -> Result<SetObjectLockConfigArgs<'a>, Error> {
|
||||
check_bucket_name(bucket_name, true)?;
|
||||
|
||||
Ok(SetObjectLockConfigArgs {
|
||||
extra_headers: None,
|
||||
extra_query_params: None,
|
||||
region: None,
|
||||
bucket: bucket_name,
|
||||
config: config,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub type GetObjectRetentionArgs<'a> = ObjectVersionArgs<'a>;
|
||||
|
||||
pub struct SetObjectRetentionArgs<'a> {
|
||||
pub extra_headers: Option<&'a Multimap>,
|
||||
pub extra_query_params: Option<&'a Multimap>,
|
||||
pub region: Option<&'a str>,
|
||||
pub bucket: &'a str,
|
||||
pub object: &'a str,
|
||||
pub version_id: Option<&'a str>,
|
||||
pub bypass_governance_mode: bool,
|
||||
pub retention_mode: Option<RetentionMode>,
|
||||
pub retain_until_date: Option<UtcTime>,
|
||||
}
|
||||
|
||||
impl<'a> SetObjectRetentionArgs<'a> {
|
||||
pub fn new(
|
||||
bucket_name: &'a str,
|
||||
object_name: &'a str,
|
||||
) -> Result<SetObjectRetentionArgs<'a>, Error> {
|
||||
check_bucket_name(bucket_name, true)?;
|
||||
|
||||
if object_name.is_empty() {
|
||||
return Err(Error::InvalidObjectName(String::from(
|
||||
"object name cannot be empty",
|
||||
)));
|
||||
}
|
||||
|
||||
Ok(SetObjectRetentionArgs {
|
||||
extra_headers: None,
|
||||
extra_query_params: None,
|
||||
region: None,
|
||||
bucket: bucket_name,
|
||||
object: object_name,
|
||||
version_id: None,
|
||||
bypass_governance_mode: false,
|
||||
retention_mode: None,
|
||||
retain_until_date: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub type DeleteObjectTagsArgs<'a> = ObjectVersionArgs<'a>;
|
||||
|
||||
pub type GetObjectTagsArgs<'a> = ObjectVersionArgs<'a>;
|
||||
|
||||
pub struct SetObjectTagsArgs<'a> {
|
||||
pub extra_headers: Option<&'a Multimap>,
|
||||
pub extra_query_params: Option<&'a Multimap>,
|
||||
pub region: Option<&'a str>,
|
||||
pub bucket: &'a str,
|
||||
pub object: &'a str,
|
||||
pub version_id: Option<&'a str>,
|
||||
pub tags: &'a HashMap<String, String>,
|
||||
}
|
||||
|
||||
impl<'a> SetObjectTagsArgs<'a> {
|
||||
pub fn new(
|
||||
bucket_name: &'a str,
|
||||
object_name: &'a str,
|
||||
tags: &'a HashMap<String, String>,
|
||||
) -> Result<SetObjectTagsArgs<'a>, Error> {
|
||||
check_bucket_name(bucket_name, true)?;
|
||||
|
||||
if object_name.is_empty() {
|
||||
return Err(Error::InvalidObjectName(String::from(
|
||||
"object name cannot be empty",
|
||||
)));
|
||||
}
|
||||
|
||||
Ok(SetObjectTagsArgs {
|
||||
extra_headers: None,
|
||||
extra_query_params: None,
|
||||
region: None,
|
||||
bucket: bucket_name,
|
||||
object: object_name,
|
||||
version_id: None,
|
||||
tags: tags,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct GetPresignedObjectUrlArgs<'a> {
|
||||
pub extra_query_params: Option<&'a Multimap>,
|
||||
pub region: Option<&'a str>,
|
||||
pub bucket: &'a str,
|
||||
pub object: &'a str,
|
||||
pub version_id: Option<&'a str>,
|
||||
pub method: Method,
|
||||
pub expiry_seconds: Option<u32>,
|
||||
pub request_time: Option<UtcTime>,
|
||||
}
|
||||
|
||||
impl<'a> GetPresignedObjectUrlArgs<'a> {
|
||||
pub fn new(
|
||||
bucket_name: &'a str,
|
||||
object_name: &'a str,
|
||||
method: Method,
|
||||
) -> Result<GetPresignedObjectUrlArgs<'a>, Error> {
|
||||
check_bucket_name(bucket_name, true)?;
|
||||
|
||||
if object_name.is_empty() {
|
||||
return Err(Error::InvalidObjectName(String::from(
|
||||
"object name cannot be empty",
|
||||
)));
|
||||
}
|
||||
|
||||
Ok(GetPresignedObjectUrlArgs {
|
||||
extra_query_params: None,
|
||||
region: None,
|
||||
bucket: bucket_name,
|
||||
object: object_name,
|
||||
version_id: None,
|
||||
method: method,
|
||||
expiry_seconds: Some(DEFAULT_EXPIRY_SECONDS),
|
||||
request_time: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PostPolicy<'a> {
|
||||
pub region: Option<&'a str>,
|
||||
pub bucket: &'a str,
|
||||
|
||||
expiration: &'a UtcTime,
|
||||
eq_conditions: HashMap<String, String>,
|
||||
starts_with_conditions: HashMap<String, String>,
|
||||
lower_limit: Option<usize>,
|
||||
upper_limit: Option<usize>,
|
||||
}
|
||||
|
||||
impl<'a> PostPolicy<'a> {
|
||||
const EQ: &str = "eq";
|
||||
const STARTS_WITH: &str = "starts-with";
|
||||
const ALGORITHM: &str = "AWS4-HMAC-SHA256";
|
||||
|
||||
pub fn new(bucket_name: &'a str, expiration: &'a UtcTime) -> Result<PostPolicy<'a>, Error> {
|
||||
check_bucket_name(bucket_name, true)?;
|
||||
|
||||
Ok(PostPolicy {
|
||||
region: None,
|
||||
bucket: bucket_name,
|
||||
expiration: expiration,
|
||||
eq_conditions: HashMap::new(),
|
||||
starts_with_conditions: HashMap::new(),
|
||||
lower_limit: None,
|
||||
upper_limit: None,
|
||||
})
|
||||
}
|
||||
|
||||
fn trim_dollar(value: &str) -> String {
|
||||
let mut s = value.to_string();
|
||||
if s.starts_with("$") {
|
||||
s.remove(0);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
fn is_reserved_element(element: &str) -> bool {
|
||||
return element == "bucket"
|
||||
|| element == "x-amz-algorithm"
|
||||
|| element == "x-amz-credential"
|
||||
|| element == "x-amz-date"
|
||||
|| element == "policy"
|
||||
|| element == "x-amz-signature";
|
||||
}
|
||||
|
||||
fn get_credential_string(access_key: &String, date: &UtcTime, region: &String) -> String {
|
||||
return format!(
|
||||
"{}/{}/{}/s3/aws4_request",
|
||||
access_key,
|
||||
to_signer_date(*date),
|
||||
region
|
||||
);
|
||||
}
|
||||
|
||||
pub fn add_equals_condition(&mut self, element: &str, value: &str) -> Result<(), Error> {
|
||||
if element.is_empty() {
|
||||
return Err(Error::PostPolicyError(format!(
|
||||
"condition element cannot be empty"
|
||||
)));
|
||||
}
|
||||
|
||||
let v = PostPolicy::trim_dollar(element);
|
||||
if v == "success_action_redirect" || v == "redirect" || v == "content-length-range" {
|
||||
return Err(Error::PostPolicyError(format!(
|
||||
"{} is unsupported for equals condition",
|
||||
element
|
||||
)));
|
||||
}
|
||||
|
||||
if PostPolicy::is_reserved_element(&v.as_str()) {
|
||||
return Err(Error::PostPolicyError(format!("{} cannot set", element)));
|
||||
}
|
||||
|
||||
self.eq_conditions.insert(v, value.to_string());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn remove_equals_condition(&mut self, element: &str) {
|
||||
self.eq_conditions.remove(element);
|
||||
}
|
||||
|
||||
pub fn add_starts_with_condition(&mut self, element: &str, value: &str) -> Result<(), Error> {
|
||||
if element.is_empty() {
|
||||
return Err(Error::PostPolicyError(format!(
|
||||
"condition element cannot be empty"
|
||||
)));
|
||||
}
|
||||
|
||||
let v = PostPolicy::trim_dollar(element);
|
||||
if v == "success_action_status"
|
||||
|| v == "content-length-range"
|
||||
|| (v.starts_with("x-amz-") && v.starts_with("x-amz-meta-"))
|
||||
{
|
||||
return Err(Error::PostPolicyError(format!(
|
||||
"{} is unsupported for starts-with condition",
|
||||
element
|
||||
)));
|
||||
}
|
||||
|
||||
if PostPolicy::is_reserved_element(&v.as_str()) {
|
||||
return Err(Error::PostPolicyError(format!("{} cannot set", element)));
|
||||
}
|
||||
|
||||
self.starts_with_conditions
|
||||
.insert(v.clone(), value.to_string());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn remove_starts_with_condition(&mut self, element: &str) {
|
||||
self.starts_with_conditions.remove(element);
|
||||
}
|
||||
|
||||
pub fn add_content_length_range_condition(
|
||||
&mut self,
|
||||
lower_limit: usize,
|
||||
upper_limit: usize,
|
||||
) -> Result<(), Error> {
|
||||
if lower_limit > upper_limit {
|
||||
return Err(Error::PostPolicyError(format!(
|
||||
"lower limit cannot be greater than upper limit"
|
||||
)));
|
||||
}
|
||||
|
||||
self.lower_limit = Some(lower_limit);
|
||||
self.upper_limit = Some(upper_limit);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn remove_content_length_range_condition(&mut self) {
|
||||
self.lower_limit = None;
|
||||
self.upper_limit = None;
|
||||
}
|
||||
|
||||
pub fn form_data(
|
||||
&self,
|
||||
access_key: String,
|
||||
secret_key: String,
|
||||
session_token: Option<String>,
|
||||
region: String,
|
||||
) -> Result<HashMap<String, String>, Error> {
|
||||
if region.is_empty() {
|
||||
return Err(Error::PostPolicyError(format!("region cannot be empty")));
|
||||
}
|
||||
|
||||
if !self.eq_conditions.contains_key("key")
|
||||
&& !self.starts_with_conditions.contains_key("key")
|
||||
{
|
||||
return Err(Error::PostPolicyError(format!("key condition must be set")));
|
||||
}
|
||||
|
||||
let mut conditions: Vec<Value> = Vec::new();
|
||||
conditions.push(json!([PostPolicy::EQ, "$bucket", self.bucket]));
|
||||
for (key, value) in &self.eq_conditions {
|
||||
conditions.push(json!([PostPolicy::EQ, String::from("$") + &key, value]));
|
||||
}
|
||||
for (key, value) in &self.starts_with_conditions {
|
||||
conditions.push(json!([
|
||||
PostPolicy::STARTS_WITH,
|
||||
String::from("$") + &key,
|
||||
value
|
||||
]));
|
||||
}
|
||||
if self.lower_limit.is_some() && self.upper_limit.is_some() {
|
||||
conditions.push(json!([
|
||||
"content-length-range",
|
||||
self.lower_limit.unwrap(),
|
||||
self.upper_limit.unwrap()
|
||||
]));
|
||||
}
|
||||
|
||||
let date = utc_now();
|
||||
let credential = PostPolicy::get_credential_string(&access_key, &date, ®ion);
|
||||
let amz_date = to_amz_date(date);
|
||||
conditions.push(json!([
|
||||
PostPolicy::EQ,
|
||||
"$x-amz-algorithm",
|
||||
PostPolicy::ALGORITHM
|
||||
]));
|
||||
conditions.push(json!([PostPolicy::EQ, "$x-amz-credential", credential]));
|
||||
if let Some(v) = &session_token {
|
||||
conditions.push(json!([PostPolicy::EQ, "$x-amz-security-token", v]));
|
||||
}
|
||||
conditions.push(json!([PostPolicy::EQ, "$x-amz-date", amz_date]));
|
||||
|
||||
let policy = json!({
|
||||
"expiration": to_iso8601utc(*self.expiration),
|
||||
"conditions": conditions,
|
||||
});
|
||||
|
||||
let encoded_policy = b64encode(policy.to_string());
|
||||
let signature = post_presign_v4(&encoded_policy, &secret_key, date, ®ion);
|
||||
|
||||
let mut data: HashMap<String, String> = HashMap::new();
|
||||
data.insert(
|
||||
String::from("x-amz-algorithm"),
|
||||
String::from(PostPolicy::ALGORITHM),
|
||||
);
|
||||
data.insert(String::from("x-amz-credential"), credential);
|
||||
data.insert(String::from("x-amz-date"), amz_date);
|
||||
data.insert(String::from("policy"), encoded_policy);
|
||||
data.insert(String::from("x-amz-signature"), signature);
|
||||
if let Some(v) = session_token {
|
||||
data.insert(String::from("x-amz-security-token"), v);
|
||||
}
|
||||
|
||||
Ok(data)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DownloadObjectArgs<'a> {
|
||||
pub extra_headers: Option<&'a Multimap>,
|
||||
pub extra_query_params: Option<&'a Multimap>,
|
||||
pub region: Option<&'a str>,
|
||||
pub bucket: &'a str,
|
||||
pub object: &'a str,
|
||||
pub version_id: Option<&'a str>,
|
||||
pub ssec: Option<&'a SseCustomerKey>,
|
||||
pub filename: &'a str,
|
||||
pub overwrite: bool,
|
||||
}
|
||||
|
||||
impl<'a> DownloadObjectArgs<'a> {
|
||||
pub fn new(
|
||||
bucket_name: &'a str,
|
||||
object_name: &'a str,
|
||||
filename: &'a str,
|
||||
) -> Result<DownloadObjectArgs<'a>, Error> {
|
||||
check_bucket_name(bucket_name, true)?;
|
||||
|
||||
if object_name.is_empty() {
|
||||
return Err(Error::InvalidObjectName(String::from(
|
||||
"object name cannot be empty",
|
||||
)));
|
||||
}
|
||||
|
||||
Ok(DownloadObjectArgs {
|
||||
extra_headers: None,
|
||||
extra_query_params: None,
|
||||
region: None,
|
||||
bucket: bucket_name,
|
||||
object: object_name,
|
||||
version_id: None,
|
||||
ssec: None,
|
||||
filename: filename,
|
||||
overwrite: false,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct UploadObjectArgs<'a> {
|
||||
pub extra_headers: Option<&'a Multimap>,
|
||||
pub extra_query_params: Option<&'a Multimap>,
|
||||
pub region: Option<&'a str>,
|
||||
pub bucket: &'a str,
|
||||
pub object: &'a str,
|
||||
pub headers: Option<&'a Multimap>,
|
||||
pub user_metadata: Option<&'a Multimap>,
|
||||
pub sse: Option<&'a dyn Sse>,
|
||||
pub tags: Option<&'a HashMap<String, String>>,
|
||||
pub retention: Option<&'a Retention>,
|
||||
pub legal_hold: bool,
|
||||
pub object_size: Option<usize>,
|
||||
pub part_size: usize,
|
||||
pub part_count: i16,
|
||||
pub content_type: &'a str,
|
||||
pub filename: &'a str,
|
||||
}
|
||||
|
||||
impl<'a> UploadObjectArgs<'a> {
|
||||
pub fn new(
|
||||
bucket_name: &'a str,
|
||||
object_name: &'a str,
|
||||
filename: &'a str,
|
||||
) -> Result<UploadObjectArgs<'a>, Error> {
|
||||
check_bucket_name(bucket_name, true)?;
|
||||
|
||||
if object_name.is_empty() {
|
||||
return Err(Error::InvalidObjectName(String::from(
|
||||
"object name cannot be empty",
|
||||
)));
|
||||
}
|
||||
|
||||
let meta = std::fs::metadata(filename)?;
|
||||
if !meta.is_file() {
|
||||
return Err(Error::IOError(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
"not a file",
|
||||
)));
|
||||
}
|
||||
|
||||
let object_size = Some(meta.len() as usize);
|
||||
let (psize, part_count) = calc_part_info(object_size, None)?;
|
||||
|
||||
Ok(UploadObjectArgs {
|
||||
extra_headers: None,
|
||||
extra_query_params: None,
|
||||
region: None,
|
||||
bucket: bucket_name,
|
||||
object: object_name,
|
||||
headers: None,
|
||||
user_metadata: None,
|
||||
sse: None,
|
||||
tags: None,
|
||||
retention: None,
|
||||
legal_hold: false,
|
||||
object_size: object_size,
|
||||
part_size: psize,
|
||||
part_count: part_count,
|
||||
content_type: "application/octet-stream",
|
||||
filename: filename,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
1290
src/s3/client.rs
1290
src/s3/client.rs
File diff suppressed because it is too large
Load Diff
@ -102,6 +102,8 @@ pub enum Error {
|
||||
InvalidDateAndDays(String),
|
||||
InvalidLifecycleRuleId,
|
||||
InvalidFilter,
|
||||
PostPolicyError(String),
|
||||
InvalidObjectLockConfig(String),
|
||||
}
|
||||
|
||||
impl std::error::Error for Error {}
|
||||
@ -160,6 +162,8 @@ impl fmt::Display for Error {
|
||||
Error::InvalidDateAndDays(m) => write!(f, "Only one of date or days of {} must be set", m),
|
||||
Error::InvalidLifecycleRuleId => write!(f, "id must be exceed 255 characters"),
|
||||
Error::InvalidFilter => write!(f, "only one of And, Prefix or Tag must be provided"),
|
||||
Error::PostPolicyError(m) => write!(f, "{}", m),
|
||||
Error::InvalidObjectLockConfig(m) => write!(f, "{}", m),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -15,7 +15,8 @@
|
||||
|
||||
use crate::s3::error::Error;
|
||||
use crate::s3::types::{
|
||||
parse_legal_hold, Bucket, Item, LifecycleConfig, RetentionMode, SelectProgress, SseConfig,
|
||||
parse_legal_hold, Bucket, Item, LifecycleConfig, NotificationConfig, ObjectLockConfig,
|
||||
ReplicationConfig, RetentionMode, SelectProgress, SseConfig,
|
||||
};
|
||||
use crate::s3::utils::{
|
||||
copy_slice, crc32, from_http_header_value, from_iso8601utc, get_text, uint32, UtcTime,
|
||||
@ -91,6 +92,8 @@ pub type CopyObjectResponse = PutObjectApiResponse;
|
||||
|
||||
pub type ComposeObjectResponse = PutObjectApiResponse;
|
||||
|
||||
pub type UploadObjectResponse = PutObjectApiResponse;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct StatObjectResponse {
|
||||
pub headers: HeaderMap,
|
||||
@ -603,6 +606,7 @@ impl SelectObjectContentResponse {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ListenBucketNotificationResponse {
|
||||
pub headers: HeaderMap,
|
||||
pub region: String,
|
||||
@ -625,6 +629,7 @@ impl ListenBucketNotificationResponse {
|
||||
|
||||
pub type DeleteBucketEncryptionResponse = BucketResponse;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct GetBucketEncryptionResponse {
|
||||
pub headers: HeaderMap,
|
||||
pub region: String,
|
||||
@ -638,6 +643,7 @@ pub type EnableObjectLegalHoldResponse = ObjectResponse;
|
||||
|
||||
pub type DisableObjectLegalHoldResponse = ObjectResponse;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct IsObjectLegalHoldEnabledResponse {
|
||||
pub headers: HeaderMap,
|
||||
pub region: String,
|
||||
@ -649,6 +655,7 @@ pub struct IsObjectLegalHoldEnabledResponse {
|
||||
|
||||
pub type DeleteBucketLifecycleResponse = BucketResponse;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct GetBucketLifecycleResponse {
|
||||
pub headers: HeaderMap,
|
||||
pub region: String,
|
||||
@ -657,3 +664,119 @@ pub struct GetBucketLifecycleResponse {
|
||||
}
|
||||
|
||||
pub type SetBucketLifecycleResponse = BucketResponse;
|
||||
|
||||
pub type DeleteBucketNotificationResponse = BucketResponse;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct GetBucketNotificationResponse {
|
||||
pub headers: HeaderMap,
|
||||
pub region: String,
|
||||
pub bucket_name: String,
|
||||
pub config: NotificationConfig,
|
||||
}
|
||||
|
||||
pub type SetBucketNotificationResponse = BucketResponse;
|
||||
|
||||
pub type DeleteBucketPolicyResponse = BucketResponse;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct GetBucketPolicyResponse {
|
||||
pub headers: HeaderMap,
|
||||
pub region: String,
|
||||
pub bucket_name: String,
|
||||
pub config: String,
|
||||
}
|
||||
|
||||
pub type SetBucketPolicyResponse = BucketResponse;
|
||||
|
||||
pub type DeleteBucketReplicationResponse = BucketResponse;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct GetBucketReplicationResponse {
|
||||
pub headers: HeaderMap,
|
||||
pub region: String,
|
||||
pub bucket_name: String,
|
||||
pub config: ReplicationConfig,
|
||||
}
|
||||
|
||||
pub type SetBucketReplicationResponse = BucketResponse;
|
||||
|
||||
pub type DeleteBucketTagsResponse = BucketResponse;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct GetBucketTagsResponse {
|
||||
pub headers: HeaderMap,
|
||||
pub region: String,
|
||||
pub bucket_name: String,
|
||||
pub tags: std::collections::HashMap<String, String>,
|
||||
}
|
||||
|
||||
pub type SetBucketTagsResponse = BucketResponse;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct GetBucketVersioningResponse {
|
||||
pub headers: HeaderMap,
|
||||
pub region: String,
|
||||
pub bucket_name: String,
|
||||
pub status: Option<bool>,
|
||||
pub mfa_delete: Option<bool>,
|
||||
}
|
||||
|
||||
pub type SetBucketVersioningResponse = BucketResponse;
|
||||
|
||||
pub type DeleteObjectLockConfigResponse = BucketResponse;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct GetObjectLockConfigResponse {
|
||||
pub headers: HeaderMap,
|
||||
pub region: String,
|
||||
pub bucket_name: String,
|
||||
pub config: ObjectLockConfig,
|
||||
}
|
||||
|
||||
pub type SetObjectLockConfigResponse = BucketResponse;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct GetObjectRetentionResponse {
|
||||
pub headers: HeaderMap,
|
||||
pub region: String,
|
||||
pub bucket_name: String,
|
||||
pub object_name: String,
|
||||
pub version_id: Option<String>,
|
||||
pub retention_mode: Option<RetentionMode>,
|
||||
pub retain_until_date: Option<UtcTime>,
|
||||
}
|
||||
|
||||
pub type SetObjectRetentionResponse = ObjectResponse;
|
||||
|
||||
pub type DeleteObjectTagsResponse = ObjectResponse;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct GetObjectTagsResponse {
|
||||
pub headers: HeaderMap,
|
||||
pub region: String,
|
||||
pub bucket_name: String,
|
||||
pub object_name: String,
|
||||
pub version_id: Option<String>,
|
||||
pub tags: std::collections::HashMap<String, String>,
|
||||
}
|
||||
|
||||
pub type SetObjectTagsResponse = ObjectResponse;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct GetPresignedObjectUrlResponse {
|
||||
pub region: String,
|
||||
pub bucket_name: String,
|
||||
pub object_name: String,
|
||||
pub version_id: Option<String>,
|
||||
pub url: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct DownloadObjectResponse {
|
||||
pub headers: HeaderMap,
|
||||
pub region: String,
|
||||
pub bucket_name: String,
|
||||
pub object_name: String,
|
||||
pub version_id: Option<String>,
|
||||
}
|
||||
|
||||
951
src/s3/types.rs
951
src/s3/types.rs
File diff suppressed because it is too large
Load Diff
@ -262,15 +262,12 @@ pub fn get_text(element: &Element, tag: &str) -> Result<String, Error> {
|
||||
.to_string())
|
||||
}
|
||||
|
||||
pub fn get_option_text(element: &Element, tag: &str) -> Result<Option<String>, Error> {
|
||||
Ok(match element.get_child(tag) {
|
||||
Some(v) => Some(
|
||||
v.get_text()
|
||||
.ok_or(Error::XmlError(format!("text of <{}> tag not found", tag)))?
|
||||
.to_string(),
|
||||
),
|
||||
None => None,
|
||||
})
|
||||
pub fn get_option_text(element: &Element, tag: &str) -> Option<String> {
|
||||
if let Some(v) = element.get_child(tag) {
|
||||
return Some(v.get_text().unwrap_or_default().to_string());
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub fn get_default_text(element: &Element, tag: &str) -> String {
|
||||
|
||||
@ -3,10 +3,14 @@
|
||||
set -x
|
||||
set -e
|
||||
|
||||
wget --quiet https://dl.min.io/server/minio/release/linux-amd64/minio && \
|
||||
chmod +x minio && \
|
||||
mkdir -p /tmp/certs && \
|
||||
cp ./tests/public.crt ./tests/private.key /tmp/certs/ && \
|
||||
wget --quiet https://dl.min.io/server/minio/release/linux-amd64/minio
|
||||
chmod +x minio
|
||||
mkdir -p /tmp/certs
|
||||
cp ./tests/public.crt ./tests/private.key /tmp/certs/
|
||||
|
||||
(MINIO_CI_CD=true \
|
||||
MINIO_NOTIFY_WEBHOOK_ENABLE_miniojavatest=on \
|
||||
MINIO_NOTIFY_WEBHOOK_ENDPOINT_miniojavatest=http://example.org/ \
|
||||
./minio server /tmp/test-xl/{1...4}/ --certs-dir /tmp/certs/ &)
|
||||
|
||||
MINIO_CI_CD=true ./minio server /tmp/test-xl/{1...4}/ --certs-dir /tmp/certs/ &
|
||||
sleep 10
|
||||
|
||||
605
tests/tests.rs
605
tests/tests.rs
@ -14,9 +14,14 @@
|
||||
// limitations under the License.
|
||||
|
||||
use async_std::task;
|
||||
use chrono::Duration;
|
||||
use hyper::http::Method;
|
||||
use minio::s3::types::NotificationRecords;
|
||||
use rand::distributions::{Alphanumeric, DistString};
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::collections::HashMap;
|
||||
use std::io::BufReader;
|
||||
use std::{fs, io};
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
use minio::s3::args::*;
|
||||
@ -24,9 +29,11 @@ use minio::s3::client::Client;
|
||||
use minio::s3::creds::StaticProvider;
|
||||
use minio::s3::http::BaseUrl;
|
||||
use minio::s3::types::{
|
||||
CsvInputSerialization, CsvOutputSerialization, DeleteObject, FileHeaderInfo, QuoteFields,
|
||||
SelectRequest,
|
||||
CsvInputSerialization, CsvOutputSerialization, DeleteObject, FileHeaderInfo,
|
||||
NotificationConfig, ObjectLockConfig, PrefixFilterRule, QueueConfig, QuoteFields,
|
||||
RetentionMode, SelectRequest, SuffixFilterRule,
|
||||
};
|
||||
use minio::s3::utils::{to_iso8601utc, utc_now};
|
||||
|
||||
struct RandReader {
|
||||
size: usize,
|
||||
@ -77,6 +84,8 @@ struct ClientTest<'a> {
|
||||
}
|
||||
|
||||
impl<'a> ClientTest<'_> {
|
||||
const SQS_ARN: &str = "arn:minio:sqs::miniojavatest:webhook";
|
||||
|
||||
fn new(
|
||||
base_url: BaseUrl,
|
||||
access_key: String,
|
||||
@ -255,6 +264,89 @@ impl<'a> ClientTest<'_> {
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn get_hash(filename: &String) -> String {
|
||||
let mut hasher = Sha256::new();
|
||||
let mut file = fs::File::open(filename).unwrap();
|
||||
io::copy(&mut file, &mut hasher).unwrap();
|
||||
return format!("{:x}", hasher.finalize());
|
||||
}
|
||||
|
||||
async fn upload_download_object(&self) {
|
||||
let object_name = rand_object_name();
|
||||
let size = 16_usize;
|
||||
let mut file = fs::File::create(&object_name).unwrap();
|
||||
io::copy(&mut RandReader::new(size), &mut file).unwrap();
|
||||
file.sync_all().unwrap();
|
||||
self.client
|
||||
.upload_object(
|
||||
&mut UploadObjectArgs::new(&self.test_bucket, &object_name, &object_name).unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let filename = rand_object_name();
|
||||
self.client
|
||||
.download_object(
|
||||
&DownloadObjectArgs::new(&self.test_bucket, &object_name, &filename).unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
ClientTest::get_hash(&object_name) == ClientTest::get_hash(&filename),
|
||||
true
|
||||
);
|
||||
|
||||
fs::remove_file(&object_name).unwrap();
|
||||
fs::remove_file(&filename).unwrap();
|
||||
|
||||
self.client
|
||||
.remove_object(&RemoveObjectArgs::new(&self.test_bucket, &object_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
self.client
|
||||
.remove_object(&RemoveObjectArgs::new(&self.test_bucket, &object_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let object_name = rand_object_name();
|
||||
let size: usize = 16 + 5 * 1024 * 1024;
|
||||
let mut file = fs::File::create(&object_name).unwrap();
|
||||
io::copy(&mut RandReader::new(size), &mut file).unwrap();
|
||||
file.sync_all().unwrap();
|
||||
self.client
|
||||
.upload_object(
|
||||
&mut UploadObjectArgs::new(&self.test_bucket, &object_name, &object_name).unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let filename = rand_object_name();
|
||||
self.client
|
||||
.download_object(
|
||||
&DownloadObjectArgs::new(&self.test_bucket, &object_name, &filename).unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
ClientTest::get_hash(&object_name) == ClientTest::get_hash(&filename),
|
||||
true
|
||||
);
|
||||
|
||||
fs::remove_file(&object_name).unwrap();
|
||||
fs::remove_file(&filename).unwrap();
|
||||
|
||||
self.client
|
||||
.remove_object(&RemoveObjectArgs::new(&self.test_bucket, &object_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
self.client
|
||||
.remove_object(&RemoveObjectArgs::new(&self.test_bucket, &object_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
async fn remove_objects(&self) {
|
||||
let bucket_name = rand_bucket_name();
|
||||
self.client
|
||||
@ -598,6 +690,479 @@ impl<'a> ClientTest<'_> {
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
async fn set_get_delete_bucket_notification(&self) {
|
||||
let bucket_name = rand_bucket_name();
|
||||
self.client
|
||||
.make_bucket(&MakeBucketArgs::new(&bucket_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
self.client
|
||||
.set_bucket_notification(
|
||||
&SetBucketNotificationArgs::new(
|
||||
&bucket_name,
|
||||
&NotificationConfig {
|
||||
cloud_func_config_list: None,
|
||||
queue_config_list: Some(vec![QueueConfig {
|
||||
events: vec![
|
||||
String::from("s3:ObjectCreated:Put"),
|
||||
String::from("s3:ObjectCreated:Copy"),
|
||||
],
|
||||
id: None,
|
||||
prefix_filter_rule: Some(PrefixFilterRule {
|
||||
value: String::from("images"),
|
||||
}),
|
||||
suffix_filter_rule: Some(SuffixFilterRule {
|
||||
value: String::from("pg"),
|
||||
}),
|
||||
queue: String::from(ClientTest::SQS_ARN),
|
||||
}]),
|
||||
topic_config_list: None,
|
||||
},
|
||||
)
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let resp = self
|
||||
.client
|
||||
.get_bucket_notification(&GetBucketNotificationArgs::new(&bucket_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.config.queue_config_list.as_ref().unwrap().len(), 1);
|
||||
assert_eq!(
|
||||
resp.config.queue_config_list.as_ref().unwrap()[0]
|
||||
.events
|
||||
.contains(&String::from("s3:ObjectCreated:Put")),
|
||||
true
|
||||
);
|
||||
assert_eq!(
|
||||
resp.config.queue_config_list.as_ref().unwrap()[0]
|
||||
.events
|
||||
.contains(&String::from("s3:ObjectCreated:Copy")),
|
||||
true
|
||||
);
|
||||
assert_eq!(
|
||||
resp.config.queue_config_list.as_ref().unwrap()[0]
|
||||
.prefix_filter_rule
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.value,
|
||||
"images"
|
||||
);
|
||||
assert_eq!(
|
||||
resp.config.queue_config_list.as_ref().unwrap()[0]
|
||||
.suffix_filter_rule
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.value,
|
||||
"pg"
|
||||
);
|
||||
assert_eq!(
|
||||
resp.config.queue_config_list.as_ref().unwrap()[0].queue,
|
||||
ClientTest::SQS_ARN
|
||||
);
|
||||
|
||||
self.client
|
||||
.delete_bucket_notification(&DeleteBucketNotificationArgs::new(&bucket_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let resp = self
|
||||
.client
|
||||
.get_bucket_notification(&GetBucketNotificationArgs::new(&bucket_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.config.queue_config_list.is_none(), true);
|
||||
|
||||
self.client
|
||||
.remove_bucket(&RemoveBucketArgs::new(&bucket_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
async fn set_get_delete_bucket_policy(&self) {
|
||||
let bucket_name = rand_bucket_name();
|
||||
self.client
|
||||
.make_bucket(&MakeBucketArgs::new(&bucket_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let config = r#"
|
||||
{
|
||||
"Version": "2012-10-17",
|
||||
"Statement": [
|
||||
{
|
||||
"Action": [
|
||||
"s3:GetObject"
|
||||
],
|
||||
"Effect": "Allow",
|
||||
"Principal": {
|
||||
"AWS": [
|
||||
"*"
|
||||
]
|
||||
},
|
||||
"Resource": [
|
||||
"arn:aws:s3:::<BUCKET>/myobject*"
|
||||
],
|
||||
"Sid": ""
|
||||
}
|
||||
]
|
||||
}
|
||||
"#
|
||||
.replace("<BUCKET>", &bucket_name);
|
||||
|
||||
self.client
|
||||
.set_bucket_policy(&SetBucketPolicyArgs::new(&bucket_name, &config).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let resp = self
|
||||
.client
|
||||
.get_bucket_policy(&GetBucketPolicyArgs::new(&bucket_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.config.is_empty(), false);
|
||||
|
||||
self.client
|
||||
.delete_bucket_policy(&DeleteBucketPolicyArgs::new(&bucket_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let resp = self
|
||||
.client
|
||||
.get_bucket_policy(&GetBucketPolicyArgs::new(&bucket_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.config, "{}");
|
||||
|
||||
self.client
|
||||
.remove_bucket(&RemoveBucketArgs::new(&bucket_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
async fn set_get_delete_bucket_tags(&self) {
|
||||
let bucket_name = rand_bucket_name();
|
||||
self.client
|
||||
.make_bucket(&MakeBucketArgs::new(&bucket_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let tags = HashMap::from([
|
||||
(String::from("Project"), String::from("Project One")),
|
||||
(String::from("User"), String::from("jsmith")),
|
||||
]);
|
||||
|
||||
self.client
|
||||
.set_bucket_tags(&SetBucketTagsArgs::new(&bucket_name, &tags).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let resp = self
|
||||
.client
|
||||
.get_bucket_tags(&GetBucketTagsArgs::new(&bucket_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
resp.tags.len() == tags.len() && resp.tags.keys().all(|k| tags.contains_key(k)),
|
||||
true
|
||||
);
|
||||
|
||||
self.client
|
||||
.delete_bucket_tags(&DeleteBucketTagsArgs::new(&bucket_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let resp = self
|
||||
.client
|
||||
.get_bucket_tags(&GetBucketTagsArgs::new(&bucket_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.tags.is_empty(), true);
|
||||
|
||||
self.client
|
||||
.remove_bucket(&RemoveBucketArgs::new(&bucket_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
async fn set_get_delete_object_lock_config(&self) {
|
||||
let bucket_name = rand_bucket_name();
|
||||
|
||||
let mut args = MakeBucketArgs::new(&bucket_name).unwrap();
|
||||
args.object_lock = true;
|
||||
self.client.make_bucket(&args).await.unwrap();
|
||||
|
||||
self.client
|
||||
.set_object_lock_config(
|
||||
&SetObjectLockConfigArgs::new(
|
||||
&bucket_name,
|
||||
&ObjectLockConfig::new(RetentionMode::GOVERNANCE, Some(7), None).unwrap(),
|
||||
)
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let resp = self
|
||||
.client
|
||||
.get_object_lock_config(&GetObjectLockConfigArgs::new(&bucket_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
match resp.config.retention_mode {
|
||||
Some(r) => match r {
|
||||
RetentionMode::GOVERNANCE => true,
|
||||
_ => false,
|
||||
},
|
||||
_ => false,
|
||||
},
|
||||
true
|
||||
);
|
||||
|
||||
assert_eq!(resp.config.retention_duration_days == Some(7), true);
|
||||
assert_eq!(resp.config.retention_duration_years.is_none(), true);
|
||||
|
||||
self.client
|
||||
.delete_object_lock_config(&DeleteObjectLockConfigArgs::new(&bucket_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let resp = self
|
||||
.client
|
||||
.get_object_lock_config(&GetObjectLockConfigArgs::new(&bucket_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.config.retention_mode.is_none(), true);
|
||||
|
||||
self.client
|
||||
.remove_bucket(&RemoveBucketArgs::new(&bucket_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
async fn set_get_delete_object_tags(&self) {
|
||||
let object_name = rand_object_name();
|
||||
|
||||
let size = 16_usize;
|
||||
self.client
|
||||
.put_object(
|
||||
&mut PutObjectArgs::new(
|
||||
&self.test_bucket,
|
||||
&object_name,
|
||||
&mut RandReader::new(size),
|
||||
Some(size),
|
||||
None,
|
||||
)
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let tags = HashMap::from([
|
||||
(String::from("Project"), String::from("Project One")),
|
||||
(String::from("User"), String::from("jsmith")),
|
||||
]);
|
||||
|
||||
self.client
|
||||
.set_object_tags(
|
||||
&SetObjectTagsArgs::new(&self.test_bucket, &object_name, &tags).unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let resp = self
|
||||
.client
|
||||
.get_object_tags(&GetObjectTagsArgs::new(&self.test_bucket, &object_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
resp.tags.len() == tags.len() && resp.tags.keys().all(|k| tags.contains_key(k)),
|
||||
true
|
||||
);
|
||||
|
||||
self.client
|
||||
.delete_object_tags(
|
||||
&DeleteObjectTagsArgs::new(&self.test_bucket, &object_name).unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let resp = self
|
||||
.client
|
||||
.get_object_tags(&GetObjectTagsArgs::new(&self.test_bucket, &object_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.tags.is_empty(), true);
|
||||
|
||||
self.client
|
||||
.remove_object(&RemoveObjectArgs::new(&self.test_bucket, &object_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
async fn set_get_bucket_versioning(&self) {
|
||||
let bucket_name = rand_bucket_name();
|
||||
|
||||
self.client
|
||||
.make_bucket(&MakeBucketArgs::new(&bucket_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
self.client
|
||||
.set_bucket_versioning(&SetBucketVersioningArgs::new(&bucket_name, true).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let resp = self
|
||||
.client
|
||||
.get_bucket_versioning(&GetBucketVersioningArgs::new(&bucket_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
match resp.status {
|
||||
Some(v) => v,
|
||||
_ => false,
|
||||
},
|
||||
true
|
||||
);
|
||||
|
||||
self.client
|
||||
.set_bucket_versioning(&SetBucketVersioningArgs::new(&bucket_name, false).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let resp = self
|
||||
.client
|
||||
.get_bucket_versioning(&GetBucketVersioningArgs::new(&bucket_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
match resp.status {
|
||||
Some(v) => v,
|
||||
_ => false,
|
||||
},
|
||||
false
|
||||
);
|
||||
|
||||
self.client
|
||||
.remove_bucket(&RemoveBucketArgs::new(&bucket_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
async fn set_get_object_retention(&self) {
|
||||
let bucket_name = rand_bucket_name();
|
||||
|
||||
let mut args = MakeBucketArgs::new(&bucket_name).unwrap();
|
||||
args.object_lock = true;
|
||||
self.client.make_bucket(&args).await.unwrap();
|
||||
|
||||
let object_name = rand_object_name();
|
||||
|
||||
let size = 16_usize;
|
||||
let obj_resp = self
|
||||
.client
|
||||
.put_object(
|
||||
&mut PutObjectArgs::new(
|
||||
&bucket_name,
|
||||
&object_name,
|
||||
&mut RandReader::new(size),
|
||||
Some(size),
|
||||
None,
|
||||
)
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let mut args = SetObjectRetentionArgs::new(&bucket_name, &object_name).unwrap();
|
||||
args.retention_mode = Some(RetentionMode::GOVERNANCE);
|
||||
let retain_until_date = utc_now() + Duration::days(1);
|
||||
args.retain_until_date = Some(retain_until_date);
|
||||
|
||||
self.client.set_object_retention(&args).await.unwrap();
|
||||
|
||||
let resp = self
|
||||
.client
|
||||
.get_object_retention(&GetObjectRetentionArgs::new(&bucket_name, &object_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
match resp.retention_mode {
|
||||
Some(v) => match v {
|
||||
RetentionMode::GOVERNANCE => true,
|
||||
_ => false,
|
||||
},
|
||||
_ => false,
|
||||
},
|
||||
true
|
||||
);
|
||||
assert_eq!(
|
||||
match resp.retain_until_date {
|
||||
Some(v) => to_iso8601utc(v) == to_iso8601utc(retain_until_date),
|
||||
_ => false,
|
||||
},
|
||||
true,
|
||||
);
|
||||
|
||||
let mut args = SetObjectRetentionArgs::new(&bucket_name, &object_name).unwrap();
|
||||
args.bypass_governance_mode = true;
|
||||
self.client.set_object_retention(&args).await.unwrap();
|
||||
|
||||
let resp = self
|
||||
.client
|
||||
.get_object_retention(&GetObjectRetentionArgs::new(&bucket_name, &object_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.retention_mode.is_none(), true);
|
||||
assert_eq!(resp.retain_until_date.is_none(), true);
|
||||
|
||||
let mut args = RemoveObjectArgs::new(&bucket_name, &object_name).unwrap();
|
||||
let version_id = obj_resp.version_id.unwrap().clone();
|
||||
args.version_id = Some(version_id.as_str());
|
||||
self.client.remove_object(&args).await.unwrap();
|
||||
|
||||
self.client
|
||||
.remove_bucket(&RemoveBucketArgs::new(&bucket_name).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
async fn get_presigned_object_url(&self) {
|
||||
let object_name = rand_object_name();
|
||||
let resp = self
|
||||
.client
|
||||
.get_presigned_object_url(
|
||||
&GetPresignedObjectUrlArgs::new(&self.test_bucket, &object_name, Method::GET)
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.url.contains("X-Amz-Signature="), true);
|
||||
}
|
||||
|
||||
async fn get_presigned_post_form_data(&self) {
|
||||
let object_name = rand_object_name();
|
||||
let expiration = utc_now() + Duration::days(5);
|
||||
|
||||
let mut policy = PostPolicy::new(&self.test_bucket, &expiration).unwrap();
|
||||
policy.add_equals_condition("key", &object_name).unwrap();
|
||||
policy
|
||||
.add_content_length_range_condition(1 * 1024 * 1024, 4 * 1024 * 1024)
|
||||
.unwrap();
|
||||
|
||||
let form_data = self
|
||||
.client
|
||||
.get_presigned_post_form_data(&policy)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(form_data.contains_key("x-amz-signature"), true);
|
||||
assert_eq!(form_data.contains_key("policy"), true);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
@ -628,9 +1193,6 @@ async fn s3_tests() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
);
|
||||
ctest.init().await;
|
||||
|
||||
println!("compose_object()");
|
||||
ctest.compose_object().await;
|
||||
|
||||
println!("make_bucket() + bucket_exists() + remove_bucket()");
|
||||
ctest.bucket_exists().await;
|
||||
|
||||
@ -646,6 +1208,9 @@ async fn s3_tests() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
println!("get_object()");
|
||||
ctest.get_object().await;
|
||||
|
||||
println!("{{upload,download}}_object()");
|
||||
ctest.upload_download_object().await;
|
||||
|
||||
println!("remove_objects()");
|
||||
ctest.remove_objects().await;
|
||||
|
||||
@ -661,6 +1226,36 @@ async fn s3_tests() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
println!("copy_object()");
|
||||
ctest.copy_object().await;
|
||||
|
||||
println!("compose_object()");
|
||||
ctest.compose_object().await;
|
||||
|
||||
println!("{{set,get,delete}}_bucket_notification()");
|
||||
ctest.set_get_delete_bucket_notification().await;
|
||||
|
||||
println!("{{set,get,delete}}_bucket_policy()");
|
||||
ctest.set_get_delete_bucket_policy().await;
|
||||
|
||||
println!("{{set,get,delete}}_bucket_tags()");
|
||||
ctest.set_get_delete_bucket_tags().await;
|
||||
|
||||
println!("{{set,get,delete}}_object_lock_config()");
|
||||
ctest.set_get_delete_object_lock_config().await;
|
||||
|
||||
println!("{{set,get,delete}}_object_tags()");
|
||||
ctest.set_get_delete_object_tags().await;
|
||||
|
||||
println!("{{set,get}}_bucket_versioning()");
|
||||
ctest.set_get_bucket_versioning().await;
|
||||
|
||||
println!("{{set,get}}_object_retention()");
|
||||
ctest.set_get_object_retention().await;
|
||||
|
||||
println!("get_presigned_object_url()");
|
||||
ctest.get_presigned_object_url().await;
|
||||
|
||||
println!("get_presigned_post_form_data()");
|
||||
ctest.get_presigned_post_form_data().await;
|
||||
|
||||
ctest.drop().await;
|
||||
|
||||
Ok(())
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user