Skip to content

Commit

Permalink
feat(services/obs): support user defined metadata (#5405)
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank-III authored Dec 11, 2024
1 parent 1ca1b63 commit 566a789
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
27 changes: 25 additions & 2 deletions core/src/services/obs/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.

use std::collections::HashMap;
use std::fmt::Debug;
use std::fmt::Formatter;
use std::sync::Arc;
Expand All @@ -27,7 +28,7 @@ use reqsign::HuaweicloudObsConfig;
use reqsign::HuaweicloudObsCredentialLoader;
use reqsign::HuaweicloudObsSigner;

use super::core::ObsCore;
use super::core::{constants, ObsCore};
use super::delete::ObsDeleter;
use super::error::parse_error;
use super::lister::ObsLister;
Expand Down Expand Up @@ -284,6 +285,7 @@ impl Access for ObsBackend {
} else {
Some(usize::MAX)
},
write_with_user_metadata: true,

delete: true,
copy: true,
Expand All @@ -306,12 +308,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 566a789

Please sign in to comment.