aliyun_oss_rs/bucket/
put_bucket_lifecycle.rs

1use crate::{
2    Error,
3    error::normal_error,
4    request::{Oss, OssRequest},
5};
6use bytes::Bytes;
7use http::Method;
8use http_body_util::Full;
9
10/// Configure lifecycle rules for a bucket.
11///
12/// See the [Alibaba Cloud documentation](https://help.aliyun.com/zh/oss/developer-reference/putbucketlifecycle) for details.
13pub struct PutBucketLifecycle {
14    req: OssRequest,
15    body: Option<String>,
16}
17
18impl PutBucketLifecycle {
19    pub(super) fn new(oss: Oss) -> Self {
20        let mut req = OssRequest::new(oss, Method::PUT);
21        req.insert_query("lifecycle", "");
22        PutBucketLifecycle { req, body: None }
23    }
24
25    /// Provide the complete lifecycle configuration document.
26    ///
27    /// The content must follow the OSS lifecycle XML schema.
28    pub fn set_configuration(mut self, xml: impl ToString) -> Self {
29        self.body = Some(xml.to_string());
30        self
31    }
32
33    /// Send the request to OSS.
34    pub async fn send(mut self) -> Result<(), Error> {
35        let body = self.body.ok_or(Error::MissingRequestBody)?;
36        self.req.set_body(Full::new(Bytes::from(body)));
37        let response = self.req.send_to_oss()?.await?;
38        match response.status() {
39            code if code.is_success() => Ok(()),
40            _ => Err(normal_error(response).await),
41        }
42    }
43}