-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
spi-obj: support outside custom s3 client (#852)
- Loading branch information
1 parent
11a80f0
commit 77ec505
Showing
11 changed files
with
298 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod object_obj_serv; | ||
pub mod obs; | ||
pub mod s3; | ||
pub mod custom_s3; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod object_custom_s3_initializer; | ||
pub mod object_custom_s3_obj_serv; |
24 changes: 24 additions & 0 deletions
24
backend/spi/spi-object/src/serv/custom_s3/object_custom_s3_initializer.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use std::collections::HashMap; | ||
|
||
use bios_basic::spi::{dto::spi_bs_dto::SpiBsCertResp, spi_funs::SpiBsInst}; | ||
use tardis::{basic::{dto::TardisContext, error::TardisError, result::TardisResult}, config::config_dto::OSModuleConfig, os::os_client::TardisOSClient, TardisFuns}; | ||
|
||
use tardis::serde_json::Value as JsonValue; | ||
|
||
/// 自定义外部obs服务初始化 | ||
/// 外部服务由API申请资源时初始化,不需要随系统spi初始化 | ||
pub async fn init(bs_cert: &SpiBsCertResp, _ctx: &TardisContext, _mgr: bool) -> TardisResult<SpiBsInst> { | ||
let ext = TardisFuns::json.str_to_json(&bs_cert.ext)?; | ||
let region = ext | ||
.get("region") | ||
.and_then(JsonValue::as_str) | ||
.ok_or_else(|| TardisError::bad_request("Tardis context ext should have a `region` field with type string", "400-spi-invalid-tardis-ctx"))?; | ||
let default_bucket = ext | ||
.get("default_bucket") | ||
.and_then(JsonValue::as_str) | ||
.ok_or_else(|| TardisError::bad_request("Tardis context ext should have a `region` field with type string", "400-spi-invalid-tardis-ctx"))?; | ||
let tardis_os_config = OSModuleConfig::builder().kind("s3").endpoint(&bs_cert.conn_uri).ak(&bs_cert.ak).sk(&bs_cert.sk).region(region).default_bucket(default_bucket).build(); | ||
let client = TardisOSClient::init(&tardis_os_config)?; | ||
Ok(SpiBsInst { client: Box::new(client), ext: HashMap::new() }) | ||
} | ||
|
52 changes: 52 additions & 0 deletions
52
backend/spi/spi-object/src/serv/custom_s3/object_custom_s3_obj_serv.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use bios_basic::spi::dto::spi_bs_dto::SpiBsCertResp; | ||
use bios_basic::spi::dto::spi_bs_dto::SpiBsDetailResp; | ||
use bios_basic::spi::spi_constants; | ||
use bios_basic::spi::spi_funs::SpiBsInst; | ||
use std::collections::HashMap; | ||
use std::sync::OnceLock; | ||
use std::sync::Arc; | ||
|
||
use tardis::{ | ||
basic::{dto::TardisContext, result::TardisResult}, | ||
os::os_client::TardisOSClient, | ||
tokio::sync::RwLock, | ||
}; | ||
|
||
use crate::serv::s3::S3; | ||
|
||
/// 自定义外部s3服务 | ||
/// 文件服务支持绕过spi配置而直接根据外部传入的配置创建客户端连接。 | ||
pub(crate) struct CustomS3Service; | ||
impl S3 for CustomS3Service { | ||
async fn rebuild_path(_bucket_name: Option<&str>, origin_path: &str, _obj_exp: Option<u32>, _client: &TardisOSClient) -> TardisResult<String> { | ||
Ok(origin_path.to_string()) | ||
} | ||
} | ||
|
||
impl CustomS3Service { | ||
pub async fn get_bs(bs_cert: &SpiBsDetailResp, ctx: &TardisContext) -> TardisResult<Arc<SpiBsInst>> { | ||
{ | ||
let read = Self::get_custom_bs_caches().read().await; | ||
if let Some(inst) = read.get(&bs_cert.id).cloned() { | ||
return Ok(inst); | ||
} | ||
} | ||
let mut spi_bs_inst = crate::serv::custom_s3::object_custom_s3_initializer::init(&SpiBsCertResp { | ||
kind_code: bs_cert.kind_code.clone(), | ||
conn_uri: bs_cert.conn_uri.clone(), | ||
ak: bs_cert.ak.clone(), | ||
sk: bs_cert.sk.clone(), | ||
ext: bs_cert.ext.clone(), | ||
private: bs_cert.private, | ||
}, ctx, true).await?; | ||
{ | ||
let mut write = Self::get_custom_bs_caches().write().await; | ||
spi_bs_inst.ext.insert(spi_constants::SPI_KIND_CODE_FLAG.to_string(), bs_cert.kind_code.clone()); | ||
return Ok(write.entry(bs_cert.id.clone()).or_insert(Arc::new(spi_bs_inst)).clone()); | ||
} | ||
} | ||
fn get_custom_bs_caches() -> &'static RwLock<HashMap<String, Arc<SpiBsInst>>> { | ||
static CUSTOM_BS_CACHES: OnceLock<RwLock<HashMap<String, Arc<SpiBsInst>>>> = OnceLock::new(); | ||
CUSTOM_BS_CACHES.get_or_init(Default::default) | ||
} | ||
} |
Oops, something went wrong.