Skip to main content

file_storage/system/r2/file/
exists.rs

1use crate::system::r2::r2_path::RUNTIME;
2use crate::system::R2Path;
3use crate::Error;
4use crate::Operation::Exists;
5
6impl<'a> R2Path<'a> {
7    //! Exists
8
9    /// See `FilePath::exists`.
10    pub fn exists(self) -> Result<bool, Error> {
11        RUNTIME.block_on(self.exists_async())
12    }
13
14    /// See `FilePath::exists`.
15    pub async fn exists_async(self) -> Result<bool, Error> {
16        use aws_sdk_s3::error::SdkError;
17        use std::io;
18        use std::io::ErrorKind::Other;
19
20        let response = Self::get_client(self.account_id)
21            .await
22            .head_object()
23            .bucket(self.bucket)
24            .key(self.key)
25            .send()
26            .await;
27        match response {
28            Ok(_response) => Ok(true),
29            Err(error) => match error {
30                SdkError::ServiceError(e) if e.err().is_not_found() => Ok(false),
31                error => Err(Error::from_source(
32                    self.path.clone(),
33                    Exists,
34                    io::Error::new(Other, error),
35                )),
36            },
37        }
38    }
39}