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/cos): Added user metadata support for cos service #5510

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
7 changes: 6 additions & 1 deletion core/src/services/cos/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ impl Access for CosBackend {
stat_has_content_md5: true,
stat_has_last_modified: true,
stat_has_content_disposition: true,
stat_has_user_metadata: true,

read: true,

Expand Down Expand Up @@ -284,6 +285,7 @@ impl Access for CosBackend {
} else {
Some(usize::MAX)
},
write_with_user_metadata: true,

delete: true,
copy: true,
Expand Down Expand Up @@ -311,7 +313,10 @@ impl Access for CosBackend {
let status = resp.status();

match status {
StatusCode::OK => parse_into_metadata(path, resp.headers()).map(RpStat::new),
StatusCode::OK => {
let meta = self.core.parse_metadata(path, resp.headers())?;
Ok(RpStat::new(meta))
}
_ => Err(parse_error(resp)),
}
}
Expand Down
55 changes: 55 additions & 0 deletions core/src/services/cos/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use http::header::CONTENT_LENGTH;
use http::header::CONTENT_TYPE;
use http::header::IF_MATCH;
use http::header::IF_NONE_MATCH;
use http::HeaderMap;
use http::Request;
use http::Response;
use reqsign::TencentCosCredential;
Expand Down Expand Up @@ -184,6 +185,20 @@ impl CosCore {
req = req.header("x-cos-forbid-overwrite", "true")
}

// Set user metadata headers.
if let Some(user_metadata) = args.user_metadata() {
for (key, value) in user_metadata {
// before insert user defined metadata header, add prefix to the header name
if !self.check_user_metadata_key(key) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I'm not sure if it's a good idea to check user's metadata key. Let's skip it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

return Err(Error::new(
ErrorKind::Unsupported,
"the format of the user metadata key is invalid, please refer the document",
));
}
req = req.header(format!("x-cos-meta-{key}"), value)
}
}

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

Ok(req)
Expand Down Expand Up @@ -345,6 +360,20 @@ impl CosCore {
req = req.header(CACHE_CONTROL, cache_control)
}

// Set user metadata headers.
if let Some(user_metadata) = args.user_metadata() {
for (key, value) in user_metadata {
// before insert user defined metadata header, add prefix to the header name
if !self.check_user_metadata_key(key) {
return Err(Error::new(
ErrorKind::Unsupported,
"the format of the user metadata key is invalid, please refer the document",
));
}
req = req.header(format!("x-cos-meta-{key}"), value)
}
}

let mut req = req.body(Buffer::new()).map_err(new_request_build_error)?;

self.sign(&mut req).await?;
Expand Down Expand Up @@ -434,6 +463,32 @@ impl CosCore {
self.sign(&mut req).await?;
self.send(req).await
}

// According to https://www.tencentcloud.com/document/product/436/7746
// there are some limits in user defined metadata key
fn check_user_metadata_key(&self, key: &str) -> bool {
key.chars().all(|c| c != '_')
}

/// parse_metadata will parse http headers(including standards http headers
/// and user defined metadata header) into Metadata.
///
/// # Arguments
///
/// * `user_metadata_prefix` is the prefix of user defined metadata key
///
/// # Notes
///
/// before return the user defined metadata, we'll strip the user_metadata_prefix from the key
pub fn parse_metadata(&self, path: &str, headers: &HeaderMap) -> Result<Metadata> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this function might be slightly overengineered.

Copy link
Contributor Author

@geetanshjuneja geetanshjuneja Jan 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by overengineered? What do I need to change in this fn?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I would like to remove this function and expand it instead.

let mut m = parse_into_metadata(path, headers)?;
let user_meta = parse_prefixed_headers(headers, "x-cos-meta-");
if !user_meta.is_empty() {
m.with_user_metadata(user_meta);
}

Ok(m)
}
}

/// Result of CreateMultipartUpload
Expand Down
Loading