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(core/services-gcs): support user defined metadata #5276

Merged
merged 14 commits into from
Nov 6, 2024
22 changes: 21 additions & 1 deletion core/src/services/gcs/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 Down Expand Up @@ -363,6 +364,7 @@ impl Access for GcsBackend {
write_can_empty: true,
write_can_multi: true,
write_with_content_type: true,
write_with_user_metadata: true,
// The min multipart size of Gcs is 5 MiB.
//
// ref: <https://cloud.google.com/storage/docs/xml-api/put-object-multipart>
Expand Down Expand Up @@ -424,6 +426,10 @@ impl Access for GcsBackend {

m.set_last_modified(parse_datetime_from_rfc3339(&meta.updated)?);

if let Some(user_metadata) = meta.metadata {
m.with_user_metadata(user_metadata);
jorgehermo9 marked this conversation as resolved.
Show resolved Hide resolved
}

Ok(RpStat::new(m))
}

Expand Down Expand Up @@ -593,6 +599,10 @@ struct GetObjectJsonResponse {
///
/// For example: `"contentType": "image/png",`
content_type: String,
/// Custom metadata of this object.
///
/// For example: `"metadata" : { "my-key": "my-value" }`
metadata: Option<HashMap<String, String>>,
}

#[cfg(test)]
Expand All @@ -618,7 +628,10 @@ mod tests {
"etag": "CKWasoTgyPkCEAE=",
"timeCreated": "2022-08-15T11:33:34.866Z",
"updated": "2022-08-15T11:33:34.866Z",
"timeStorageClassUpdated": "2022-08-15T11:33:34.866Z"
"timeStorageClassUpdated": "2022-08-15T11:33:34.866Z",
"metadata" : {
"location" : "everywhere"
}
}"#;

let meta: GetObjectJsonResponse =
Expand All @@ -629,5 +642,12 @@ mod tests {
assert_eq!(meta.md5_hash, "fHcEH1vPwA6eTPqxuasXcg==");
assert_eq!(meta.etag, "CKWasoTgyPkCEAE=");
assert_eq!(meta.content_type, "image/png");
assert_eq!(
meta.metadata,
Some(HashMap::from_iter([(
"location".to_string(),
"everywhere".to_string()
)]))
);
}
}
25 changes: 23 additions & 2 deletions core/src/services/gcs/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ use std::time::Duration;
use backon::ExponentialBuilder;
use backon::Retryable;
use bytes::Bytes;
use constants::X_GOOG_ACL;
use constants::X_GOOG_STORAGE_CLASS;
use http::header::CONTENT_LENGTH;
use http::header::CONTENT_TYPE;
use http::header::HOST;
Expand All @@ -44,6 +46,13 @@ use serde_json::json;
use super::uri::percent_encode_path;
use crate::raw::*;
use crate::*;
use constants::*;

pub mod constants {
pub const X_GOOG_ACL: &str = "x-goog-acl";
pub const X_GOOG_STORAGE_CLASS: &str = "x-goog-storage-class";
pub const X_GOOG_META_PREFIX: &str = "x-goog-meta-";
}

pub struct GcsCore {
pub endpoint: String,
Expand Down Expand Up @@ -267,6 +276,12 @@ impl GcsCore {

let mut req = Request::post(&url);

if let Some(user_metadata) = op.user_metadata() {
for (key, value) in user_metadata {
req = req.header(format!("{X_GOOG_META_PREFIX}{key}"), value)
Xuanwo marked this conversation as resolved.
Show resolved Hide resolved
}
}

req = req.header(CONTENT_LENGTH, size.unwrap_or_default());

if metadata.is_empty() {
Expand Down Expand Up @@ -318,16 +333,22 @@ impl GcsCore {

let mut req = Request::put(&url);

if let Some(user_metadata) = args.user_metadata() {
jorgehermo9 marked this conversation as resolved.
Show resolved Hide resolved
for (key, value) in user_metadata {
jorgehermo9 marked this conversation as resolved.
Show resolved Hide resolved
req = req.header(format!("{X_GOOG_META_PREFIX}{key}"), value)
}
}

if let Some(content_type) = args.content_type() {
req = req.header(CONTENT_TYPE, content_type);
}

if let Some(acl) = &self.predefined_acl {
req = req.header("x-goog-acl", acl);
req = req.header(X_GOOG_ACL, acl);
jorgehermo9 marked this conversation as resolved.
Show resolved Hide resolved
}

if let Some(storage_class) = &self.default_storage_class {
req = req.header("x-goog-storage-class", storage_class);
req = req.header(X_GOOG_STORAGE_CLASS, storage_class);
}

let req = req.body(body).map_err(new_request_build_error)?;
Expand Down
Loading