aliyun_oss_rs/bucket/
get_bucket_location.rs

1use crate::common::body_to_bytes;
2use crate::{
3    Error,
4    error::normal_error,
5    request::{Oss, OssRequest},
6};
7use http::Method;
8use serde_derive::Deserialize;
9
10#[derive(Debug, Deserialize)]
11#[serde(rename_all = "PascalCase")]
12struct LocationConstraint {
13    #[serde(rename = "$value")]
14    pub location: String,
15}
16
17/// Retrieve bucket location information
18///
19/// See the [Alibaba Cloud documentation](https://help.aliyun.com/zh/oss/developer-reference/getbucketlocation) for details
20pub struct GetBucketLocation {
21    req: OssRequest,
22}
23impl GetBucketLocation {
24    pub(super) fn new(oss: Oss) -> Self {
25        let mut req = OssRequest::new(oss, Method::GET);
26        req.insert_query("location", "");
27        GetBucketLocation { req }
28    }
29    /// Send the request
30    pub async fn send(self) -> Result<String, Error> {
31        let response = self.req.send_to_oss()?.await?;
32        let status_code = response.status();
33        match status_code {
34            code if code.is_success() => {
35                let response_bytes = body_to_bytes(response.into_body())
36                    .await
37                    .map_err(|_| Error::OssInvalidResponse(None))?;
38                let location: LocationConstraint = serde_xml_rs::from_reader(&*response_bytes)
39                    .map_err(|_| Error::OssInvalidResponse(Some(response_bytes)))?;
40                Ok(location.location)
41            }
42            _ => Err(normal_error(response).await),
43        }
44    }
45}