aliyun_oss_rs/bucket/
get_bucket_info.rs

1use crate::common::body_to_bytes;
2use crate::{
3    Error,
4    common::{Acl, DataRedundancyType, Owner, StorageClass},
5    error::normal_error,
6    request::{Oss, OssRequest},
7};
8use http::Method;
9use serde_derive::Deserialize;
10
11// Returned content
12#[derive(Debug, Deserialize)]
13#[serde(rename_all = "PascalCase")]
14pub(crate) struct BucketList {
15    pub bucket: BucketInfo,
16}
17
18/// Detailed bucket information
19#[derive(Debug, Deserialize)]
20#[serde(rename_all = "PascalCase")]
21pub struct BucketInfo {
22    /// Access monitoring status
23    pub access_monitor: String,
24    /// Comment
25    pub comment: String,
26    /// Creation date
27    pub creation_date: String,
28    /// Cross-region replication status
29    pub cross_region_replication: String,
30    /// Data redundancy type
31    pub data_redundancy_type: DataRedundancyType,
32    /// Public endpoint
33    pub extranet_endpoint: String,
34    /// Internal endpoint
35    pub intranet_endpoint: String,
36    /// Region
37    pub location: String,
38    /// Name
39    pub name: String,
40    /// Resource group
41    pub resource_group_id: String,
42    /// Storage class
43    pub storage_class: StorageClass,
44    /// Transfer acceleration status
45    pub transfer_acceleration: String,
46    /// Owner information
47    pub owner: Owner,
48    /// Access control
49    pub access_control_list: AccessControlList,
50    /// Server-side encryption information
51    pub server_side_encryption_rule: ServerSideEncryptionRule,
52    /// Logging information
53    pub bucket_policy: BucketPolicy,
54}
55
56/// Bucket access control information
57#[derive(Debug, Deserialize)]
58#[serde(rename_all = "PascalCase")]
59pub struct AccessControlList {
60    /// Access control
61    pub grant: Acl,
62}
63
64/// Bucket server-side encryption information
65#[derive(Debug, Deserialize)]
66pub struct ServerSideEncryptionRule {
67    /// Default server-side encryption algorithm
68    #[serde(rename = "SSEAlgorithm")]
69    pub sse_algorithm: String,
70}
71
72/// Bucket logging information
73#[derive(Debug, Deserialize)]
74#[serde(rename_all = "PascalCase")]
75pub struct BucketPolicy {
76    /// Name of the bucket storing logs
77    pub log_bucket: String,
78    /// Directory for storing log files
79    pub log_prefix: String,
80}
81
82/// Retrieve detailed information of the bucket
83///
84/// See the [Alibaba Cloud documentation](https://help.aliyun.com/document_detail/31968.html) for details
85pub struct GetBucketInfo {
86    req: OssRequest,
87}
88impl GetBucketInfo {
89    pub(super) fn new(oss: Oss) -> Self {
90        let mut req = OssRequest::new(oss, Method::GET);
91        req.insert_query("bucketInfo", "");
92        GetBucketInfo { req }
93    }
94    /// Send the request
95    pub async fn send(self) -> Result<BucketInfo, Error> {
96        // Build the HTTP request
97        let response = self.req.send_to_oss()?.await?;
98        // Parse the response
99        let status_code = response.status();
100        match status_code {
101            code if code.is_success() => {
102                let response_bytes = body_to_bytes(response.into_body())
103                    .await
104                    .map_err(|_| Error::OssInvalidResponse(None))?;
105                let bucket_info: BucketList = serde_xml_rs::from_reader(&*response_bytes)
106                    .map_err(|_| Error::OssInvalidResponse(Some(response_bytes)))?;
107                Ok(bucket_info.bucket)
108            }
109            _ => Err(normal_error(response).await),
110        }
111    }
112}