#[cfg(feature = "async")]
use super::{
AbortBucketWorm, CompleteBucketWorm, DelBucket, DelBucketCors, DelBucketEncryption,
DelBucketInventory, DelBucketLifecycle, DelBucketLogging, DelBucketPolicy, DelBucketReferer,
DelBucketWebsite, DelObjects, ExtendBucketWorm, GetBucketAcl, GetBucketCors,
GetBucketEncryption, GetBucketInfo, GetBucketInventory, GetBucketLifecycle, GetBucketLocation,
GetBucketLogging, GetBucketPolicy, GetBucketReferer, GetBucketStat, GetBucketWebsite,
GetBucketWorm, InitiateBucketWorm, ListBucketInventory, ListObjects, ListUploads, PutBucket,
PutBucketAcl, PutBucketCors, PutBucketEncryption, PutBucketInventory, PutBucketLifecycle,
PutBucketLogging, PutBucketPolicy, PutBucketReferer, PutBucketWebsite,
};
#[cfg(feature = "sync")]
use super::{
DelBucketLoggingSync, GetBucketAclSync, GetBucketLocationSync, GetBucketLoggingSync,
PutBucketAclSync, PutBucketLoggingSync,
};
#[cfg(feature = "async")]
use crate::OssObject;
use crate::oss::Oss;
#[derive(Debug, Clone)]
pub struct OssBucket {
pub(crate) oss: Oss,
}
impl OssBucket {
pub(crate) fn new(mut oss: Oss, bucket: impl ToString, endpoint: impl ToString) -> Self {
oss.set_bucket(bucket);
oss.set_endpoint(endpoint);
OssBucket { oss }
}
pub fn with_security_token(mut self, token: impl Into<String>) -> Self {
self.oss.set_security_token(token);
self
}
pub fn set_security_token(&mut self, token: impl Into<String>) {
self.oss.set_security_token(token);
}
pub fn set_custom_domain(mut self, custom_domain: impl ToString, enable_https: bool) -> Self {
self.oss.set_custom_domain(custom_domain);
self.oss.set_https(enable_https);
self
}
#[cfg(feature = "async")]
pub fn object(&self, object: impl ToString) -> OssObject {
OssObject::new(self.oss.clone(), object)
}
#[cfg(feature = "async")]
pub fn put_bucket(&self) -> PutBucket {
PutBucket::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn del_bucket(&self) -> DelBucket {
DelBucket::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn list_objects(&self) -> ListObjects {
ListObjects::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn get_bucket_info(&self) -> GetBucketInfo {
GetBucketInfo::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn get_bucket_stat(&self) -> GetBucketStat {
GetBucketStat::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn get_bucket_lifecycle(&self) -> GetBucketLifecycle {
GetBucketLifecycle::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn put_bucket_lifecycle(&self) -> PutBucketLifecycle {
PutBucketLifecycle::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn del_bucket_lifecycle(&self) -> DelBucketLifecycle {
DelBucketLifecycle::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn get_bucket_location(&self) -> GetBucketLocation {
GetBucketLocation::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn get_bucket_acl(&self) -> GetBucketAcl {
GetBucketAcl::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn put_bucket_acl(&self) -> PutBucketAcl {
PutBucketAcl::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn get_bucket_referer(&self) -> GetBucketReferer {
GetBucketReferer::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn put_bucket_referer(&self) -> PutBucketReferer {
PutBucketReferer::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn del_bucket_referer(&self) -> DelBucketReferer {
DelBucketReferer::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn get_bucket_policy(&self) -> GetBucketPolicy {
GetBucketPolicy::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn put_bucket_policy(&self) -> PutBucketPolicy {
PutBucketPolicy::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn del_bucket_policy(&self) -> DelBucketPolicy {
DelBucketPolicy::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn get_bucket_encryption(&self) -> GetBucketEncryption {
GetBucketEncryption::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn put_bucket_encryption(&self) -> PutBucketEncryption {
PutBucketEncryption::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn del_bucket_encryption(&self) -> DelBucketEncryption {
DelBucketEncryption::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn initiate_bucket_worm(&self) -> InitiateBucketWorm {
InitiateBucketWorm::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn get_bucket_worm(&self) -> GetBucketWorm {
GetBucketWorm::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn complete_bucket_worm(&self, worm_id: impl ToString) -> CompleteBucketWorm {
CompleteBucketWorm::new(self.oss.clone(), worm_id)
}
#[cfg(feature = "async")]
pub fn extend_bucket_worm(&self, worm_id: impl ToString) -> ExtendBucketWorm {
ExtendBucketWorm::new(self.oss.clone(), worm_id)
}
#[cfg(feature = "async")]
pub fn abort_bucket_worm(&self, worm_id: impl ToString) -> AbortBucketWorm {
AbortBucketWorm::new(self.oss.clone(), worm_id)
}
#[cfg(feature = "async")]
pub fn put_bucket_inventory(&self, inventory_id: impl ToString) -> PutBucketInventory {
PutBucketInventory::new(self.oss.clone(), inventory_id)
}
#[cfg(feature = "async")]
pub fn get_bucket_inventory(&self, inventory_id: impl ToString) -> GetBucketInventory {
GetBucketInventory::new(self.oss.clone(), inventory_id)
}
#[cfg(feature = "async")]
pub fn del_bucket_inventory(&self, inventory_id: impl ToString) -> DelBucketInventory {
DelBucketInventory::new(self.oss.clone(), inventory_id)
}
#[cfg(feature = "async")]
pub fn list_bucket_inventory(&self) -> ListBucketInventory {
ListBucketInventory::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn get_bucket_website(&self) -> GetBucketWebsite {
GetBucketWebsite::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn put_bucket_website(&self) -> PutBucketWebsite {
PutBucketWebsite::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn del_bucket_website(&self) -> DelBucketWebsite {
DelBucketWebsite::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn get_bucket_cors(&self) -> GetBucketCors {
GetBucketCors::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn put_bucket_cors(&self) -> PutBucketCors {
PutBucketCors::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn del_bucket_cors(&self) -> DelBucketCors {
DelBucketCors::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn get_bucket_logging(&self) -> GetBucketLogging {
GetBucketLogging::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn put_bucket_logging(
&self,
target_bucket: impl ToString,
target_prefix: impl ToString,
) -> PutBucketLogging {
PutBucketLogging::new(self.oss.clone(), target_bucket, target_prefix)
}
#[cfg(feature = "async")]
pub fn del_bucket_logging(&self) -> DelBucketLogging {
DelBucketLogging::new(self.oss.clone())
}
#[cfg(feature = "sync")]
pub fn get_bucket_location_sync(&self) -> GetBucketLocationSync {
GetBucketLocationSync::new(self.oss.clone())
}
#[cfg(feature = "sync")]
pub fn get_bucket_acl_sync(&self) -> GetBucketAclSync {
GetBucketAclSync::new(self.oss.clone())
}
#[cfg(feature = "sync")]
pub fn put_bucket_acl_sync(&self) -> PutBucketAclSync {
PutBucketAclSync::new(self.oss.clone())
}
#[cfg(feature = "sync")]
pub fn get_bucket_logging_sync(&self) -> GetBucketLoggingSync {
GetBucketLoggingSync::new(self.oss.clone())
}
#[cfg(feature = "sync")]
pub fn put_bucket_logging_sync(
&self,
target_bucket: impl ToString,
target_prefix: impl ToString,
) -> PutBucketLoggingSync {
PutBucketLoggingSync::new(self.oss.clone(), target_bucket, target_prefix)
}
#[cfg(feature = "sync")]
pub fn del_bucket_logging_sync(&self) -> DelBucketLoggingSync {
DelBucketLoggingSync::new(self.oss.clone())
}
#[cfg(feature = "async")]
pub fn del_objects(&self, files: Vec<impl ToString>) -> DelObjects {
DelObjects::new(self.oss.clone(), files)
}
#[cfg(feature = "async")]
pub fn multipart_list_uploads(&self) -> ListUploads {
ListUploads::new(self.oss.clone())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_bucket_creation_and_custom_domain() {
let bucket = OssBucket::new(
Oss::new("id", "secret"),
"my-bucket",
"oss-cn-example.aliyuncs.com",
)
.with_security_token("token")
.set_custom_domain("cdn.example.com", false);
assert_eq!(bucket.oss.bucket.as_deref(), Some("my-bucket"));
assert_eq!(bucket.oss.endpoint.as_ref(), "oss-cn-example.aliyuncs.com");
assert_eq!(bucket.oss.custom_domain.as_deref(), Some("cdn.example.com"));
assert!(!bucket.oss.enable_https);
assert_eq!(bucket.oss.security_token.as_deref(), Some("token"));
let mut bucket = bucket.clone();
bucket.set_security_token("token2");
assert_eq!(bucket.oss.security_token.as_deref(), Some("token2"));
}
}