iceberg/io/
storage_oss.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::collections::HashMap;
19
20use opendal::services::OssConfig;
21use opendal::{Configurator, Operator};
22use url::Url;
23
24use crate::{Error, ErrorKind, Result};
25
26/// Required configuration arguments for creating an Aliyun OSS Operator with OpenDAL:
27/// - `oss.endpoint`: The OSS service endpoint URL
28/// - `oss.access-key-id`: The access key ID for authentication
29/// - `oss.access-key-secret`: The access key secret for authentication
30///   Aliyun oss endpoint.
31pub const OSS_ENDPOINT: &str = "oss.endpoint";
32/// Aliyun oss access key id.
33pub const OSS_ACCESS_KEY_ID: &str = "oss.access-key-id";
34/// Aliyun oss access key secret.
35pub const OSS_ACCESS_KEY_SECRET: &str = "oss.access-key-secret";
36
37/// Parse iceberg props to oss config.
38pub(crate) fn oss_config_parse(mut m: HashMap<String, String>) -> Result<OssConfig> {
39    let mut cfg: OssConfig = OssConfig::default();
40    if let Some(endpoint) = m.remove(OSS_ENDPOINT) {
41        cfg.endpoint = Some(endpoint);
42    };
43    if let Some(access_key_id) = m.remove(OSS_ACCESS_KEY_ID) {
44        cfg.access_key_id = Some(access_key_id);
45    };
46    if let Some(access_key_secret) = m.remove(OSS_ACCESS_KEY_SECRET) {
47        cfg.access_key_secret = Some(access_key_secret);
48    };
49
50    Ok(cfg)
51}
52
53/// Build new opendal operator from give path.
54pub(crate) fn oss_config_build(cfg: &OssConfig, path: &str) -> Result<Operator> {
55    let url = Url::parse(path)?;
56    let bucket = url.host_str().ok_or_else(|| {
57        Error::new(
58            ErrorKind::DataInvalid,
59            format!("Invalid oss url: {}, missing bucket", path),
60        )
61    })?;
62
63    let builder = cfg.clone().into_builder().bucket(bucket);
64
65    Ok(Operator::new(builder)?.finish())
66}