1#[cfg(feature = "async")]
2use super::{
3 AbortBucketWorm, CompleteBucketWorm, DelBucket, DelBucketCors, DelBucketEncryption,
4 DelBucketInventory, DelBucketLifecycle, DelBucketLogging, DelBucketPolicy, DelBucketReferer,
5 DelBucketWebsite, DelObjects, ExtendBucketWorm, GetBucketAcl, GetBucketCors,
6 GetBucketEncryption, GetBucketInfo, GetBucketInventory, GetBucketLifecycle, GetBucketLocation,
7 GetBucketLogging, GetBucketPolicy, GetBucketReferer, GetBucketStat, GetBucketWebsite,
8 GetBucketWorm, InitiateBucketWorm, ListBucketInventory, ListObjects, ListUploads, PutBucket,
9 PutBucketAcl, PutBucketCors, PutBucketEncryption, PutBucketInventory, PutBucketLifecycle,
10 PutBucketLogging, PutBucketPolicy, PutBucketReferer, PutBucketWebsite,
11};
12#[cfg(feature = "sync")]
13use super::{
14 DelBucketLoggingSync, GetBucketAclSync, GetBucketLocationSync, GetBucketLoggingSync,
15 PutBucketAclSync, PutBucketLoggingSync,
16};
17#[cfg(feature = "async")]
18use crate::OssObject;
19use crate::oss::Oss;
20
21#[derive(Debug, Clone)]
23pub struct OssBucket {
24 pub(crate) oss: Oss,
25}
26
27impl OssBucket {
28 pub(crate) fn new(mut oss: Oss, bucket: impl ToString, endpoint: impl ToString) -> Self {
29 oss.set_bucket(bucket);
30 oss.set_endpoint(endpoint);
31 OssBucket { oss }
32 }
33 pub fn with_security_token(mut self, token: impl Into<String>) -> Self {
35 self.oss.set_security_token(token);
36 self
37 }
38 pub fn set_security_token(&mut self, token: impl Into<String>) {
40 self.oss.set_security_token(token);
41 }
42 pub fn set_custom_domain(mut self, custom_domain: impl ToString, enable_https: bool) -> Self {
45 self.oss.set_custom_domain(custom_domain);
46 self.oss.set_https(enable_https);
47 self
48 }
49 #[cfg(feature = "async")]
51 pub fn object(&self, object: impl ToString) -> OssObject {
52 OssObject::new(self.oss.clone(), object)
53 }
54 #[cfg(feature = "async")]
56 pub fn put_bucket(&self) -> PutBucket {
57 PutBucket::new(self.oss.clone())
58 }
59 #[cfg(feature = "async")]
61 pub fn del_bucket(&self) -> DelBucket {
62 DelBucket::new(self.oss.clone())
63 }
64 #[cfg(feature = "async")]
66 pub fn list_objects(&self) -> ListObjects {
67 ListObjects::new(self.oss.clone())
68 }
69 #[cfg(feature = "async")]
71 pub fn get_bucket_info(&self) -> GetBucketInfo {
72 GetBucketInfo::new(self.oss.clone())
73 }
74 #[cfg(feature = "async")]
76 pub fn get_bucket_stat(&self) -> GetBucketStat {
77 GetBucketStat::new(self.oss.clone())
78 }
79 #[cfg(feature = "async")]
81 pub fn get_bucket_lifecycle(&self) -> GetBucketLifecycle {
82 GetBucketLifecycle::new(self.oss.clone())
83 }
84 #[cfg(feature = "async")]
86 pub fn put_bucket_lifecycle(&self) -> PutBucketLifecycle {
87 PutBucketLifecycle::new(self.oss.clone())
88 }
89 #[cfg(feature = "async")]
91 pub fn del_bucket_lifecycle(&self) -> DelBucketLifecycle {
92 DelBucketLifecycle::new(self.oss.clone())
93 }
94 #[cfg(feature = "async")]
96 pub fn get_bucket_location(&self) -> GetBucketLocation {
97 GetBucketLocation::new(self.oss.clone())
98 }
99 #[cfg(feature = "async")]
101 pub fn get_bucket_acl(&self) -> GetBucketAcl {
102 GetBucketAcl::new(self.oss.clone())
103 }
104 #[cfg(feature = "async")]
106 pub fn put_bucket_acl(&self) -> PutBucketAcl {
107 PutBucketAcl::new(self.oss.clone())
108 }
109 #[cfg(feature = "async")]
111 pub fn get_bucket_referer(&self) -> GetBucketReferer {
112 GetBucketReferer::new(self.oss.clone())
113 }
114 #[cfg(feature = "async")]
116 pub fn put_bucket_referer(&self) -> PutBucketReferer {
117 PutBucketReferer::new(self.oss.clone())
118 }
119 #[cfg(feature = "async")]
121 pub fn del_bucket_referer(&self) -> DelBucketReferer {
122 DelBucketReferer::new(self.oss.clone())
123 }
124 #[cfg(feature = "async")]
126 pub fn get_bucket_policy(&self) -> GetBucketPolicy {
127 GetBucketPolicy::new(self.oss.clone())
128 }
129 #[cfg(feature = "async")]
131 pub fn put_bucket_policy(&self) -> PutBucketPolicy {
132 PutBucketPolicy::new(self.oss.clone())
133 }
134 #[cfg(feature = "async")]
136 pub fn del_bucket_policy(&self) -> DelBucketPolicy {
137 DelBucketPolicy::new(self.oss.clone())
138 }
139 #[cfg(feature = "async")]
141 pub fn get_bucket_encryption(&self) -> GetBucketEncryption {
142 GetBucketEncryption::new(self.oss.clone())
143 }
144 #[cfg(feature = "async")]
146 pub fn put_bucket_encryption(&self) -> PutBucketEncryption {
147 PutBucketEncryption::new(self.oss.clone())
148 }
149 #[cfg(feature = "async")]
151 pub fn del_bucket_encryption(&self) -> DelBucketEncryption {
152 DelBucketEncryption::new(self.oss.clone())
153 }
154 #[cfg(feature = "async")]
156 pub fn initiate_bucket_worm(&self) -> InitiateBucketWorm {
157 InitiateBucketWorm::new(self.oss.clone())
158 }
159 #[cfg(feature = "async")]
161 pub fn get_bucket_worm(&self) -> GetBucketWorm {
162 GetBucketWorm::new(self.oss.clone())
163 }
164 #[cfg(feature = "async")]
166 pub fn complete_bucket_worm(&self, worm_id: impl ToString) -> CompleteBucketWorm {
167 CompleteBucketWorm::new(self.oss.clone(), worm_id)
168 }
169 #[cfg(feature = "async")]
171 pub fn extend_bucket_worm(&self, worm_id: impl ToString) -> ExtendBucketWorm {
172 ExtendBucketWorm::new(self.oss.clone(), worm_id)
173 }
174 #[cfg(feature = "async")]
176 pub fn abort_bucket_worm(&self, worm_id: impl ToString) -> AbortBucketWorm {
177 AbortBucketWorm::new(self.oss.clone(), worm_id)
178 }
179 #[cfg(feature = "async")]
181 pub fn put_bucket_inventory(&self, inventory_id: impl ToString) -> PutBucketInventory {
182 PutBucketInventory::new(self.oss.clone(), inventory_id)
183 }
184 #[cfg(feature = "async")]
186 pub fn get_bucket_inventory(&self, inventory_id: impl ToString) -> GetBucketInventory {
187 GetBucketInventory::new(self.oss.clone(), inventory_id)
188 }
189 #[cfg(feature = "async")]
191 pub fn del_bucket_inventory(&self, inventory_id: impl ToString) -> DelBucketInventory {
192 DelBucketInventory::new(self.oss.clone(), inventory_id)
193 }
194 #[cfg(feature = "async")]
196 pub fn list_bucket_inventory(&self) -> ListBucketInventory {
197 ListBucketInventory::new(self.oss.clone())
198 }
199 #[cfg(feature = "async")]
201 pub fn get_bucket_website(&self) -> GetBucketWebsite {
202 GetBucketWebsite::new(self.oss.clone())
203 }
204 #[cfg(feature = "async")]
206 pub fn put_bucket_website(&self) -> PutBucketWebsite {
207 PutBucketWebsite::new(self.oss.clone())
208 }
209 #[cfg(feature = "async")]
211 pub fn del_bucket_website(&self) -> DelBucketWebsite {
212 DelBucketWebsite::new(self.oss.clone())
213 }
214 #[cfg(feature = "async")]
216 pub fn get_bucket_cors(&self) -> GetBucketCors {
217 GetBucketCors::new(self.oss.clone())
218 }
219 #[cfg(feature = "async")]
221 pub fn put_bucket_cors(&self) -> PutBucketCors {
222 PutBucketCors::new(self.oss.clone())
223 }
224 #[cfg(feature = "async")]
226 pub fn del_bucket_cors(&self) -> DelBucketCors {
227 DelBucketCors::new(self.oss.clone())
228 }
229 #[cfg(feature = "async")]
231 pub fn get_bucket_logging(&self) -> GetBucketLogging {
232 GetBucketLogging::new(self.oss.clone())
233 }
234 #[cfg(feature = "async")]
236 pub fn put_bucket_logging(
237 &self,
238 target_bucket: impl ToString,
239 target_prefix: impl ToString,
240 ) -> PutBucketLogging {
241 PutBucketLogging::new(self.oss.clone(), target_bucket, target_prefix)
242 }
243 #[cfg(feature = "async")]
245 pub fn del_bucket_logging(&self) -> DelBucketLogging {
246 DelBucketLogging::new(self.oss.clone())
247 }
248
249 #[cfg(feature = "sync")]
250 pub fn get_bucket_location_sync(&self) -> GetBucketLocationSync {
252 GetBucketLocationSync::new(self.oss.clone())
253 }
254 #[cfg(feature = "sync")]
255 pub fn get_bucket_acl_sync(&self) -> GetBucketAclSync {
257 GetBucketAclSync::new(self.oss.clone())
258 }
259 #[cfg(feature = "sync")]
260 pub fn put_bucket_acl_sync(&self) -> PutBucketAclSync {
262 PutBucketAclSync::new(self.oss.clone())
263 }
264 #[cfg(feature = "sync")]
265 pub fn get_bucket_logging_sync(&self) -> GetBucketLoggingSync {
267 GetBucketLoggingSync::new(self.oss.clone())
268 }
269 #[cfg(feature = "sync")]
270 pub fn put_bucket_logging_sync(
272 &self,
273 target_bucket: impl ToString,
274 target_prefix: impl ToString,
275 ) -> PutBucketLoggingSync {
276 PutBucketLoggingSync::new(self.oss.clone(), target_bucket, target_prefix)
277 }
278 #[cfg(feature = "sync")]
279 pub fn del_bucket_logging_sync(&self) -> DelBucketLoggingSync {
281 DelBucketLoggingSync::new(self.oss.clone())
282 }
283 #[cfg(feature = "async")]
285 pub fn del_objects(&self, files: Vec<impl ToString>) -> DelObjects {
286 DelObjects::new(self.oss.clone(), files)
287 }
288 #[cfg(feature = "async")]
290 pub fn multipart_list_uploads(&self) -> ListUploads {
291 ListUploads::new(self.oss.clone())
292 }
293}
294
295#[cfg(test)]
296mod tests {
297 use super::*;
298
299 #[test]
300 fn test_bucket_creation_and_custom_domain() {
301 let bucket = OssBucket::new(
302 Oss::new("id", "secret"),
303 "my-bucket",
304 "oss-cn-example.aliyuncs.com",
305 )
306 .with_security_token("token")
307 .set_custom_domain("cdn.example.com", false);
308 assert_eq!(bucket.oss.bucket.as_deref(), Some("my-bucket"));
309 assert_eq!(bucket.oss.endpoint.as_ref(), "oss-cn-example.aliyuncs.com");
310 assert_eq!(bucket.oss.custom_domain.as_deref(), Some("cdn.example.com"));
311 assert!(!bucket.oss.enable_https);
312 assert_eq!(bucket.oss.security_token.as_deref(), Some("token"));
313
314 let mut bucket = bucket.clone();
315 bucket.set_security_token("token2");
316 assert_eq!(bucket.oss.security_token.as_deref(), Some("token2"));
317 }
318}