rusty_s3/actions/
create_bucket.rs1use std::time::Duration;
2
3use jiff::Timestamp;
4use url::Url;
5
6use crate::actions::Method;
7use crate::actions::S3Action;
8use crate::signing::sign;
9use crate::{Bucket, Credentials, Map};
10
11#[derive(Debug, Clone)]
17pub struct CreateBucket<'a> {
18 bucket: &'a Bucket,
19 credentials: &'a Credentials,
20
21 query: Map<'a>,
22 headers: Map<'a>,
23}
24
25impl<'a> CreateBucket<'a> {
26 #[must_use]
27 pub const fn new(bucket: &'a Bucket, credentials: &'a Credentials) -> Self {
28 Self {
29 bucket,
30 credentials,
31
32 query: Map::new(),
33 headers: Map::new(),
34 }
35 }
36}
37
38impl<'a> S3Action<'a> for CreateBucket<'a> {
39 const METHOD: Method = Method::Put;
40
41 fn query_mut(&mut self) -> &mut Map<'a> {
42 &mut self.query
43 }
44
45 fn headers_mut(&mut self) -> &mut Map<'a> {
46 &mut self.headers
47 }
48
49 fn sign_with_time(&self, expires_in: Duration, time: &Timestamp) -> Url {
50 let url = self.bucket.base_url().clone();
51
52 sign(
53 time,
54 Self::METHOD,
55 url,
56 self.credentials.key(),
57 self.credentials.secret(),
58 self.credentials.token(),
59 self.bucket.region(),
60 expires_in.as_secs(),
61 self.query.iter(),
62 self.headers.iter(),
63 )
64 }
65}
66
67#[cfg(test)]
68mod tests {
69 use pretty_assertions::assert_eq;
70
71 use super::*;
72 use crate::{Bucket, Credentials, UrlStyle};
73
74 #[test]
75 fn aws_example() {
76 let date = Timestamp::from_second(1369353600).unwrap();
78 let expires_in = Duration::from_secs(86400);
79
80 let endpoint = "https://s3.amazonaws.com".parse().unwrap();
81 let bucket = Bucket::new(
82 endpoint,
83 UrlStyle::VirtualHost,
84 "examplebucket",
85 "us-east-1",
86 )
87 .unwrap();
88 let credentials = Credentials::new(
89 "AKIAIOSFODNN7EXAMPLE",
90 "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
91 );
92
93 let action = CreateBucket::new(&bucket, &credentials);
94
95 let url = action.sign_with_time(expires_in, &date);
96 let expected = "https://examplebucket.s3.amazonaws.com/?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIOSFODNN7EXAMPLE%2F20130524%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20130524T000000Z&X-Amz-Expires=86400&X-Amz-SignedHeaders=host&X-Amz-Signature=fb5c8ab11e9fd9d3c54ea0293e1df0820feef6c1f2de12e5fe00636e3f0cf9d2";
97
98 assert_eq!(expected, url.as_str());
99 }
100}