s3m/s3/actions/
mod.rs

1//! Actions
2//! <https://docs.aws.amazon.com/AmazonS3/latest/API/API_Operations.html>
3
4use crate::s3::{S3, responses::ErrorResponse, signature::Signature};
5use anyhow::{Result, anyhow};
6use quick_xml::de::from_str;
7use reqwest::{Method, Response};
8use std::{collections::BTreeMap, fmt::Write};
9use url::Url;
10
11// <https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListBuckets.html>
12mod listbuckets;
13pub use self::listbuckets::ListBuckets;
14
15// <https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html>
16mod listobjectsv2;
17pub use self::listobjectsv2::ListObjectsV2;
18
19// <https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectVersions.html>
20mod listobjectversions;
21pub use self::listobjectversions::ListObjectVersions;
22
23// <https://docs.aws.amazon.com/AmazonS3/latest/API/API_HeadObject.html>
24mod headobject;
25pub use self::headobject::HeadObject;
26
27// <https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObject.html>
28mod getobject;
29pub use self::getobject::GetObject;
30
31// <https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObjectAcl.html>
32mod getobjectacl;
33pub use self::getobjectacl::GetObjectAcl;
34
35// <https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObjectAttributes.html>
36mod getobjectattributes;
37pub use self::getobjectattributes::GetObjectAttributes;
38
39// <https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html>
40mod putobject;
41pub use self::putobject::PutObject;
42
43// <https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObjectAcl.html>
44mod putobjectacl;
45pub use self::putobjectacl::PutObjectAcl;
46
47// <https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateMultipartUpload.html>
48mod createmultipartupload;
49pub use self::createmultipartupload::CreateMultipartUpload;
50
51// <https://docs.aws.amazon.com/AmazonS3/latest/API/API_UploadPart.html>
52mod uploadpart;
53pub use self::uploadpart::UploadPart;
54
55mod streampart;
56pub use self::streampart::StreamPart;
57
58// <https://docs.aws.amazon.com/AmazonS3/latest/API/API_CompleteMultipartUpload.html>
59mod completemultipartupload;
60pub use self::completemultipartupload::{CompleteMultipartUpload, Part};
61
62// <https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListMultipartUploads.html>
63mod listmultipartuploads;
64pub use self::listmultipartuploads::ListMultipartUploads;
65
66// <https://docs.aws.amazon.com/AmazonS3/latest/API/API_AbortMultipartUpload.html>
67mod abortmultipartupload;
68pub use self::abortmultipartupload::AbortMultipartUpload;
69
70// <https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteObject.html>
71mod deleteobject;
72pub use self::deleteobject::DeleteObject;
73
74// <https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateBucket.html>
75mod createbucket;
76pub use self::createbucket::CreateBucket;
77
78// https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteBucket.html
79mod deletebucket;
80pub use self::deletebucket::DeleteBucket;
81
82pub trait Action {
83    // headers to send in the request
84    fn headers(&self) -> Option<BTreeMap<&str, &str>>;
85
86    // HTTP method to use
87    /// # Errors
88    /// Will return an error if the HTTP method is not supported
89    fn http_method(&self) -> Result<Method>;
90
91    // URL query pairs
92    fn query_pairs(&self) -> Option<BTreeMap<&str, &str>>;
93
94    // URL path
95    fn path(&self) -> Option<Vec<&str>>;
96
97    /// # Errors
98    ///
99    /// Will return `Err` if the signature can not be created
100    fn sign(
101        &self,
102        s3: &S3,
103        hash_payload: &[u8],
104        md5: Option<&[u8]>,
105        content_length: Option<usize>,
106    ) -> Result<(Url, BTreeMap<String, String>)> {
107        let mut url = s3.endpoint()?;
108
109        // mainly for PUT when uploading an object
110        if let Some(path) = self.path() {
111            for p in path {
112                url.path_segments_mut()
113                    .map_err(|e| anyhow!("cannot be base: {:#?}", e))?
114                    .push(p);
115            }
116        }
117
118        // GET - query pairs
119        if let Some(pairs) = &self.query_pairs() {
120            for (k, v) in pairs {
121                url.query_pairs_mut().append_pair(k, v);
122            }
123        }
124
125        // --no-sign-request
126        if s3.no_sign_request {
127            return Ok((url, BTreeMap::new()));
128        }
129
130        log::info!("URL to sign: {url}");
131
132        let mut signature = Signature::new(s3, "s3", self.http_method()?)?;
133
134        let headers = signature.sign(&url, hash_payload, md5, content_length, self.headers());
135
136        Ok((url, headers?))
137    }
138}
139
140/// # Errors
141/// Will return an error if can't add the headers to the request
142pub async fn response_error(response: Response) -> Result<String> {
143    let mut error: BTreeMap<&str, String> = BTreeMap::new();
144
145    error.insert("HTTP Status Code", response.status().to_string());
146
147    if let Some(x_amz_id_2) = response.headers().get("x-amz-id-2") {
148        error.insert("x-amz-id-2", x_amz_id_2.to_str()?.to_string());
149    }
150
151    if let Some(rid) = response.headers().get("x-amz-request-id") {
152        error.insert("Request ID", rid.to_str()?.to_string());
153    }
154
155    // Try to read the response body, but don't fail if we can't
156    match response.text().await {
157        Ok(body) => {
158            if let Ok(e) = from_str::<ErrorResponse>(&body) {
159                error.insert("Code", e.code);
160                error.insert("Message", e.message);
161            } else if !body.is_empty() {
162                error.insert("Response", body);
163            }
164        }
165        Err(e) => {
166            // If we can't read the body, still report what we know
167            log::warn!("Failed to read error response body: {}", e);
168            error.insert(
169                "Note",
170                "Connection closed before response body could be read".to_string(),
171            );
172        }
173    }
174
175    Ok(error.iter().fold(String::new(), |mut output, (k, v)| {
176        let _ = writeln!(output, "{k}: {v}");
177        output
178    }))
179}