aliyun_oss_rs/bucket/
put_bucket.rs1use 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
20pub 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 pub fn set_acl(mut self, acl: Acl) -> Self {
40 self.req.insert_header("x-oss-acl", acl);
41 self
42 }
43 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 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 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 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 let response = self.req.send_to_oss()?.await?;
93 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}