aliyun_oss_rs/bucket/
put_bucket_policy.rs

1use crate::{
2    Error,
3    error::normal_error,
4    request::{Oss, OssRequest},
5};
6use bytes::Bytes;
7use http::Method;
8use http_body_util::Full;
9
10/// Configure an access policy for the bucket.
11///
12/// See the [Alibaba Cloud documentation](https://help.aliyun.com/zh/oss/developer-reference/putbucketpolicy) for details.
13pub struct PutBucketPolicy {
14    req: OssRequest,
15    policy: Option<String>,
16}
17
18impl PutBucketPolicy {
19    pub(super) fn new(oss: Oss) -> Self {
20        let mut req = OssRequest::new(oss, Method::PUT);
21        req.insert_query("policy", "");
22        PutBucketPolicy { req, policy: None }
23    }
24
25    /// Set the policy document in JSON format.
26    pub fn set_policy(mut self, policy: impl ToString) -> Self {
27        self.policy = Some(policy.to_string());
28        self
29    }
30
31    /// Send the request.
32    pub async fn send(mut self) -> Result<(), Error> {
33        let body = self.policy.ok_or(Error::MissingRequestBody)?;
34        self.req.set_body(Full::new(Bytes::from(body)));
35        let response = self.req.send_to_oss()?.await?;
36        match response.status() {
37            code if code.is_success() => Ok(()),
38            _ => Err(normal_error(response).await),
39        }
40    }
41}