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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use std::iter;
use std::time::Duration;
use serde::Deserialize;
use time::OffsetDateTime;
use url::Url;
use crate::actions::Method;
use crate::actions::S3Action;
use crate::signing::sign;
use crate::{Bucket, Credentials};
#[derive(Debug, Clone)]
pub struct CreateMultipartUpload<'a> {
bucket: &'a Bucket,
credentials: Option<&'a Credentials>,
object: &'a str,
}
#[derive(Debug, Clone)]
pub struct CreateMultipartUploadResponse(InnerCreateMultipartUploadResponse);
#[derive(Debug, Clone, Deserialize)]
struct InnerCreateMultipartUploadResponse {
#[serde(rename = "UploadId")]
upload_id: String,
}
impl<'a> CreateMultipartUpload<'a> {
#[inline]
pub fn new(bucket: &'a Bucket, credentials: Option<&'a Credentials>, object: &'a str) -> Self {
Self {
bucket,
credentials,
object,
}
}
pub fn parse_response(s: &str) -> Result<CreateMultipartUploadResponse, quick_xml::DeError> {
let parsed = quick_xml::de::from_str(s)?;
Ok(CreateMultipartUploadResponse(parsed))
}
fn sign_with_time(&self, expires_in: Duration, time: &OffsetDateTime) -> Url {
let url = self.bucket.object_url(self.object).unwrap();
let query = iter::once(("uploads", "1"));
match self.credentials {
Some(credentials) => sign(
time,
Method::Post,
url,
credentials.key(),
credentials.secret(),
credentials.token(),
self.bucket.region(),
expires_in.as_secs(),
query,
iter::empty(),
),
None => crate::signing::util::add_query_params(url, query),
}
}
}
impl CreateMultipartUploadResponse {
pub fn upload_id(&self) -> &str {
&self.0.upload_id
}
}
impl<'a> S3Action for CreateMultipartUpload<'a> {
const METHOD: Method = Method::Post;
fn sign(&self, expires_in: Duration) -> Url {
let now = OffsetDateTime::now_utc();
self.sign_with_time(expires_in, &now)
}
}
#[cfg(test)]
mod tests {
use time::PrimitiveDateTime;
use pretty_assertions::assert_eq;
use super::*;
use crate::{Bucket, Credentials};
#[test]
fn aws_example() {
let date = PrimitiveDateTime::parse(
"Fri, 24 May 2013 00:00:00 GMT",
"%a, %d %b %Y %-H:%M:%S GMT",
)
.unwrap()
.assume_utc();
let expires_in = Duration::from_secs(86400);
let endpoint = "https://s3.amazonaws.com".parse().unwrap();
let bucket =
Bucket::new(endpoint, false, "examplebucket".into(), "us-east-1".into()).unwrap();
let credentials = Credentials::new(
"AKIAIOSFODNN7EXAMPLE".into(),
"wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY".into(),
);
let action = CreateMultipartUpload::new(&bucket, Some(&credentials), "test.txt");
let url = action.sign_with_time(expires_in, &date);
let expected = "https://examplebucket.s3.amazonaws.com/test.txt?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&uploads=1&X-Amz-Signature=a6289f9e5ff2a914c6e324403bcd00b1d258c568487faa50d317ef0910c25c0a";
assert_eq!(expected, url.as_str());
}
#[test]
fn anonymous_custom_query() {
let expires_in = Duration::from_secs(86400);
let endpoint = "https://s3.amazonaws.com".parse().unwrap();
let bucket =
Bucket::new(endpoint, false, "examplebucket".into(), "us-east-1".into()).unwrap();
let action = CreateMultipartUpload::new(&bucket, None, "test.txt");
let url = action.sign(expires_in);
let expected = "https://examplebucket.s3.amazonaws.com/test.txt?uploads=1";
assert_eq!(expected, url.as_str());
}
}