1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use super::{wrap_internal_error, ReqContext, S3Handler};
use crate::dto::{
CreateBucketConfiguration, CreateBucketError, CreateBucketOutput, CreateBucketRequest,
};
use crate::errors::{S3Error, S3ErrorCode, S3Result};
use crate::headers::{
LOCATION, X_AMZ_ACL, X_AMZ_BUCKET_OBJECT_LOCK_ENABLED, X_AMZ_GRANT_FULL_CONTROL,
X_AMZ_GRANT_READ, X_AMZ_GRANT_READ_ACP, X_AMZ_GRANT_WRITE, X_AMZ_GRANT_WRITE_ACP,
};
use crate::output::S3Output;
use crate::storage::S3Storage;
use crate::utils::body::deserialize_xml_body;
use crate::utils::ResponseExt;
use crate::{async_trait, Method, Response};
pub struct Handler;
#[async_trait]
impl S3Handler for Handler {
fn is_match(&self, ctx: &'_ ReqContext<'_>) -> bool {
bool_try!(ctx.req.method() == Method::PUT);
ctx.path.is_bucket()
}
async fn handle(
&self,
ctx: &mut ReqContext<'_>,
storage: &(dyn S3Storage + Send + Sync),
) -> S3Result<Response> {
let input = extract(ctx).await?;
let output = storage.create_bucket(input).await;
output.try_into_response()
}
}
async fn extract(ctx: &mut ReqContext<'_>) -> S3Result<CreateBucketRequest> {
let bucket = ctx.unwrap_bucket_path();
let config: Option<self::xml::CreateBucketConfiguration> =
deserialize_xml_body(ctx.take_body())
.await
.map_err(|err| invalid_request!("Invalid xml format", err))?;
let mut input: CreateBucketRequest = CreateBucketRequest {
bucket: bucket.into(),
create_bucket_configuration: config.map(Into::into),
..CreateBucketRequest::default()
};
let h = &ctx.headers;
h.assign_str(&*X_AMZ_ACL, &mut input.acl);
h.assign_str(&*X_AMZ_GRANT_FULL_CONTROL, &mut input.grant_full_control);
h.assign_str(&*X_AMZ_GRANT_READ, &mut input.grant_read);
h.assign_str(&*X_AMZ_GRANT_READ_ACP, &mut input.grant_read_acp);
h.assign_str(&*X_AMZ_GRANT_WRITE, &mut input.grant_write);
h.assign_str(&*X_AMZ_GRANT_WRITE_ACP, &mut input.grant_write_acp);
h.assign(
&*X_AMZ_BUCKET_OBJECT_LOCK_ENABLED,
&mut input.object_lock_enabled_for_bucket,
)
.map_err(|err| invalid_request!("Invalid header: x-amz-bucket-object-lock-enabled", err))?;
Ok(input)
}
impl S3Output for CreateBucketOutput {
fn try_into_response(self) -> S3Result<Response> {
wrap_internal_error(|res| {
res.set_optional_header(LOCATION, self.location)?;
Ok(())
})
}
}
impl From<CreateBucketError> for S3Error {
fn from(e: CreateBucketError) -> Self {
match e {
CreateBucketError::BucketAlreadyExists(msg) => {
Self::new(S3ErrorCode::BucketAlreadyExists, msg)
}
CreateBucketError::BucketAlreadyOwnedByYou(msg) => {
Self::new(S3ErrorCode::BucketAlreadyOwnedByYou, msg)
}
}
}
}
mod xml {
use serde::Deserialize;
#[derive(Debug, Deserialize)]
pub struct CreateBucketConfiguration {
#[serde(rename = "LocationConstraint")]
pub location_constraint: Option<String>,
}
impl From<CreateBucketConfiguration> for super::CreateBucketConfiguration {
fn from(config: CreateBucketConfiguration) -> Self {
Self {
location_constraint: config.location_constraint,
}
}
}
}