xt_oss/oss/api/bucket/
encryption.rs

1use crate::oss;
2
3use self::builders::{
4    DeleteBucketEncryptionBuilder, GetBucketEncryptionBuilder, PutBucketEncryptionBuilder,
5};
6
7pub mod builders {
8    use crate::oss::{
9        self,
10        api::{self, ApiResponseFrom},
11        entities::encryption::{
12            ApplyServerSideEncryptionByDefault, SSEAlgorithm, ServerSideEncryptionRule,
13        },
14        http,
15    };
16
17    #[allow(unused)]
18    pub struct PutBucketEncryptionBuilder<'a> {
19        client: &'a oss::Client<'a>,
20        algorithm: SSEAlgorithm,
21        data_encryption: Option<&'a str>,
22        master_key_id: Option<&'a str>,
23    }
24
25    #[allow(unused)]
26    impl<'a> PutBucketEncryptionBuilder<'a> {
27        pub(crate) fn new(client: &'a oss::Client) -> Self {
28            Self {
29                client,
30                algorithm: SSEAlgorithm::default(),
31                data_encryption: None,
32                master_key_id: None,
33            }
34        }
35
36        pub fn with_algorithm(mut self, value: SSEAlgorithm) -> Self {
37            self.algorithm = value;
38            self
39        }
40
41        pub fn with_data_encryption(mut self, value: &'a str) -> Self {
42            self.data_encryption = Some(value);
43            self
44        }
45
46        pub fn with_master_key_id(mut self, value: &'a str) -> Self {
47            self.master_key_id = Some(value);
48            self
49        }
50
51        pub async fn execute(&self) -> api::ApiResult {
52            let res = format!("/{}/?{}", self.client.bucket(), "encryption");
53            let url = format!("{}/?{}", self.client.base_url(), "encryption");
54
55            let mut content = ServerSideEncryptionRule {
56                apply_server_side_encryption_by_default: ApplyServerSideEncryptionByDefault {
57                    sse_algorithm: self.algorithm,
58                    kms_data_encryption: self.data_encryption.map(|enc| enc.into()),
59                    kms_master_key_id: self.master_key_id.map(|key_id| key_id.into()),
60                },
61            };
62
63            let data = oss::Bytes::from(quick_xml::se::to_string(&content).unwrap());
64
65            let resp = self
66                .client
67                .request
68                .task()
69                .with_url(&url)
70                .with_method(http::Method::PUT)
71                .with_resource(&res)
72                .with_body(data)
73                .execute()
74                .await?;
75
76            Ok(ApiResponseFrom(resp).to_empty().await)
77        }
78    }
79
80    pub struct GetBucketEncryptionBuilder<'a> {
81        client: &'a oss::Client<'a>,
82    }
83
84    impl<'a> GetBucketEncryptionBuilder<'a> {
85        pub fn new(client: &'a oss::Client) -> Self {
86            Self { client }
87        }
88
89        pub async fn execute(&self) -> api::ApiResult<ServerSideEncryptionRule> {
90            let res = format!("/{}/?{}", self.client.bucket(), "encryption");
91            let url = format!("{}/?{}", self.client.base_url(), "encryption");
92            let resp = self
93                .client
94                .request
95                .task()
96                .with_url(&url)
97                .with_resource(&res)
98                .execute()
99                .await?;
100
101            Ok(ApiResponseFrom(resp).to_type().await)
102        }
103    }
104
105    pub struct DeleteBucketEncryptionBuilder<'a> {
106        client: &'a oss::Client<'a>,
107    }
108
109    impl<'a> DeleteBucketEncryptionBuilder<'a> {
110        pub fn new(client: &'a oss::Client) -> Self {
111            Self { client }
112        }
113
114        pub async fn execute(&self) -> api::ApiResult {
115            let res = format!("/{}/?{}", self.client.bucket(), "encryption");
116            let url = format!("{}/?{}", self.client.base_url(), "encryption");
117            let resp = self
118                .client
119                .request
120                .task()
121                .with_url(&url)
122                .with_method(http::Method::DELETE)
123                .with_resource(&res)
124                .execute()
125                .await?;
126
127            Ok(ApiResponseFrom(resp).to_empty().await)
128        }
129    }
130}
131
132/// # 加密(Encryption)
133#[allow(non_snake_case)]
134impl<'a> oss::Client<'a> {
135    /// PutBucketEncryption接口用于配置存储空间`Bucket`的加密规则。
136    ///
137    /// - [official docs](https://help.aliyun.com/zh/oss/developer-reference/putbucketencryption)
138    /// - [xtoss example](https://github.com/isme-sun/xt_oss/blob/main/examples/api_bucket_encryption_put.rs)
139    pub fn PutBucketEncryption(&self) -> PutBucketEncryptionBuilder {
140        PutBucketEncryptionBuilder::new(self)
141    }
142
143    /// GetBucketEncryption接口用于获取存储空间`Bucket`的加密规则。
144    ///
145    /// - [official docs](https://help.aliyun.com/zh/oss/developer-reference/getbucketencryption)
146    /// - [xtoss example](https://github.com/isme-sun/xt_oss/blob/main/examples/api_bucket_encryption_get.rs)
147    pub fn GetBucketEncryption(&self) -> GetBucketEncryptionBuilder {
148        GetBucketEncryptionBuilder::new(&self)
149    }
150
151    /// DeleteBucketEncryption接口用于删除Bucket加密规则。
152    ///
153    /// - [official docs](https://help.aliyun.com/zh/oss/developer-reference/deletebucketencryption)
154    /// - [xtoss example](https://github.com/isme-sun/xt_oss/blob/main/examples/api_bucket_encryption_del.rs)
155    pub fn DeleteBucketEncryption(&self) -> DeleteBucketEncryptionBuilder {
156        DeleteBucketEncryptionBuilder::new(&self)
157    }
158}