Skip to content
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

[object_store] Support MD5 checksum in attributes #6915

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions object_store/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use std::borrow::Cow;
use std::collections::HashMap;
use std::ops::Deref;
use crate::checksum::ChecksumAlgorithm;

/// Additional object attribute types
#[non_exhaustive]
Expand Down Expand Up @@ -45,6 +46,8 @@ pub enum Attribute {
///
/// See [Cache-Control](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control)
CacheControl,
/// Provides a checksum used to verify object data integrity
Checksum(ChecksumAlgorithm),
/// Specifies a user-defined metadata field for the object
///
/// The String is a user-defined key
Expand Down Expand Up @@ -188,21 +191,23 @@ impl<'a> Iterator for AttributesIter<'a> {

#[cfg(test)]
mod tests {
use crate::checksum::ChecksumAlgorithm;
use super::*;

#[test]
fn test_attributes_basic() {
let mut attributes = Attributes::from_iter([
(Attribute::CacheControl, "control"),
(Attribute::Checksum(ChecksumAlgorithm::MD5), "checksumValue"),
(Attribute::ContentDisposition, "inline"),
(Attribute::ContentEncoding, "gzip"),
(Attribute::ContentLanguage, "en-US"),
(Attribute::ContentType, "test"),
(Attribute::CacheControl, "control"),
(Attribute::Metadata("key1".into()), "value1"),
]);

assert!(!attributes.is_empty());
assert_eq!(attributes.len(), 6);
assert_eq!(attributes.len(), 7);

assert_eq!(
attributes.get(&Attribute::ContentType),
Expand All @@ -215,19 +220,23 @@ mod tests {
attributes.insert(Attribute::CacheControl, "v1".into()),
Some(metav)
);
assert_eq!(attributes.len(), 6);
assert_eq!(attributes.len(), 7);

assert_eq!(
attributes.remove(&Attribute::CacheControl).unwrap(),
"v1".into()
);
assert_eq!(attributes.len(), 5);
assert_eq!(attributes.len(), 6);

let metav: AttributeValue = "v2".into();
attributes.insert(Attribute::CacheControl, metav.clone());
assert_eq!(attributes.get(&Attribute::CacheControl), Some(&metav));
assert_eq!(attributes.len(), 6);
assert_eq!(attributes.len(), 7);

assert_eq!(
attributes.get(&Attribute::Checksum(ChecksumAlgorithm::MD5)),
Some(&"checksumValue".into())
);
assert_eq!(
attributes.get(&Attribute::ContentDisposition),
Some(&"inline".into())
Expand Down
12 changes: 8 additions & 4 deletions object_store/src/aws/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ use crate::client::s3::{
use crate::client::GetOptionsExt;
use crate::multipart::PartId;
use crate::path::DELIMITER;
use crate::{
Attribute, Attributes, ClientOptions, GetOptions, ListResult, MultipartId, Path,
PutMultipartOpts, PutPayload, PutResult, Result, RetryConfig, TagSet,
};
use crate::{Attribute, Attributes, ClientOptions, GetOptions, ListResult, MultipartId, Path, PutMultipartOpts, PutPayload, PutResult, Result, RetryConfig, TagSet};
use async_trait::async_trait;
use base64::prelude::BASE64_STANDARD;
use base64::Engine;
Expand All @@ -58,9 +55,11 @@ use ring::digest::Context;
use serde::{Deserialize, Serialize};
use snafu::{ResultExt, Snafu};
use std::sync::Arc;
use crate::checksum::ChecksumAlgorithm;

const VERSION_HEADER: &str = "x-amz-version-id";
const SHA256_CHECKSUM: &str = "x-amz-checksum-sha256";
const CHECKSUM_MD5_HEADER: &str = "Content-MD5";
const USER_DEFINED_METADATA_HEADER_PREFIX: &str = "x-amz-meta-";
const ALGORITHM: &str = "x-amz-checksum-algorithm";

Expand Down Expand Up @@ -360,6 +359,11 @@ impl<'a> Request<'a> {
for (k, v) in &attributes {
builder = match k {
Attribute::CacheControl => builder.header(CACHE_CONTROL, v.as_ref()),
Attribute::Checksum(algorithm) => {
match algorithm {
ChecksumAlgorithm::MD5 => builder.header(CHECKSUM_MD5_HEADER, v.as_ref()),
}
}
Attribute::ContentDisposition => builder.header(CONTENT_DISPOSITION, v.as_ref()),
Attribute::ContentEncoding => builder.header(CONTENT_ENCODING, v.as_ref()),
Attribute::ContentLanguage => builder.header(CONTENT_LANGUAGE, v.as_ref()),
Expand Down
5 changes: 5 additions & 0 deletions object_store/src/azure/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use url::Url;
use crate::checksum::ChecksumAlgorithm;

const VERSION_HEADER: &str = "x-ms-version-id";
const USER_DEFINED_METADATA_HEADER_PREFIX: &str = "x-ms-meta-";
Expand All @@ -58,6 +59,7 @@ static MS_CONTENT_ENCODING: HeaderName = HeaderName::from_static("x-ms-blob-cont
static MS_CONTENT_LANGUAGE: HeaderName = HeaderName::from_static("x-ms-blob-content-language");

static TAGS_HEADER: HeaderName = HeaderName::from_static("x-ms-tags");
static CHECKSUM_MD5_HEADER: HeaderName = HeaderName::from_static("Content-MD5");

/// A specialized `Error` for object store-related errors
#[derive(Debug, Snafu)]
Expand Down Expand Up @@ -228,6 +230,9 @@ impl<'a> PutRequest<'a> {
for (k, v) in &attributes {
builder = match k {
Attribute::CacheControl => builder.header(&MS_CACHE_CONTROL, v.as_ref()),
Attribute::Checksum(algorithm) => match algorithm {
ChecksumAlgorithm::MD5 => builder.header(&CHECKSUM_MD5_HEADER, v.as_ref()),
}
Attribute::ContentDisposition => {
builder.header(&MS_CONTENT_DISPOSITION, v.as_ref())
}
Expand Down
7 changes: 7 additions & 0 deletions object_store/src/checksum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[allow(non_camel_case_types)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
/// Enum representing checksum algorithms that may be used to verify object integrity.
pub enum ChecksumAlgorithm {
/// MD5 algorithm.
MD5,
}
5 changes: 5 additions & 0 deletions object_store/src/gcp/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ use reqwest::{Client, Method, RequestBuilder, Response, StatusCode};
use serde::{Deserialize, Serialize};
use snafu::{OptionExt, ResultExt, Snafu};
use std::sync::Arc;
use crate::checksum::ChecksumAlgorithm;

const VERSION_HEADER: &str = "x-goog-generation";
const DEFAULT_CONTENT_TYPE: &str = "application/octet-stream";
const USER_DEFINED_METADATA_HEADER_PREFIX: &str = "x-goog-meta-";
const CHECKSUM_MD5_HEADER: &str = "Content-MD5";

static VERSION_MATCH: HeaderName = HeaderName::from_static("x-goog-if-generation-match");

Expand Down Expand Up @@ -196,6 +198,9 @@ impl<'a> Request<'a> {
for (k, v) in &attributes {
builder = match k {
Attribute::CacheControl => builder.header(CACHE_CONTROL, v.as_ref()),
Attribute::Checksum(algorithm) => match algorithm{
ChecksumAlgorithm::MD5 => builder.header(CHECKSUM_MD5_HEADER, v.as_ref()),
}
Attribute::ContentDisposition => builder.header(CONTENT_DISPOSITION, v.as_ref()),
Attribute::ContentEncoding => builder.header(CONTENT_ENCODING, v.as_ref()),
Attribute::ContentLanguage => builder.header(CONTENT_LANGUAGE, v.as_ref()),
Expand Down
2 changes: 2 additions & 0 deletions object_store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,11 +545,13 @@ mod upload;
mod util;

mod attributes;
mod checksum;

#[cfg(any(feature = "integration", test))]
pub mod integration;

pub use attributes::*;
pub use checksum::*;

pub use parse::{parse_url, parse_url_opts, ObjectStoreScheme};
pub use payload::*;
Expand Down
Loading