minio 0.4.0

MinIO SDK for Amazon S3 compatible object storage access
Documentation
// 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::{
    ComposeObject, ComposeObjectBldr, ComposeObjectInternal, ComposeObjectInternalBldr,
    ComposeSource, CopyObject, CopyObjectBldr, CopyObjectInternal, CopyObjectInternalBldr,
    UploadPartCopy, UploadPartCopyBldr,
};
use crate::s3::client::MinioClient;
use crate::s3::error::ValidationErr;
use crate::s3::types::{BucketName, ObjectKey, UploadId};

impl MinioClient {
    /// Creates a [`UploadPartCopy`] request builder.
    /// See [UploadPartCopy](https://docs.aws.amazon.com/AmazonS3/latest/API/API_UploadPartCopy.html) S3 API
    ///
    /// To execute the request, call [`UploadPartCopy::send()`](crate::s3::types::S3Api::send),
    /// which returns a [`Result`] containing a [`UploadPartCopyResponse`](crate::s3::response::UploadPartCopyResponse).    
    ///
    /// # Example
    ///
    /// ```no_run
    /// use minio::s3::MinioClient;
    /// use minio::s3::creds::StaticProvider;
    /// use minio::s3::http::BaseUrl;
    /// use minio::s3::response::UploadPartCopyResponse;
    /// use minio::s3::segmented_bytes::SegmentedBytes;
    /// use minio::s3::types::S3Api;
    /// use minio::s3::response_traits::HasObject;
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let base_url = "http://localhost:9000/".parse::<BaseUrl>().unwrap();
    ///     let static_provider = StaticProvider::new("minioadmin", "minioadmin", None);
    ///     let client = MinioClient::new(base_url, Some(static_provider), None, None).unwrap();
    ///     let data1: SegmentedBytes = SegmentedBytes::from("aaaa".to_string());
    ///     todo!();
    ///     let resp: UploadPartCopyResponse = client
    ///         .upload_part_copy("bucket-name", "object-name", "upload-id-123")
    ///         .unwrap().build().send().await.unwrap();
    ///     println!("uploaded {}", resp.object().unwrap());
    /// }
    /// ```
    pub fn upload_part_copy<B, O, U>(
        &self,
        bucket: B,
        object: O,
        upload_id: U,
    ) -> Result<UploadPartCopyBldr, ValidationErr>
    where
        B: TryInto<BucketName>,
        B::Error: Into<ValidationErr>,
        O: TryInto<ObjectKey>,
        O::Error: Into<ValidationErr>,
        U: TryInto<UploadId>,
        U::Error: Into<ValidationErr>,
    {
        Ok(UploadPartCopy::builder()
            .client(self.clone())
            .bucket(bucket.try_into().map_err(Into::into)?)
            .object(object.try_into().map_err(Into::into)?)
            .upload_id(upload_id.try_into().map_err(Into::into)?))
    }

    /// Create a CopyObject request builder. This is a lower-level API that
    /// performs a non-multipart object copy.
    pub(crate) fn copy_object_internal(
        &self,
        bucket: BucketName,
        object: ObjectKey,
    ) -> CopyObjectInternalBldr {
        CopyObjectInternal::builder()
            .client(self.clone())
            .bucket(bucket)
            .object(object)
    }

    /// Create a CopyObject request builder.
    /// See [CopyObject](https://docs.aws.amazon.com/AmazonS3/latest/API/API_CopyObject.html) S3 API
    ///
    /// To execute the copy operation, call [`CopyObject::send()`](crate::s3::types::S3Api::send),
    /// which returns a [`Result`] containing a [`CopyObjectResponse`](crate::s3::response::CopyObjectResponse).
    ///
    /// The destination of the copy is specified via the `bucket` and `object` parameters of this function.
    /// To specify the source object to be copied, call `.source(...)` on the returned [`CopyObject`] builder.
    ///
    /// Internally, this function first performs a [`stat_object`](MinioClient::stat_object) call
    /// to retrieve metadata about the source object. It then constructs a
    /// [`compose_object`](MinioClient::compose_object) request to perform the actual copy.
    ///
    /// # Arguments
    ///
    /// - `bucket`: The name of the destination bucket.
    /// - `object`: The key (name) of the destination object.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use minio::s3::MinioClient;
    /// use minio::s3::creds::StaticProvider;
    /// use minio::s3::http::BaseUrl;
    /// use minio::s3::response::CopyObjectResponse;
    /// use minio::s3::builders::CopySource;
    /// use minio::s3::types::{BucketName, ObjectKey, S3Api};
    /// use minio::s3::response_traits::HasVersion;
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let base_url = "http://localhost:9000/".parse::<BaseUrl>().unwrap();
    ///     let static_provider = StaticProvider::new("minioadmin", "minioadmin", None);
    ///     let client = MinioClient::new(base_url, Some(static_provider), None, None).unwrap();
    ///     let resp: CopyObjectResponse = client
    ///         .copy_object("bucket-name-dst", "object-name-dst")
    ///         .unwrap()
    ///         .source(CopySource::builder().bucket(BucketName::new("bucket-name-src").unwrap()).object(ObjectKey::new("object-name-src").unwrap()).build())
    ///         .build().send().await.unwrap();
    ///     println!("copied the file from src to dst. New version: {:?}", resp.version_id());
    /// }
    /// ```
    pub fn copy_object<B, O>(&self, bucket: B, object: O) -> Result<CopyObjectBldr, ValidationErr>
    where
        B: TryInto<BucketName>,
        B::Error: Into<ValidationErr>,
        O: TryInto<ObjectKey>,
        O::Error: Into<ValidationErr>,
    {
        Ok(CopyObject::builder()
            .client(self.clone())
            .bucket(bucket.try_into().map_err(Into::into)?)
            .object(object.try_into().map_err(Into::into)?))
    }

    /// Create a ComposeObjectInternal request builder. This is a higher-level API that
    /// performs a multipart object compose.
    pub(crate) fn compose_object_internal(
        &self,
        bucket: BucketName,
        object: ObjectKey,
    ) -> ComposeObjectInternalBldr {
        ComposeObjectInternal::builder()
            .client(self.clone())
            .bucket(bucket)
            .object(object)
    }

    /// compose object is higher-level API that calls an internal compose object, and if that call fails,
    /// it calls ['abort_multipart_upload`](MinioClient::abort_multipart_upload).
    pub fn compose_object<B, O>(
        &self,
        bucket: B,
        object: O,
        sources: Vec<ComposeSource>,
    ) -> Result<ComposeObjectBldr, ValidationErr>
    where
        B: TryInto<BucketName>,
        B::Error: Into<ValidationErr>,
        O: TryInto<ObjectKey>,
        O::Error: Into<ValidationErr>,
    {
        Ok(ComposeObject::builder()
            .client(self.clone())
            .bucket(bucket.try_into().map_err(Into::into)?)
            .object(object.try_into().map_err(Into::into)?)
            .sources(sources))
    }
}