Skip to content

Commit

Permalink
feat(services/obs): support user defined metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank-III committed Dec 10, 2024
1 parent 1ca1b63 commit 2b02ce5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
24 changes: 23 additions & 1 deletion core/src/services/obs/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ impl Access for ObsBackend {
} else {
Some(usize::MAX)
},
write_with_user_metadata: true,

delete: true,
copy: true,
Expand All @@ -306,12 +307,33 @@ impl Access for ObsBackend {

async fn stat(&self, path: &str, args: OpStat) -> Result<RpStat> {
let resp = self.core.obs_head_object(path, &args).await?;
let headers = resp.headers();

let status = resp.status();

// The response is very similar to azblob.
match status {
StatusCode::OK => parse_into_metadata(path, resp.headers()).map(RpStat::new),
StatusCode::OK => {
let mut meta = parse_into_metadata(path, headers);
let user_meta = headers
.iter()
.filter_map(|(name, _)| {
name.as_str()
.strip_prefix(constants::X_OBS_META_PREFIX)
.and_then(|stripped_key| {
parse_header_to_str(headers, name)
.unwrap_or(None)
.map(|val| (stripped_key.to_string(), val.to_string()))
})
})
.collect::<HashMap<_, _>>();

if !user_meta.is_empty() {
meta.with_user_metadata(user_meta);
}

Ok(RpStat::new(meta))
}
StatusCode::NOT_FOUND if path.ends_with('/') => {
Ok(RpStat::new(Metadata::new(EntryMode::DIR)))
}
Expand Down
11 changes: 11 additions & 0 deletions core/src/services/obs/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ use serde::Serialize;
use crate::raw::*;
use crate::*;

pub mod constants {
pub const X_OBS_META_PREFIX: &str = "x-obs-meta-";
}

pub struct ObsCore {
pub bucket: String,
pub root: String,
Expand Down Expand Up @@ -167,6 +171,13 @@ impl ObsCore {
req = req.header(CONTENT_TYPE, mime)
}

// Set user metadata headers.
if let Some(user_metadata) = args.user_metadata() {
for (key, value) in user_metadata {
req = req.header(format!("{}{}", constants::X_OBS_META_PREFIX, key), value)
}
}

let req = req.body(body).map_err(new_request_build_error)?;

Ok(req)
Expand Down

0 comments on commit 2b02ce5

Please sign in to comment.