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

feat(services/obs): support user defined metadata #5405

Merged
merged 2 commits into from
Dec 11, 2024
Merged
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
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
Loading