aliyun_oss_rs/bucket/
put_bucket_logging.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/// Enable or update the bucket logging configuration
11///
12/// See the [Alibaba Cloud documentation](https://help.aliyun.com/zh/oss/developer-reference/putbucketlogging) for details
13pub struct PutBucketLogging {
14    req: OssRequest,
15}
16impl PutBucketLogging {
17    pub(super) fn new(
18        oss: Oss,
19        target_bucket: impl ToString,
20        target_prefix: impl ToString,
21    ) -> Self {
22        let mut req = OssRequest::new(oss, Method::PUT);
23        req.insert_query("logging", "");
24        let body = format!(
25            "<?xml version=\"1.0\" encoding=\"UTF-8\"?><BucketLoggingStatus><LoggingEnabled><TargetBucket>{}</TargetBucket><TargetPrefix>{}</TargetPrefix></LoggingEnabled></BucketLoggingStatus>",
26            target_bucket.to_string(),
27            target_prefix.to_string()
28        );
29        req.set_body(Full::new(Bytes::from(body)));
30        PutBucketLogging { req }
31    }
32    /// Send the request
33    pub async fn send(self) -> Result<(), Error> {
34        let response = self.req.send_to_oss()?.await?;
35        let status_code = response.status();
36        match status_code {
37            code if code.is_success() => Ok(()),
38            _ => Err(normal_error(response).await),
39        }
40    }
41}