s3/bucket/copy.rs
1use crate::bucket::{Bucket, Request};
2use crate::command::Command;
3use crate::error::S3Error;
4use crate::request::RequestImpl;
5
6impl Bucket {
7 /// Copy file from an S3 path, internally within the same bucket.
8 ///
9 /// # Example:
10 ///
11 /// ```rust,no_run
12 /// use s3::bucket::Bucket;
13 /// use s3::creds::Credentials;
14 /// use anyhow::Result;
15 ///
16 /// # #[tokio::main]
17 /// # async fn main() -> Result<()> {
18 ///
19 /// let bucket_name = "rust-s3-test";
20 /// let region = "us-east-1".parse()?;
21 /// let credentials = Credentials::default()?;
22 /// let bucket = Bucket::new(bucket_name, region, credentials)?;
23 ///
24 /// let code = bucket.copy_object_internal("/from.file", "/to.file").await?;
25 ///
26 /// # Ok(())
27 /// # }
28 /// ```
29 pub async fn copy_object_internal<F: AsRef<str>, T: AsRef<str>>(
30 &self,
31 from: F,
32 to: T,
33 ) -> Result<u16, S3Error> {
34 let fq_from = {
35 let from = from.as_ref();
36 let from = from.strip_prefix('/').unwrap_or(from);
37 format!("{bucket}/{path}", bucket = self.name(), path = from)
38 };
39 self.copy_object(fq_from, to).await
40 }
41
42 async fn copy_object<F: AsRef<str>, T: AsRef<str>>(
43 &self,
44 from: F,
45 to: T,
46 ) -> Result<u16, S3Error> {
47 let command = Command::CopyObject {
48 from: from.as_ref(),
49 };
50 let request = RequestImpl::new(self, to.as_ref(), command)?;
51 let response_data = request.response_data(false).await?;
52 Ok(response_data.status_code())
53 }
54}