aliyun_oss_rs/object/
restore_object.rs

1use crate::{
2    Error,
3    common::RestoreTier,
4    error::normal_error,
5    request::{Oss, OssRequest},
6};
7use bytes::Bytes;
8use http::Method;
9use http_body_util::Full;
10
11/// Restore an archived object
12///
13/// See the [Alibaba Cloud documentation](https://help.aliyun.com/document_detail/52930.html) for details
14pub struct RestoreObject {
15    req: OssRequest,
16    days: Option<u32>,
17    tier: Option<RestoreTier>,
18}
19impl RestoreObject {
20    pub(super) fn new(oss: Oss) -> Self {
21        let mut req = OssRequest::new(oss, Method::POST);
22        req.insert_query("restore", "");
23        RestoreObject {
24            req,
25            days: None,
26            tier: None,
27        }
28    }
29    /// Set the number of days to keep the restored copy
30    ///
31    pub fn set_days(mut self, days: u32) -> Self {
32        self.days = Some(days);
33        self
34    }
35    /// Set the restore priority
36    ///
37    pub fn set_tier(mut self, tier: RestoreTier) -> Self {
38        self.tier = Some(tier);
39        self
40    }
41    /// Send the request
42    ///
43    pub async fn send(mut self) -> Result<(), Error> {
44        // Build the body
45        let days_str = self
46            .days
47            .map(|v| format!("<Days>{}</Days>", v))
48            .unwrap_or_else(|| String::new());
49        let tier_str = self
50            .tier
51            .map(|v| format!("<JobParameters><Tier>{}</Tier></JobParameters>", v))
52            .unwrap_or_else(|| String::new());
53        if !days_str.is_empty() || !tier_str.is_empty() {
54            let body_str = format!("<RestoreRequest>{}{}</RestoreRequest>", days_str, tier_str);
55            self.req.set_body(Full::new(Bytes::from(body_str)));
56        }
57        // Build the HTTP request
58        let response = self.req.send_to_oss()?.await?;
59        // Parse the response
60        let status_code = response.status();
61        match status_code {
62            code if code.is_success() => Ok(()),
63            _ => Err(normal_error(response).await),
64        }
65    }
66}