xt_oss/oss/api/bucket/
worm.rs

1use crate::oss;
2
3use self::builders::{
4    AbortBucketWormBuilder, CompleteBucketWormBuilder, ExtendBucketWormBuilder,
5    GetBucketWormBuilder, InitiateBucketWormBuilder,
6};
7
8pub mod builders {
9
10    use crate::oss::{
11        self,
12        api::{self, ApiResponseFrom},
13        entities::worm::{ExtendWormConfiguration, InitiateWormConfiguration, WormConfiguration},
14        http,
15    };
16
17    pub struct InitiateBucketWormBuilder<'a> {
18        client: &'a oss::Client<'a>,
19        days: i32,
20    }
21
22    impl<'a> InitiateBucketWormBuilder<'a> {
23        pub fn new(client: &'a oss::Client) -> Self {
24            Self { client, days: 1 }
25        }
26
27        pub fn with_days(mut self, value: i32) -> Self {
28            self.days = value;
29            self
30        }
31
32        fn config(&self) -> String {
33            let config = InitiateWormConfiguration {
34                retention_period_in_days: self.days,
35            };
36            quick_xml::se::to_string(&config).unwrap()
37        }
38
39        pub async fn execute(&self) -> api::ApiResult {
40            let res = format!("/{}/?{}", self.client.bucket(), "worm");
41            let url = format!("{}/?{}", self.client.base_url(), "worm");
42
43            let data = oss::Bytes::from(self.config());
44            let resp = self
45                .client
46                .request
47                .task()
48                .with_url(&url)
49                .with_method(http::Method::POST)
50                .with_resource(&res)
51                .with_body(data)
52                .execute()
53                .await?;
54
55            Ok(ApiResponseFrom(resp).to_empty().await)
56        }
57    }
58
59    pub struct AbortBucketWormBuilder<'a> {
60        client: &'a oss::Client<'a>,
61    }
62
63    impl<'a> AbortBucketWormBuilder<'a> {
64        pub fn new(client: &'a oss::Client) -> Self {
65            Self { client }
66        }
67        pub async fn execute(&self) -> api::ApiResult {
68            let res = format!("/{}/?{}", self.client.bucket(), "worm");
69            let url = format!("{}/?{}", self.client.base_url(), "worm");
70
71            let resp = self
72                .client
73                .request
74                .task()
75                .with_url(&url)
76                .with_method(http::Method::DELETE)
77                .with_resource(&res)
78                .execute()
79                .await?;
80
81            Ok(ApiResponseFrom(resp).to_empty().await)
82        }
83    }
84
85    pub struct ExtendBucketWormBuilder<'a> {
86        client: &'a oss::Client<'a>,
87        worm_id: &'a str,
88        days: u32,
89    }
90
91    impl<'a> ExtendBucketWormBuilder<'a> {
92        pub fn new(client: &'a oss::Client, worm_id: &'a str) -> Self {
93            Self {
94                client,
95                days: 1,
96                worm_id,
97            }
98        }
99
100        pub fn with_days(mut self, value: u32) -> Self {
101            self.days = value;
102            self
103        }
104
105        fn config(&self) -> String {
106            let config = ExtendWormConfiguration {
107                retention_period_in_days: self.days,
108            };
109            quick_xml::se::to_string(&config).unwrap()
110        }
111
112        pub async fn execute(&self) -> api::ApiResult {
113            let res = format!(
114                "/{}/?{}&wormId={}",
115                self.client.bucket(),
116                "wormExtend",
117                self.worm_id
118            );
119            let url = format!(
120                "{}/?{}&wormId={}",
121                self.client.base_url(),
122                "wormExtend",
123                self.worm_id
124            );
125            let config = self.config();
126            let data = oss::Bytes::from(config);
127
128            let resp = self
129                .client
130                .request
131                .task()
132                .with_url(&url)
133                .with_method(http::Method::POST)
134                .with_body(data)
135                .with_resource(&res)
136                .execute()
137                .await?;
138
139            Ok(ApiResponseFrom(resp).to_empty().await)
140        }
141    }
142
143    pub struct CompleteBucketWormBuilder<'a> {
144        client: &'a oss::Client<'a>,
145        worm_id: &'a str,
146    }
147
148    impl<'a> CompleteBucketWormBuilder<'a> {
149        pub fn new(client: &'a oss::Client, worm_id: &'a str) -> Self {
150            Self { client, worm_id }
151        }
152
153        pub async fn execute(&self) -> api::ApiResult {
154            let res = format!("/{}/?wormId={}", self.client.bucket(), self.worm_id);
155            let url = format!("{}/?wormId={}", self.client.base_url(), self.worm_id);
156
157            let resp = self
158                .client
159                .request
160                .task()
161                .with_url(&url)
162                .with_method(http::Method::POST)
163                .with_resource(&res)
164                .execute()
165                .await?;
166            Ok(ApiResponseFrom(resp).to_empty().await)
167        }
168    }
169
170    pub struct GetBucketWormBuilder<'a> {
171        client: &'a oss::Client<'a>,
172    }
173
174    impl<'a> GetBucketWormBuilder<'a> {
175        pub fn new(client: &'a oss::Client) -> Self {
176            Self { client }
177        }
178
179        pub async fn execute(&self) -> api::ApiResult<WormConfiguration> {
180            let res = format!("/{}/?{}", self.client.bucket(), "worm");
181            let url = format!("{}/?{}", self.client.base_url(), "worm");
182
183            let resp = self
184                .client
185                .request
186                .task()
187                .with_url(&url)
188                .with_resource(&res)
189                .execute()
190                .await?;
191            Ok(ApiResponseFrom(resp).to_type().await)
192        }
193    }
194}
195
196/// # 合规保留策略`WORM``
197#[allow(non_snake_case)]
198impl<'a> oss::Client<'a> {
199    /// 调用InitiateBucketWorm接口新建一条合规保留策略。
200    ///
201    /// - [official docs](https://help.aliyun.com/zh/oss/developer-reference/initiatebucketworm)
202    /// - [xtoss example](https://github.com/isme-sun/xt_oss/blob/main/examples/api_bucket_worm_init.rs)
203    #[allow(non_snake_case)]
204    pub fn InitiateBucketWorm(&self) -> InitiateBucketWormBuilder {
205        InitiateBucketWormBuilder::new(self)
206    }
207
208    /// AbortBucketWorm用于删除未锁定的合规保留策略。
209    ///
210    /// - [official docs](https://help.aliyun.com/zh/oss/developer-reference/abortbucketworm)
211    /// - [xtoss example](https://github.com/isme-sun/xt_oss/blob/main/examples/api_bucket_worm_abort.rs)
212    #[allow(non_snake_case)]
213    pub fn AbortBucketWorm(&self) -> AbortBucketWormBuilder {
214        AbortBucketWormBuilder::new(self)
215    }
216
217    /// CompleteBucketWorm用于锁定合规保留策略。
218    ///
219    /// - [official docs](https://help.aliyun.com/zh/oss/developer-reference/completebucketworm)
220    /// - [xtoss example](https://github.com/isme-sun/xt_oss/blob/main/examples/api_bucket_worm_complete.rs)
221    #[allow(non_snake_case)]
222    pub fn CompleteBucketWorm(&self, worm_id: &'a str) -> CompleteBucketWormBuilder {
223        CompleteBucketWormBuilder::new(self, worm_id)
224    }
225
226    /// ExtendBucketWorm用于延长已锁定的合规保留策略对应Bucket中Object的保留天数。
227    ///
228    /// - [official docs](https://help.aliyun.com/zh/oss/developer-reference/extendbucketworm)
229    /// - [xtoss example](https://github.com/isme-sun/xt_oss/blob/main/examples/api_bucket_worm_extend.rs)
230    pub fn ExtendBucketWorm(&self, worm_id: &'a str) -> ExtendBucketWormBuilder {
231        ExtendBucketWormBuilder::new(self, worm_id)
232    }
233
234    /// GetBucketWorm用于获取指定存储空间`Bucket`的合规保留策略信息。
235    ///
236    /// - [official docs](https://help.aliyun.com/zh/oss/developer-reference/getbucketworm)
237    /// - [xtoss example](https://github.com/isme-sun/xt_oss/blob/main/examples/api_bucket_worm_get.rs)
238    pub fn GetBucketWorm(&self) -> GetBucketWormBuilder {
239        GetBucketWormBuilder::new(self)
240    }
241}