minio 0.4.0

MinIO SDK for Amazon S3 compatible object storage access
Documentation
// MinIO Rust Library for Amazon S3 Compatible Cloud Storage
// Copyright 2023 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::client::MinioClient;
use crate::s3::error::ValidationErr;
use crate::s3::header_constants::*;
use crate::s3::multimap_ext::{Multimap, MultimapExt};
use crate::s3::response::StatObjectResponse;
use crate::s3::sse::{Sse, SseCustomerKey};
use crate::s3::types::{BucketName, ObjectKey, Region, S3Api, S3Request, ToS3Request, VersionId};
use crate::s3::utils::{UtcTime, check_ssec, to_http_header_value};
use async_trait::async_trait;
use http::Method;
use typed_builder::TypedBuilder;

/// Argument builder for the [`HeadObject`](https://docs.aws.amazon.com/AmazonS3/latest/API/API_HeadObject.html) S3 API operation.
///
/// This struct constructs the parameters required for the [`Client::stat_object`](crate::s3::client::MinioClient::stat_object) method.
///
/// # HTTP Method
///
/// This operation uses the HTTP HEAD method, which retrieves object metadata
/// without transferring the object body. This is more efficient than GET when
/// you only need metadata (size, ETag, Content-Type, Last-Modified, etc.).
#[derive(Debug, Clone, TypedBuilder)]
pub struct StatObject {
    #[builder(!default)] // force required
    client: MinioClient,

    #[builder(default, setter(into))]
    extra_headers: Option<Multimap>,
    #[builder(default, setter(into))]
    extra_query_params: Option<Multimap>,
    #[builder(setter(into), !default)]
    bucket: BucketName,
    #[builder(setter(into), !default)]
    object: ObjectKey,

    #[builder(default, setter(into))]
    version_id: Option<VersionId>,
    #[builder(default, setter(into))]
    region: Option<Region>,
    #[builder(default, setter(into))]
    ssec: Option<SseCustomerKey>,

    // Conditionals
    #[builder(default, setter(into))]
    match_etag: Option<String>,
    #[builder(default, setter(into))]
    not_match_etag: Option<String>,
    #[builder(default, setter(into))]
    modified_since: Option<UtcTime>,
    #[builder(default, setter(into))]
    unmodified_since: Option<UtcTime>,
}

/// Builder type for [`StatObject`] that is returned by [`MinioClient::stat_object`](crate::s3::client::MinioClient::stat_object).
///
/// This type alias simplifies the complex generic signature generated by the `typed_builder` crate.
pub type StatObjectBldr = StatObjectBuilder<(
    (MinioClient,),
    (),
    (),
    (BucketName,),
    (ObjectKey,),
    (),
    (),
    (),
    (),
    (),
    (),
    (),
)>;

impl S3Api for StatObject {
    type S3Response = StatObjectResponse;
}

#[async_trait]
impl ToS3Request for StatObject {
    fn to_s3request(self) -> Result<S3Request, ValidationErr> {
        check_ssec(&self.ssec, &self.client)?;

        let mut headers: Multimap = self.extra_headers.unwrap_or_default();
        {
            if let Some(v) = self.match_etag {
                headers.add(IF_MATCH, v);
            }
            if let Some(v) = self.not_match_etag {
                headers.add(IF_NONE_MATCH, v);
            }
            if let Some(v) = self.modified_since {
                headers.add(IF_MODIFIED_SINCE, to_http_header_value(v));
            }
            if let Some(v) = self.unmodified_since {
                headers.add(IF_UNMODIFIED_SINCE, to_http_header_value(v));
            }
            if let Some(v) = self.ssec {
                headers.add_multimap(v.headers());
            }
        }

        let mut query_params: Multimap = self.extra_query_params.unwrap_or_default();
        query_params.add_version(self.version_id);

        Ok(S3Request::builder()
            .client(self.client)
            .method(Method::HEAD)
            .region(self.region)
            .bucket(self.bucket)
            .object(self.object)
            .query_params(query_params)
            .headers(headers)
            .build())
    }
}