-
Notifications
You must be signed in to change notification settings - Fork 472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add crc32c checksums to S3 Service #4533
Changes from 5 commits
7a01f4d
f5819e0
30cf048
3678539
6ebfd55
da1c104
d6aef7c
de44c26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,8 @@ use std::sync::atomic; | |
use std::sync::atomic::AtomicBool; | ||
use std::time::Duration; | ||
|
||
use base64::prelude::BASE64_STANDARD; | ||
use base64::Engine; | ||
use bytes::Bytes; | ||
use http::header::HeaderName; | ||
use http::header::CACHE_CONTROL; | ||
|
@@ -88,6 +90,7 @@ pub struct S3Core { | |
pub credential_loaded: AtomicBool, | ||
pub client: HttpClient, | ||
pub batch_max_operations: usize, | ||
pub checksum_algorithm: Option<ChecksumAlgorithm>, | ||
} | ||
|
||
impl Debug for S3Core { | ||
|
@@ -246,6 +249,40 @@ impl S3Core { | |
|
||
req | ||
} | ||
|
||
pub fn insert_checksum_header( | ||
&self, | ||
mut req: http::request::Builder, | ||
body: &Buffer, | ||
) -> http::request::Builder { | ||
if let Some(checksum_algorithm) = self.checksum_algorithm.as_ref() { | ||
let checksum = match checksum_algorithm { | ||
ChecksumAlgorithm::Crc32c => { | ||
let mut crc = 0u32; | ||
body.clone() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This clone should be okay, since it does not do any allocation (?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, this clone is |
||
.for_each(|b| crc = crc32c::crc32c_append(crc, &b)); | ||
BASE64_STANDARD.encode(crc.to_be_bytes()) | ||
} | ||
}; | ||
req = req.header(checksum_algorithm.to_header_key(), checksum); | ||
} | ||
req | ||
} | ||
|
||
pub fn insert_checksum_type_header( | ||
&self, | ||
mut req: http::request::Builder, | ||
) -> http::request::Builder { | ||
if let Some(checksum_algorithm) = self.checksum_algorithm.as_ref() { | ||
req = req.header( | ||
"x-amz-checksum-algorithm", | ||
match checksum_algorithm { | ||
ChecksumAlgorithm::Crc32c => "CRC32C", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can implement |
||
}, | ||
); | ||
} | ||
req | ||
} | ||
} | ||
|
||
impl S3Core { | ||
|
@@ -408,6 +445,9 @@ impl S3Core { | |
// Set SSE headers. | ||
req = self.insert_sse_headers(req, true); | ||
|
||
// Set Checksum header. | ||
req = self.insert_checksum_header(req, &body); | ||
|
||
// Set body | ||
let req = req.body(body).map_err(new_request_build_error)?; | ||
|
||
|
@@ -573,6 +613,9 @@ impl S3Core { | |
// Set SSE headers. | ||
let req = self.insert_sse_headers(req, true); | ||
|
||
// Set SSE headers. | ||
let req = self.insert_checksum_type_header(req); | ||
|
||
let mut req = req.body(Buffer::new()).map_err(new_request_build_error)?; | ||
|
||
self.sign(&mut req).await?; | ||
|
@@ -605,6 +648,9 @@ impl S3Core { | |
// Set SSE headers. | ||
req = self.insert_sse_headers(req, true); | ||
|
||
// Set Checksum header. | ||
req = self.insert_checksum_header(req, &body); | ||
|
||
// Set body | ||
let req = req.body(body).map_err(new_request_build_error)?; | ||
|
||
|
@@ -821,6 +867,17 @@ pub struct OutputCommonPrefix { | |
pub prefix: String, | ||
} | ||
|
||
pub enum ChecksumAlgorithm { | ||
Crc32c, | ||
} | ||
impl ChecksumAlgorithm { | ||
pub fn to_header_key(&self) -> &str { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about using |
||
match self { | ||
Self::Crc32c => "x-amz-checksum-crc32c", | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use bytes::Buf; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This dependency doesn't seem to be grouped cleanly like others.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch, would like to send a PR to fix this? Also, I think this dep should be hidden under services-s3.