Skip to content

Commit

Permalink
Providing more detailed error when the put API fails (#257)
Browse files Browse the repository at this point in the history
* s3 0.29.0 (#253)

* cargo: Move `tokio-stream` create behind the `with-tokio` feature (#248)

`tokio-stream` has a dependency on `tokio` and is only used within the
`request` module, which depends on the `with-tokio` feature.  As such,
in order to completely prevent `tokio` from appearing in the crate
graph, this crate must be marked optional and enabled when `with-tokio`
is requested, omitted otherwise.

Also includes two drive-by typo-fixes in the features section of the
readme.

* Fixup #248

* Migrate from `chrono` to `time` to solve RUSTSEC-2020-0159 (#250)

`chrono` still hasn't found a solution to [RUSTSEC-2020-0159] whereas
`time` already solved its vulnerability to [RUSTSEC-2020-0071] by hiding
the affected functionality behind a cfg flag (not to be confused with a
`feature`, such `cfg`s can only be enabled through `RUSTFLAGS`).  At the
same time `chrono` is a superset of `time` even though this crate hardly
uses any functionality of it: only UTC time is needed which does not
suffer from aforementioned local time vulnerabilities.

[RUSTSEC-2020-0071]: https://rustsec.org/advisories/RUSTSEC-2020-0071
[RUSTSEC-2020-0159]: https://rustsec.org/advisories/RUSTSEC-2020-0159

Co-authored-by: Drazen Urch <drazen@urch.eu>

* Update Makefile

* Closes #245

* Bump aws-region

* s3 0.29.0

Co-authored-by: Marijn Suijten <marijns95@gmail.com>

* Provide better error information

Co-authored-by: Drazen Urch <drazen@urch.eu>
Co-authored-by: Marijn Suijten <marijns95@gmail.com>
  • Loading branch information
3 people committed Mar 4, 2022
1 parent 1259d34 commit d7f3ee7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
12 changes: 11 additions & 1 deletion s3/src/bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ use anyhow::anyhow;
use anyhow::Result;
use http::header::HeaderName;
use http::HeaderMap;
use crate::utils::error_from_response_data;

pub const CHUNK_SIZE: usize = 8_388_608; // 8 Mebibytes, min is 5 (5_242_880);

Expand Down Expand Up @@ -755,13 +756,19 @@ impl Bucket {
// Otherwise perform a multi-part upload.
let first_chunk = crate::utils::read_chunk(reader).await?;
if first_chunk.len() < CHUNK_SIZE {
let (_, code) = self.put_object(s3_path, first_chunk.as_slice()).await?;
let (data, code) = self.put_object(s3_path, first_chunk.as_slice()).await?;
if code >= 300 {
return Err(error_from_response_data(data, code))
}
return Ok(code);
}

let command = Command::InitiateMultipartUpload;
let request = RequestImpl::new(self, s3_path, command);
let (data, code) = request.response_data(false).await?;
if code >= 300 {
return Err(error_from_response_data(data, code))
}

let msg: InitiateMultipartUploadResponse =
serde_xml::from_str(std::str::from_utf8(data.as_slice())?)?;
Expand Down Expand Up @@ -820,6 +827,9 @@ impl Bucket {
let command = Command::InitiateMultipartUpload;
let request = RequestImpl::new(self, s3_path, command);
let (data, code) = request.response_data(false)?;
if code >= 300 {
return Err(error_from_response_data(data, code))
}
let msg: InitiateMultipartUploadResponse =
serde_xml::from_str(std::str::from_utf8(data.as_slice())?)?;

Expand Down
19 changes: 19 additions & 0 deletions s3/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,25 @@ impl From<&http::HeaderMap> for HeadObjectResult {
}
}

pub(crate) fn error_from_response_data(data: Vec<u8>, code: u16) -> anyhow::Error {
let utf8_content = String::from_utf8(data);
let err = if let Ok(utf8_content) = utf8_content {
format!(
"Invalid return code: got HTTP {} with content '{}'",
code, utf8_content
)
} else {
format!(
"Invalid return code: got HTTP {} with invalid UTF8 content",
code
)
};
anyhow::Error::new(std::io::Error::new(
std::io::ErrorKind::InvalidData,
err,
))
}

#[cfg(test)]
mod test {
use crate::utils::etag_for_path;
Expand Down

0 comments on commit d7f3ee7

Please sign in to comment.