QuakeWang commented on code in PR #346: URL: https://github.com/apache/paimon-rust/pull/346#discussion_r3321833244
########## crates/paimon/src/io/storage_gcs.rs: ########## @@ -0,0 +1,242 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::collections::HashMap; + +use opendal::services::GcsConfig; +use opendal::{Configurator, Operator}; +use url::Url; + +use crate::error::Error; +use crate::Result; + +const GCS_ENDPOINT: &str = "gcs.endpoint"; +const GCS_CREDENTIAL: &str = "gcs.credential"; +const GCS_CREDENTIAL_PATH: &str = "gcs.credential-path"; +const GCS_SERVICE_ACCOUNT: &str = "gcs.service-account"; +const GCS_ALLOW_ANONYMOUS: &str = "gcs.allow-anonymous"; + +const CONFIG_PREFIXES: &[&str] = &["fs.gs.", "fs.gcs.", "gs.", "gcs."]; +const MIRRORED_KEYS: &[(&str, &str)] = &[ + ("gcs.credential-path", "gcs.google_application_credentials"), + ("gcs.credential-path", "gcs.google-application-credentials"), + ("gcs.credential-path", "gcs.application-credentials"), + ("gcs.credential", "gcs.google_service_account_key"), + ("gcs.credential", "gcs.google-service-account-key"), + ("gcs.credential", "gcs.service-account-key"), + ("gcs.credential", "gcs.service_account_key"), + ("gcs.service-account", "gcs.google_service_account"), + ("gcs.service-account", "gcs.google-service-account"), + ("gcs.service-account", "gcs.service_account"), + ("gcs.predefined-acl", "gcs.predefined_acl"), + ("gcs.default-storage-class", "gcs.default_storage_class"), + ("gcs.allow-anonymous", "gcs.google_skip_signature"), + ("gcs.allow-anonymous", "gcs.google-skip-signature"), + ("gcs.allow_anonymous", "gcs.google_skip_signature"), + ("gcs.allow-anonymous", "gcs.allow_anonymous"), + ("gcs.allow-anonymous", "gcs.skip-signature"), + ("gcs.allow-anonymous", "gcs.skip_signature"), + ("gcs.skip-signature", "gcs.google_skip_signature"), + ("gcs.skip_signature", "gcs.google_skip_signature"), + ("gcs.disable-vm-metadata", "gcs.disable_vm_metadata"), + ("gcs.disable-config-load", "gcs.disable_config_load"), +]; + +pub(crate) fn gcs_config_parse(props: HashMap<String, String>) -> Result<GcsConfig> { + let normalized = normalize_config(props); + let mut cfg = GcsConfig::default(); + + cfg.endpoint = normalized.get(GCS_ENDPOINT).cloned(); + cfg.credential = normalized.get(GCS_CREDENTIAL).cloned(); + cfg.credential_path = normalized.get(GCS_CREDENTIAL_PATH).cloned(); + cfg.service_account = normalized.get(GCS_SERVICE_ACCOUNT).cloned(); + cfg.scope = normalized.get("gcs.scope").cloned(); + cfg.predefined_acl = normalized.get("gcs.predefined-acl").cloned(); + cfg.default_storage_class = normalized.get("gcs.default-storage-class").cloned(); + cfg.token = normalized.get("gcs.token").cloned(); + + if let Some(v) = normalized.get(GCS_ALLOW_ANONYMOUS) { + if v.eq_ignore_ascii_case("true") { + cfg.allow_anonymous = true; + } + } + if let Some(v) = normalized.get("gcs.disable-vm-metadata") { + if v.eq_ignore_ascii_case("true") { + cfg.disable_vm_metadata = true; + } + } + if let Some(v) = normalized.get("gcs.disable-config-load") { + if v.eq_ignore_ascii_case("true") { + cfg.disable_config_load = true; + } + } + + Ok(cfg) +} + +pub(crate) fn gcs_config_build(cfg: &GcsConfig, path: &str) -> Result<Operator> { + let url = Url::parse(path).map_err(|_| Error::ConfigInvalid { + message: format!("Invalid GCS url: {path}"), + })?; + + let bucket = url.host_str().ok_or_else(|| Error::ConfigInvalid { + message: format!("Invalid GCS url: {path}, missing bucket"), + })?; + + let builder = cfg.clone().into_builder().bucket(bucket); + Ok(Operator::new(builder)?.finish()) +} + +fn normalize_config(props: HashMap<String, String>) -> HashMap<String, String> { Review Comment: nit: The config normalization and mirrored-key logic are duplicated across the backend modules. Maybe a small shared helper could be useful if we keep adding storage backends. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
