aliyun_oss_rs/bucket/
oss_bucket.rs

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/// OSS bucket implementing APIs for creating buckets, retrieving bucket information, listing objects, and more
22#[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    /// Attach a temporary security token for STS authentication
34    pub fn with_security_token(mut self, token: impl Into<String>) -> Self {
35        self.oss.set_security_token(token);
36        self
37    }
38    /// Update security token without consuming the bucket
39    pub fn set_security_token(&mut self, token: impl Into<String>) {
40        self.oss.set_security_token(token);
41    }
42    /// Set a custom domain
43    ///
44    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    /// Initialize an OssObject
50    #[cfg(feature = "async")]
51    pub fn object(&self, object: impl ToString) -> OssObject {
52        OssObject::new(self.oss.clone(), object)
53    }
54    /// Create a bucket
55    #[cfg(feature = "async")]
56    pub fn put_bucket(&self) -> PutBucket {
57        PutBucket::new(self.oss.clone())
58    }
59    /// Delete a bucket
60    #[cfg(feature = "async")]
61    pub fn del_bucket(&self) -> DelBucket {
62        DelBucket::new(self.oss.clone())
63    }
64    /// List all objects in the bucket
65    #[cfg(feature = "async")]
66    pub fn list_objects(&self) -> ListObjects {
67        ListObjects::new(self.oss.clone())
68    }
69    /// Retrieve detailed information of the bucket
70    #[cfg(feature = "async")]
71    pub fn get_bucket_info(&self) -> GetBucketInfo {
72        GetBucketInfo::new(self.oss.clone())
73    }
74    /// Retrieve the bucket's storage size and file count
75    #[cfg(feature = "async")]
76    pub fn get_bucket_stat(&self) -> GetBucketStat {
77        GetBucketStat::new(self.oss.clone())
78    }
79    /// Retrieve the lifecycle rules configured on the bucket
80    #[cfg(feature = "async")]
81    pub fn get_bucket_lifecycle(&self) -> GetBucketLifecycle {
82        GetBucketLifecycle::new(self.oss.clone())
83    }
84    /// Configure lifecycle rules for the bucket
85    #[cfg(feature = "async")]
86    pub fn put_bucket_lifecycle(&self) -> PutBucketLifecycle {
87        PutBucketLifecycle::new(self.oss.clone())
88    }
89    /// Delete all lifecycle rules for the bucket
90    #[cfg(feature = "async")]
91    pub fn del_bucket_lifecycle(&self) -> DelBucketLifecycle {
92        DelBucketLifecycle::new(self.oss.clone())
93    }
94    /// Retrieve the bucket location
95    #[cfg(feature = "async")]
96    pub fn get_bucket_location(&self) -> GetBucketLocation {
97        GetBucketLocation::new(self.oss.clone())
98    }
99    /// Get the bucket access control
100    #[cfg(feature = "async")]
101    pub fn get_bucket_acl(&self) -> GetBucketAcl {
102        GetBucketAcl::new(self.oss.clone())
103    }
104    /// Set the bucket access control
105    #[cfg(feature = "async")]
106    pub fn put_bucket_acl(&self) -> PutBucketAcl {
107        PutBucketAcl::new(self.oss.clone())
108    }
109    /// Retrieve the referer configuration
110    #[cfg(feature = "async")]
111    pub fn get_bucket_referer(&self) -> GetBucketReferer {
112        GetBucketReferer::new(self.oss.clone())
113    }
114    /// Configure the referer whitelist
115    #[cfg(feature = "async")]
116    pub fn put_bucket_referer(&self) -> PutBucketReferer {
117        PutBucketReferer::new(self.oss.clone())
118    }
119    /// Delete the referer configuration
120    #[cfg(feature = "async")]
121    pub fn del_bucket_referer(&self) -> DelBucketReferer {
122        DelBucketReferer::new(self.oss.clone())
123    }
124    /// Retrieve the bucket policy
125    #[cfg(feature = "async")]
126    pub fn get_bucket_policy(&self) -> GetBucketPolicy {
127        GetBucketPolicy::new(self.oss.clone())
128    }
129    /// Configure the bucket policy
130    #[cfg(feature = "async")]
131    pub fn put_bucket_policy(&self) -> PutBucketPolicy {
132        PutBucketPolicy::new(self.oss.clone())
133    }
134    /// Delete the bucket policy
135    #[cfg(feature = "async")]
136    pub fn del_bucket_policy(&self) -> DelBucketPolicy {
137        DelBucketPolicy::new(self.oss.clone())
138    }
139    /// Retrieve the bucket encryption configuration
140    #[cfg(feature = "async")]
141    pub fn get_bucket_encryption(&self) -> GetBucketEncryption {
142        GetBucketEncryption::new(self.oss.clone())
143    }
144    /// Configure default bucket encryption
145    #[cfg(feature = "async")]
146    pub fn put_bucket_encryption(&self) -> PutBucketEncryption {
147        PutBucketEncryption::new(self.oss.clone())
148    }
149    /// Delete the bucket encryption configuration
150    #[cfg(feature = "async")]
151    pub fn del_bucket_encryption(&self) -> DelBucketEncryption {
152        DelBucketEncryption::new(self.oss.clone())
153    }
154    /// Initiate a WORM retention configuration
155    #[cfg(feature = "async")]
156    pub fn initiate_bucket_worm(&self) -> InitiateBucketWorm {
157        InitiateBucketWorm::new(self.oss.clone())
158    }
159    /// Retrieve the WORM configuration
160    #[cfg(feature = "async")]
161    pub fn get_bucket_worm(&self) -> GetBucketWorm {
162        GetBucketWorm::new(self.oss.clone())
163    }
164    /// Complete a WORM configuration
165    #[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    /// Extend a WORM configuration
170    #[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    /// Abort a WORM configuration
175    #[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    /// Configure an inventory task
180    #[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    /// Retrieve an inventory task configuration
185    #[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    /// Delete an inventory task configuration
190    #[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    /// List inventory task configurations
195    #[cfg(feature = "async")]
196    pub fn list_bucket_inventory(&self) -> ListBucketInventory {
197        ListBucketInventory::new(self.oss.clone())
198    }
199    /// Retrieve the static website configuration
200    #[cfg(feature = "async")]
201    pub fn get_bucket_website(&self) -> GetBucketWebsite {
202        GetBucketWebsite::new(self.oss.clone())
203    }
204    /// Configure the static website behavior
205    #[cfg(feature = "async")]
206    pub fn put_bucket_website(&self) -> PutBucketWebsite {
207        PutBucketWebsite::new(self.oss.clone())
208    }
209    /// Delete the static website configuration
210    #[cfg(feature = "async")]
211    pub fn del_bucket_website(&self) -> DelBucketWebsite {
212        DelBucketWebsite::new(self.oss.clone())
213    }
214    /// Retrieve the CORS rules configured on the bucket
215    #[cfg(feature = "async")]
216    pub fn get_bucket_cors(&self) -> GetBucketCors {
217        GetBucketCors::new(self.oss.clone())
218    }
219    /// Replace the CORS rules configured on the bucket
220    #[cfg(feature = "async")]
221    pub fn put_bucket_cors(&self) -> PutBucketCors {
222        PutBucketCors::new(self.oss.clone())
223    }
224    /// Delete all CORS rules configured on the bucket
225    #[cfg(feature = "async")]
226    pub fn del_bucket_cors(&self) -> DelBucketCors {
227        DelBucketCors::new(self.oss.clone())
228    }
229    /// Get the bucket logging configuration
230    #[cfg(feature = "async")]
231    pub fn get_bucket_logging(&self) -> GetBucketLogging {
232        GetBucketLogging::new(self.oss.clone())
233    }
234    /// Configure the bucket logging
235    #[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    /// Delete the bucket logging configuration
244    #[cfg(feature = "async")]
245    pub fn del_bucket_logging(&self) -> DelBucketLogging {
246        DelBucketLogging::new(self.oss.clone())
247    }
248
249    #[cfg(feature = "sync")]
250    /// Retrieve the bucket location (synchronous)
251    pub fn get_bucket_location_sync(&self) -> GetBucketLocationSync {
252        GetBucketLocationSync::new(self.oss.clone())
253    }
254    #[cfg(feature = "sync")]
255    /// Get the bucket access control (synchronous)
256    pub fn get_bucket_acl_sync(&self) -> GetBucketAclSync {
257        GetBucketAclSync::new(self.oss.clone())
258    }
259    #[cfg(feature = "sync")]
260    /// Set the bucket access control (synchronous)
261    pub fn put_bucket_acl_sync(&self) -> PutBucketAclSync {
262        PutBucketAclSync::new(self.oss.clone())
263    }
264    #[cfg(feature = "sync")]
265    /// Get the bucket logging configuration (synchronous)
266    pub fn get_bucket_logging_sync(&self) -> GetBucketLoggingSync {
267        GetBucketLoggingSync::new(self.oss.clone())
268    }
269    #[cfg(feature = "sync")]
270    /// Configure the bucket logging (synchronous)
271    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    /// Delete the bucket logging configuration (synchronous)
280    pub fn del_bucket_logging_sync(&self) -> DelBucketLoggingSync {
281        DelBucketLoggingSync::new(self.oss.clone())
282    }
283    /// Delete multiple objects
284    #[cfg(feature = "async")]
285    pub fn del_objects(&self, files: Vec<impl ToString>) -> DelObjects {
286        DelObjects::new(self.oss.clone(), files)
287    }
288    /// List multipart uploads that have been initiated but not completed
289    #[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}