Skip to content

Commit

Permalink
refactored from_s3response trait, and refactored get/set/delete policy
Browse files Browse the repository at this point in the history
  • Loading branch information
HJLebbink committed Mar 3, 2025
1 parent fc3f122 commit 26770c8
Show file tree
Hide file tree
Showing 28 changed files with 586 additions and 244 deletions.
61 changes: 0 additions & 61 deletions src/s3/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1392,67 +1392,6 @@ impl<'a> SetBucketNotificationArgs<'a> {
}
}

/// Argument for [delete_bucket_policy()](crate::s3::client::Client::delete_bucket_policy) API
pub type DeleteBucketPolicyArgs<'a> = BucketArgs<'a>;

/// Argument for [get_bucket_policy()](crate::s3::client::Client::get_bucket_policy) API
pub type GetBucketPolicyArgs<'a> = BucketArgs<'a>;

/// Argument for [set_bucket_policy()](crate::s3::client::Client::set_bucket_policy) API
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> {
/// Returns argument for [set_bucket_policy()](crate::s3::client::Client::set_bucket_policy) API with given bucket name and configuration
///
/// # Examples
///
/// ```
/// use minio::s3::args::*;
/// let config = r#"{
/// "Version": "2012-10-17",
/// "Statement": [
/// {
/// "Effect": "Allow",
/// "Principal": {
/// "AWS": "*"
/// },
/// "Action": [
/// "s3:GetBucketLocation",
/// "s3:ListBucket"
/// ],
/// "Resource": "arn:aws:s3:::my-bucket"
/// },
/// {
/// "Effect": "Allow",
/// "Principal": {
/// "AWS": "*"
/// },
/// "Action": "s3:GetObject",
/// "Resource": "arn:aws:s3:::my-bucket/*"
/// }
/// ]
/// }"#;
/// let args = SetBucketPolicyArgs::new("my-bucket", config).unwrap();
/// ```
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,
})
}
}

/// Argument for [delete_bucket_replication()](crate::s3::client::Client::delete_bucket_replication) API
pub type DeleteBucketReplicationArgs<'a> = BucketArgs<'a>;

Expand Down
6 changes: 6 additions & 0 deletions src/s3/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
//! Argument builders for [minio::s3::client::Client](crate::s3::client::Client) APIs
mod bucket_common;
mod delete_bucket_policy;
mod get_bucket_encryption;
mod get_bucket_lifecycle;
mod get_bucket_policy;
mod get_bucket_versioning;
mod get_object;
mod list_buckets;
Expand All @@ -29,11 +31,14 @@ mod put_object;
mod remove_objects;
mod set_bucket_encryption;
mod set_bucket_lifecycle;
mod set_bucket_policy;
mod set_bucket_versioning;

pub use bucket_common::*;
pub use delete_bucket_policy::*;
pub use get_bucket_encryption::*;
pub use get_bucket_lifecycle::*;
pub use get_bucket_policy::*;
pub use get_bucket_versioning::*;
pub use get_object::*;
pub use list_buckets::*;
Expand All @@ -45,4 +50,5 @@ pub use put_object::*;
pub use remove_objects::*;
pub use set_bucket_encryption::*;
pub use set_bucket_lifecycle::*;
pub use set_bucket_policy::*;
pub use set_bucket_versioning::*;
63 changes: 63 additions & 0 deletions src/s3/builders/delete_bucket_policy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// MinIO Rust Library for Amazon S3 Compatible Cloud Storage
// Copyright 2025 MinIO, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::s3::builders::BucketCommon;
use crate::s3::error::Error;
use crate::s3::response::DeleteBucketPolicyResponse;
use crate::s3::types::{S3Api, S3Request, ToS3Request};
use crate::s3::utils::check_bucket_name;
use crate::s3::Client;
use http::Method;

/// Argument builder for [delete_bucket_policy()](Client::delete_bucket_policy) API
pub type DeleteBucketPolicy = BucketCommon<DeleteBucketPolicyPhantomData>;

#[derive(Default, Debug)]
pub struct DeleteBucketPolicyPhantomData;

impl S3Api for DeleteBucketPolicy {
type S3Response = DeleteBucketPolicyResponse;
}

impl ToS3Request for DeleteBucketPolicy {
fn to_s3request(&self) -> Result<S3Request, Error> {
check_bucket_name(&self.bucket, true)?;

let headers = self
.extra_headers
.as_ref()
.filter(|v| !v.is_empty())
.cloned()
.unwrap_or_default();
let mut query_params = self
.extra_query_params
.as_ref()
.filter(|v| !v.is_empty())
.cloned()
.unwrap_or_default();

query_params.insert(String::from("policy"), String::new());

let client: &Client = self.client.as_ref().ok_or(Error::NoClientProvided)?;

let req = S3Request::new(client, Method::DELETE)
.region(self.region.as_deref())
.bucket(Some(&self.bucket))
.query_params(query_params)
.headers(headers);

Ok(req)
}
}
63 changes: 63 additions & 0 deletions src/s3/builders/get_bucket_policy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// MinIO Rust Library for Amazon S3 Compatible Cloud Storage
// Copyright 2025 MinIO, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::s3::builders::BucketCommon;
use crate::s3::error::Error;
use crate::s3::response::GetBucketPolicyResponse;
use crate::s3::types::{S3Api, S3Request, ToS3Request};
use crate::s3::utils::check_bucket_name;
use crate::s3::Client;
use http::Method;

/// Argument builder for [get_bucket_policy()](Client::get_bucket_policy) API
pub type GetBucketPolicy = BucketCommon<GetBucketPolicyPhantomData>;

#[derive(Default, Debug)]
pub struct GetBucketPolicyPhantomData;

impl S3Api for GetBucketPolicy {
type S3Response = GetBucketPolicyResponse;
}

impl ToS3Request for GetBucketPolicy {
fn to_s3request(&self) -> Result<S3Request, Error> {
check_bucket_name(&self.bucket, true)?;

let headers = self
.extra_headers
.as_ref()
.filter(|v| !v.is_empty())
.cloned()
.unwrap_or_default();
let mut query_params = self
.extra_query_params
.as_ref()
.filter(|v| !v.is_empty())
.cloned()
.unwrap_or_default();

query_params.insert(String::from("policy"), String::new());

let client: &Client = self.client.as_ref().ok_or(Error::NoClientProvided)?;

let req = S3Request::new(client, Method::GET)
.region(self.region.as_deref())
.bucket(Some(&self.bucket))
.query_params(query_params)
.headers(headers);

Ok(req)
}
}
108 changes: 108 additions & 0 deletions src/s3/builders/set_bucket_policy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// MinIO Rust Library for Amazon S3 Compatible Cloud Storage
// Copyright 2025 MinIO, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::s3::builders::SegmentedBytes;
use crate::s3::error::Error;
use crate::s3::response::SetBucketLifecycleResponse;
use crate::s3::types::{S3Api, S3Request, ToS3Request};
use crate::s3::utils::{check_bucket_name, Multimap};
use crate::s3::Client;
use bytes::Bytes;
use http::Method;

/// Argument builder for [set_bucket_policy()](crate::s3::client::Client::set_bucket_policy) API
#[derive(Clone, Debug, Default)]
pub struct SetBucketPolicy {
pub(crate) client: Option<Client>,

pub(crate) extra_headers: Option<Multimap>,
pub(crate) extra_query_params: Option<Multimap>,
pub(crate) region: Option<String>,
pub(crate) bucket: String,

pub(crate) config: String, //TODO consider PolicyConfig struct
}

impl SetBucketPolicy {
pub fn new(bucket: &str) -> Self {
Self {
bucket: bucket.to_owned(),
..Default::default()
}
}
pub fn client(mut self, client: &Client) -> Self {
self.client = Some(client.clone());
self
}

pub fn extra_headers(mut self, extra_headers: Option<Multimap>) -> Self {
self.extra_headers = extra_headers;
self
}

pub fn extra_query_params(mut self, extra_query_params: Option<Multimap>) -> Self {
self.extra_query_params = extra_query_params;
self
}

pub fn region(mut self, region: Option<String>) -> Self {
self.region = region;
self
}

pub fn config(mut self, config: String) -> Self {
self.config = config;
self
}
}

impl S3Api for SetBucketPolicy {
type S3Response = SetBucketLifecycleResponse;
}

impl ToS3Request for SetBucketPolicy {
fn to_s3request(&self) -> Result<S3Request, Error> {
check_bucket_name(&self.bucket, true)?;

let headers = self
.extra_headers
.as_ref()
.filter(|v| !v.is_empty())
.cloned()
.unwrap_or_default();
let mut query_params = self
.extra_query_params
.as_ref()
.filter(|v| !v.is_empty())
.cloned()
.unwrap_or_default();

query_params.insert(String::from("policy"), String::new());

let bytes: Bytes = self.config.to_string().into();
let body: Option<SegmentedBytes> = Some(SegmentedBytes::from(bytes));

let client: &Client = self.client.as_ref().ok_or(Error::NoClientProvided)?;

let req = S3Request::new(client, Method::PUT)
.region(self.region.as_deref())
.bucket(Some(&self.bucket))
.query_params(query_params)
.headers(headers)
.body(body);

Ok(req)
}
}
Loading

0 comments on commit 26770c8

Please sign in to comment.