aliyun_oss_rs/bucket/
put_bucket.rs

1use crate::{
2    Error,
3    common::{Acl, DataRedundancyType, StorageClass},
4    error::normal_error,
5    request::{Oss, OssRequest},
6};
7use bytes::Bytes;
8use http::Method;
9use http_body_util::Full;
10use serde_derive::Serialize;
11use serde_xml_rs::to_string;
12
13#[derive(Debug, Serialize)]
14#[serde(rename_all = "PascalCase")]
15struct CreateBucketConfiguration {
16    storage_class: Option<StorageClass>,
17    data_redundancy_type: Option<DataRedundancyType>,
18}
19
20/// Call the PutBucket interface to create a bucket
21///
22/// An Alibaba Cloud account can create up to 100 buckets in a single region
23///
24/// See the [Alibaba Cloud documentation](https://help.aliyun.com/document_detail/31959.html) for details
25pub struct PutBucket {
26    req: OssRequest,
27    storage_class: Option<StorageClass>,
28    data_redundancy_type: Option<DataRedundancyType>,
29}
30impl PutBucket {
31    pub(super) fn new(oss: Oss) -> Self {
32        PutBucket {
33            req: OssRequest::new(oss, Method::PUT),
34            storage_class: None,
35            data_redundancy_type: None,
36        }
37    }
38    /// Set the bucket access permissions
39    pub fn set_acl(mut self, acl: Acl) -> Self {
40        self.req.insert_header("x-oss-acl", acl);
41        self
42    }
43    /// Specify the resource group ID
44    ///
45    /// If this header is included with a resource group ID, the created bucket belongs to that group. When the ID is rg-default-id, the bucket belongs to the default group.
46    ///
47    /// If the header is omitted, the bucket belongs to the default resource group.
48    pub fn set_group_id(mut self, group_id: impl ToString) -> Self {
49        self.req.insert_header("x-oss-resource-group-id", group_id);
50        self
51    }
52    /// Set the bucket storage class
53    pub fn set_storage_class(mut self, storage_class: StorageClass) -> Self {
54        let body_str = format!(
55            "<?xml version=\"1.0\" encoding=\"UTF-8\"?><CreateBucketConfiguration>{}{}</CreateBucketConfiguration>",
56            storage_class.to_string(),
57            self.data_redundancy_type.map_or(String::new(), |v| format!(
58                "<DataRedundancyType>{}</DataRedundancyType>",
59                v.to_string()
60            ))
61        );
62        self.storage_class = Some(storage_class);
63        self.req.set_body(Full::new(Bytes::from(body_str)));
64        self
65    }
66    /// Set the bucket data redundancy type
67    pub fn set_redundancy_type(mut self, redundancy_type: DataRedundancyType) -> Self {
68        let body_str = format!(
69            "<?xml version=\"1.0\" encoding=\"UTF-8\"?><CreateBucketConfiguration>{}{}</CreateBucketConfiguration>",
70            self.storage_class
71                .map(|v| format!("<StorageClass>{}</StorageClass>", v.to_string()))
72                .unwrap_or_else(|| String::new()),
73            redundancy_type.to_string()
74        );
75        self.req.set_body(Full::new(Bytes::from(body_str)));
76        self.data_redundancy_type = Some(redundancy_type);
77        self
78    }
79    /// Send the request
80    pub async fn send(self) -> Result<(), Error> {
81        let mut body = String::new();
82        if self.data_redundancy_type.is_some() || self.storage_class.is_some() {
83            let bucket_config = CreateBucketConfiguration {
84                storage_class: self.storage_class,
85                data_redundancy_type: self.data_redundancy_type,
86            };
87            if let Ok(body_str) = to_string(&bucket_config) {
88                body.push_str(&body_str)
89            };
90        }
91        // Build the HTTP request
92        let response = self.req.send_to_oss()?.await?;
93        // Parse the response
94        let status_code = response.status();
95        match status_code {
96            code if code.is_success() => Ok(()),
97            _ => Err(normal_error(response).await),
98        }
99    }
100}