xt_oss/oss/api/bucket/
log.rs

1use crate::oss;
2
3use self::builders::{
4    DeleteBucketLoggingBuilder, GetBucketLoggingBuilder, PutBucketLoggingBuilder,
5};
6
7pub mod builders {
8    use crate::oss::{
9        self,
10        api::{self, ApiResponseFrom},
11        entities::log::{BucketLoggingStatus, LoggingEnabled},
12        http,
13    };
14
15    pub struct PutBucketLoggingBuilder<'a> {
16        client: &'a oss::Client<'a>,
17        enabled: Option<bool>,
18        bucket: Option<&'a str>,
19        target_prefix: Option<&'a str>,
20    }
21
22    impl<'a> PutBucketLoggingBuilder<'a> {
23        pub(crate) fn new(client: &'a oss::Client) -> Self {
24            Self {
25                client,
26                enabled: None,
27                bucket: None,
28                target_prefix: None,
29            }
30        }
31
32        pub fn with_enabled(mut self, value: bool) -> Self {
33            self.enabled = Some(value);
34            self
35        }
36
37        pub fn with_bucket(mut self, value: &'a str) -> Self {
38            self.bucket = Some(value);
39            self
40        }
41
42        pub fn with_target_prefix(mut self, value: &'a str) -> Self {
43            self.target_prefix = Some(value);
44            self
45        }
46
47        // todo 确认生成方式时候合理
48        pub(crate) fn config(&self) -> String {
49            let config = if self.enabled == Some(true) {
50                BucketLoggingStatus {
51                    logging_enabled: Some(LoggingEnabled {
52                        target_bucket: self
53                            .bucket
54                            .or(Some(self.client.bucket()))
55                            .map(|s| s.to_string()),
56                        target_prefix: self.target_prefix.map(|s| s.to_string()),
57                    }),
58                }
59            } else {
60                BucketLoggingStatus {
61                    logging_enabled: None,
62                }
63            };
64            // dbg!(&config);
65            quick_xml::se::to_string(&config).unwrap()
66        }
67
68        pub async fn execute(&self) -> api::ApiResult {
69            let res = format!("/{}/?{}", self.client.options.bucket, "logging");
70            let url = format!("{}/?{}", self.client.options.base_url(), "logging");
71            let config = self.config();
72            let data = oss::Bytes::from(config);
73
74            let resp = self
75                .client
76                .request
77                .task()
78                .with_url(&url)
79                .with_method(http::Method::PUT)
80                .with_body(data)
81                .with_resource(&res)
82                .execute()
83                .await?;
84            Ok(ApiResponseFrom(resp).to_empty().await)
85        }
86    }
87
88    pub struct GetBucketLoggingBuilder<'a> {
89        pub client: &'a oss::Client<'a>,
90    }
91
92    impl<'a> GetBucketLoggingBuilder<'a> {
93        pub(crate) fn new(client: &'a oss::Client) -> Self {
94            Self { client }
95        }
96
97        pub async fn execute(&self) -> api::ApiResult<BucketLoggingStatus> {
98            let res = format!("/{}/?{}", self.client.bucket(), "logging");
99            let url = format!("{}/?{}", self.client.base_url(), "logging");
100
101            let resp = self
102                .client
103                .request
104                .task()
105                .with_url(&url)
106                .with_resource(&res)
107                .execute()
108                .await?;
109            Ok(ApiResponseFrom(resp).to_type().await)
110        }
111    }
112
113    pub struct DeleteBucketLoggingBuilder<'a> {
114        pub client: &'a oss::Client<'a>,
115    }
116
117    impl<'a> DeleteBucketLoggingBuilder<'a> {
118        pub(crate) fn new(client: &'a oss::Client) -> Self {
119            Self { client }
120        }
121
122        pub async fn execute(&self) -> api::ApiResult {
123            let res = format!("/{}/?{}", self.client.bucket(), "logging");
124            let url = format!("{}/?{}", self.client.base_url(), "logging");
125
126            let resp = self
127                .client
128                .request
129                .task()
130                .with_url(&url)
131                .with_resource(&res)
132                .with_method(http::Method::DELETE)
133                .execute()
134                .await?;
135            Ok(ApiResponseFrom(resp).to_empty().await)
136        }
137    }
138}
139
140/// # 日志管理`Logging``
141#[allow(non_snake_case)]
142impl<'a> oss::Client<'a> {
143    /// PutBucketLogging接口用于为存储空间`Bucket`开启日志转存功能,
144    /// 可将OSS的访问日志按照固定命名规则,以小时为单位生成日志文件写入您
145    /// 指定的Bucket。
146    ///
147    /// - [official docs](https://help.aliyun.com/zh/oss/developer-reference/putbucketlogging)
148    /// - [xtoss example](https://github.com/isme-sun/xt_oss/blob/main/examples/api_bucket_logging_put.rs)
149    pub fn PutBucketLogging(&self) -> PutBucketLoggingBuilder {
150        PutBucketLoggingBuilder::new(self)
151    }
152
153    /// GetBucketLogging接口用于查看存储空间`Bucket`的访问日志配置。
154    /// 只有Bucket的拥有者才能查看Bucket的访问日志配置。
155    ///
156    /// - [official docs](https://help.aliyun.com/zh/oss/developer-reference/getbucketlogging)
157    /// - [xtoss example](https://github.com/isme-sun/xt_oss/blob/main/examples/api_bucket_logging_get.rs)
158    pub fn GetBucketLogging(&self) -> GetBucketLoggingBuilder {
159        GetBucketLoggingBuilder::new(&self)
160    }
161
162    /// DeleteBucketLogging用于关闭存储空间`Bucket`的访问日志记录功能。
163    /// 只有Bucket的拥有者才有权限关闭Bucket访问日志记录功能
164    ///
165    /// - [official docs](https://help.aliyun.com/zh/oss/developer-reference/deletebucketlogging)
166    /// - [xtoss example](https://github.com/isme-sun/xt_oss/blob/main/examples/api_bucket_logging_del.rs)
167    pub fn DeleteBucketLogging(&self) -> DeleteBucketLoggingBuilder {
168        DeleteBucketLoggingBuilder::new(&self)
169    }
170}