1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use reqwest::Method;

use crate::{
    client::Client,
    error::ObsError,
    model::bucket::{
        create_bucket::CreateBucketRequest, list_bucket::ListAllMyBuckets,
        list_object::ListObjectsResponse, copy_object::CopyObjectResponse,
    },
    object::ObjectTrait,
};

#[async_trait::async_trait]
pub trait BucketTrait {
    async fn list_buckets(&self) -> Result<ListAllMyBuckets, ObsError>;
    async fn create_bucket(&self, name: &str, location: Option<&str>) -> Result<(), ObsError>;
    async fn list_objects(
        &self,
        name: &str,
        prefix: Option<&str>,
        marker: Option<&str>,
        max_keys: Option<usize>,
    ) -> Result<ListObjectsResponse, ObsError>;
}

pub struct Bucket<'a> {
    name: &'a str,
    client: &'a Client,
}

impl<'a> Bucket<'a> {
    pub fn new(name: &'a str, client: &'a Client) -> Self {
        Self { name, client }
    }

    pub async fn put_object(&self, key: &str, object: &'static [u8]) -> Result<(), ObsError> {
        self.client.put_object(self.name, key, object).await
    }

    pub async fn copy_object(&self, src: &str, dest: &str) -> Result<CopyObjectResponse, ObsError> {
        self.client.copy_object(self.name, src, dest).await
    }
}

#[async_trait::async_trait]
impl BucketTrait for Client {
    async fn list_buckets(&self) -> Result<ListAllMyBuckets, ObsError> {
        let resp: ListAllMyBuckets = self
            .do_action_without_bucket_name(Method::GET, "", None, None::<String>)
            .await?
            .json()
            .await?;

        Ok(resp)
    }
    async fn create_bucket(&self, name: &str, location: Option<&str>) -> Result<(), ObsError> {
        let body = if let Some(loc) = location {
            let xml = CreateBucketRequest::new(loc);
            serde_xml_rs::to_string(&xml)?
        } else {
            String::new()
        };

        let _res = self
            .do_action(Method::PUT, name, "", None, Some(body))
            .await?;

        Ok(())
    }

    async fn list_objects(
        &self,
        name: &str,
        prefix: Option<&str>,
        marker: Option<&str>,
        max_keys: Option<usize>,
    ) -> Result<ListObjectsResponse, ObsError> {
        unimplemented!()
    }
}