You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@paimon.apache.org by lz...@apache.org on 2026/03/16 05:50:38 UTC

(paimon-rust) branch main updated: feat: add OSS storage support. (#134)

This is an automated email from the ASF dual-hosted git repository.

lzljs3620320 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/paimon-rust.git


The following commit(s) were added to refs/heads/main by this push:
     new 1247d8c  feat: add OSS storage support. (#134)
1247d8c is described below

commit 1247d8c0ce6533420a643ee5d6958aed791fbecf
Author: Zach <45...@users.noreply.github.com>
AuthorDate: Mon Mar 16 13:50:33 2026 +0800

    feat: add OSS storage support. (#134)
---
 crates/paimon/Cargo.toml            |   3 +-
 crates/paimon/src/io/mod.rs         |   5 ++
 crates/paimon/src/io/storage.rs     |  25 ++++++-
 crates/paimon/src/io/storage_oss.rs | 141 ++++++++++++++++++++++++++++++++++++
 4 files changed, 172 insertions(+), 2 deletions(-)

diff --git a/crates/paimon/Cargo.toml b/crates/paimon/Cargo.toml
index 4f6c6a6..5bfbc02 100644
--- a/crates/paimon/Cargo.toml
+++ b/crates/paimon/Cargo.toml
@@ -28,10 +28,11 @@ version.workspace = true
 
 [features]
 default = ["storage-memory", "storage-fs"]
-storage-all = ["storage-memory", "storage-fs"]
+storage-all = ["storage-memory", "storage-fs", "storage-oss"]
 
 storage-memory = ["opendal/services-memory"]
 storage-fs = ["opendal/services-fs"]
+storage-oss = ["opendal/services-oss"]
 
 [dependencies]
 url = "2.5.2"
diff --git a/crates/paimon/src/io/mod.rs b/crates/paimon/src/io/mod.rs
index a216946..4e69be4 100644
--- a/crates/paimon/src/io/mod.rs
+++ b/crates/paimon/src/io/mod.rs
@@ -30,3 +30,8 @@ use storage_fs::*;
 mod storage_memory;
 #[cfg(feature = "storage-memory")]
 use storage_memory::*;
+
+#[cfg(feature = "storage-oss")]
+mod storage_oss;
+#[cfg(feature = "storage-oss")]
+use storage_oss::*;
diff --git a/crates/paimon/src/io/storage.rs b/crates/paimon/src/io/storage.rs
index 14c528c..1abfbc0 100644
--- a/crates/paimon/src/io/storage.rs
+++ b/crates/paimon/src/io/storage.rs
@@ -15,6 +15,8 @@
 // specific language governing permissions and limitations
 // under the License.
 
+#[cfg(feature = "storage-oss")]
+use opendal::services::OssConfig;
 use opendal::{Operator, Scheme};
 
 use crate::error;
@@ -28,11 +30,13 @@ pub enum Storage {
     Memory,
     #[cfg(feature = "storage-fs")]
     LocalFs,
+    #[cfg(feature = "storage-oss")]
+    Oss { config: Box<OssConfig> },
 }
 
 impl Storage {
     pub(crate) fn build(file_io_builder: FileIOBuilder) -> crate::Result<Self> {
-        let (scheme_str, _) = file_io_builder.into_parts();
+        let (scheme_str, _props) = file_io_builder.into_parts();
         let scheme = Self::parse_scheme(&scheme_str)?;
 
         match scheme {
@@ -40,6 +44,13 @@ impl Storage {
             Scheme::Memory => Ok(Self::Memory),
             #[cfg(feature = "storage-fs")]
             Scheme::Fs => Ok(Self::LocalFs),
+            #[cfg(feature = "storage-oss")]
+            Scheme::Oss => {
+                let config = super::oss_config_parse(_props)?;
+                Ok(Self::Oss {
+                    config: Box::new(config),
+                })
+            }
             _ => Err(error::Error::IoUnsupported {
                 message: "Unsupported storage feature".to_string(),
             }),
@@ -68,6 +79,18 @@ impl Storage {
                     Ok((op, &path[1..]))
                 }
             }
+            #[cfg(feature = "storage-oss")]
+            Storage::Oss { config } => {
+                let op = super::oss_config_build(config, path)?;
+                let prefix = format!("oss://{}/", op.info().name());
+                if let Some(stripped) = path.strip_prefix(&prefix) {
+                    Ok((op, stripped))
+                } else {
+                    Err(error::Error::ConfigInvalid {
+                        message: format!("Invalid OSS url: {path}, should start with {prefix}"),
+                    })
+                }
+            }
         }
     }
 
diff --git a/crates/paimon/src/io/storage_oss.rs b/crates/paimon/src/io/storage_oss.rs
new file mode 100644
index 0000000..884b0a7
--- /dev/null
+++ b/crates/paimon/src/io/storage_oss.rs
@@ -0,0 +1,141 @@
+// 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::OssConfig;
+use opendal::{Configurator, Operator};
+use url::Url;
+
+use crate::error::Error;
+use crate::Result;
+
+/// Configuration key for OSS endpoint.
+///
+/// Compatible with paimon-java's `fs.oss.endpoint`.
+const OSS_ENDPOINT: &str = "fs.oss.endpoint";
+
+/// Configuration key for OSS access key ID.
+///
+/// Compatible with paimon-java's `fs.oss.accessKeyId`.
+const OSS_ACCESS_KEY_ID: &str = "fs.oss.accessKeyId";
+
+/// Configuration key for OSS access key secret.
+///
+/// Compatible with paimon-java's `fs.oss.accessKeySecret`.
+const OSS_ACCESS_KEY_SECRET: &str = "fs.oss.accessKeySecret";
+
+/// Parse paimon catalog options into an [`OssConfig`].
+///
+/// Extracts OSS-related configuration keys (endpoint, access key, secret key)
+/// from the provided properties map and maps them to the corresponding
+/// [`OssConfig`] fields.
+///
+/// Returns an error if any required configuration key is missing.
+pub(crate) fn oss_config_parse(mut props: HashMap<String, String>) -> Result<OssConfig> {
+    let mut cfg = OssConfig::default();
+
+    cfg.endpoint = Some(
+        props
+            .remove(OSS_ENDPOINT)
+            .ok_or_else(|| Error::ConfigInvalid {
+                message: format!("Missing required OSS config: {OSS_ENDPOINT}"),
+            })?,
+    );
+    cfg.access_key_id =
+        Some(
+            props
+                .remove(OSS_ACCESS_KEY_ID)
+                .ok_or_else(|| Error::ConfigInvalid {
+                    message: format!("Missing required OSS config: {OSS_ACCESS_KEY_ID}"),
+                })?,
+        );
+    cfg.access_key_secret =
+        Some(
+            props
+                .remove(OSS_ACCESS_KEY_SECRET)
+                .ok_or_else(|| Error::ConfigInvalid {
+                    message: format!("Missing required OSS config: {OSS_ACCESS_KEY_SECRET}"),
+                })?,
+        );
+
+    Ok(cfg)
+}
+
+/// Build an [`Operator`] for the given OSS path.
+///
+/// Parses the bucket name from the `oss://bucket/key` URL and combines it
+/// with the provided [`OssConfig`] to construct an OpenDAL operator.
+pub(crate) fn oss_config_build(cfg: &OssConfig, path: &str) -> Result<Operator> {
+    let url = Url::parse(path).map_err(|_| Error::ConfigInvalid {
+        message: format!("Invalid OSS url: {path}"),
+    })?;
+
+    let bucket = url.host_str().ok_or_else(|| Error::ConfigInvalid {
+        message: format!("Invalid OSS url: {path}, missing bucket"),
+    })?;
+
+    let builder = cfg.clone().into_builder().bucket(bucket);
+    Ok(Operator::new(builder)?.finish())
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_oss_config_parse_with_all_keys() {
+        let mut props = HashMap::new();
+        props.insert(
+            OSS_ENDPOINT.to_string(),
+            "https://oss-cn-hangzhou.aliyuncs.com".to_string(),
+        );
+        props.insert(OSS_ACCESS_KEY_ID.to_string(), "test-ak".to_string());
+        props.insert(OSS_ACCESS_KEY_SECRET.to_string(), "test-sk".to_string());
+
+        let cfg = oss_config_parse(props).unwrap();
+        assert_eq!(
+            cfg.endpoint.as_deref(),
+            Some("https://oss-cn-hangzhou.aliyuncs.com")
+        );
+        assert_eq!(cfg.access_key_id.as_deref(), Some("test-ak"));
+        assert_eq!(cfg.access_key_secret.as_deref(), Some("test-sk"));
+    }
+
+    #[test]
+    fn test_oss_config_build_extracts_bucket() {
+        let mut cfg = OssConfig::default();
+        cfg.endpoint = Some("https://oss-cn-hangzhou.aliyuncs.com".to_string());
+
+        let op = oss_config_build(&cfg, "oss://my-bucket/some/path").unwrap();
+        assert_eq!(op.info().name(), "my-bucket");
+    }
+
+    #[test]
+    fn test_oss_config_build_invalid_url() {
+        let cfg = OssConfig::default();
+        let result = oss_config_build(&cfg, "not-a-valid-url");
+        assert!(result.is_err());
+    }
+
+    #[test]
+    fn test_oss_config_build_missing_bucket() {
+        let cfg = OssConfig::default();
+        let result = oss_config_build(&cfg, "oss:///path/without/bucket");
+        assert!(result.is_err());
+    }
+}