-
Notifications
You must be signed in to change notification settings - Fork 510
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
base: main
Are you sure you want to change the base?
Changes from 6 commits
36ea40a
ed0dc6f
cff50c9
255e7ce
4206211
38d0f97
ed49fce
2842b18
6a8429a
db22c30
44dd9b8
38edeac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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) { | ||
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) | ||
|
@@ -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?; | ||
|
@@ -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> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this function might be slightly overengineered. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok