in core/src/services/obs/backend.rs [159:310]
fn build(self) -> Result<impl Access> {
debug!("backend build started: {:?}", &self);
let root = normalize_root(&self.config.root.unwrap_or_default());
debug!("backend use root {}", root);
let bucket = match &self.config.bucket {
Some(bucket) => Ok(bucket.to_string()),
None => Err(
Error::new(ErrorKind::ConfigInvalid, "The bucket is misconfigured")
.with_context("service", Scheme::Obs),
),
}?;
debug!("backend use bucket {}", &bucket);
let uri = match &self.config.endpoint {
Some(endpoint) => endpoint.parse::<Uri>().map_err(|err| {
Error::new(ErrorKind::ConfigInvalid, "endpoint is invalid")
.with_context("service", Scheme::Obs)
.set_source(err)
}),
None => Err(Error::new(ErrorKind::ConfigInvalid, "endpoint is empty")
.with_context("service", Scheme::Obs)),
}?;
let scheme = match uri.scheme_str() {
Some(scheme) => scheme.to_string(),
None => "https".to_string(),
};
let (endpoint, is_obs_default) = {
let host = uri.host().unwrap_or_default().to_string();
if host.starts_with("obs.")
&& (host.ends_with(".myhuaweicloud.com") || host.ends_with(".huawei.com"))
{
(format!("{bucket}.{host}"), true)
} else {
(host, false)
}
};
debug!("backend use endpoint {}", &endpoint);
let mut cfg = HuaweicloudObsConfig::default();
// Load cfg from env first.
cfg = cfg.from_env();
if let Some(v) = self.config.access_key_id {
cfg.access_key_id = Some(v);
}
if let Some(v) = self.config.secret_access_key {
cfg.secret_access_key = Some(v);
}
let loader = HuaweicloudObsCredentialLoader::new(cfg);
// Set the bucket name in CanonicalizedResource.
// 1. If the bucket is bound to a user domain name, use the user domain name as the bucket name,
// for example, `/obs.ccc.com/object`. `obs.ccc.com` is the user domain name bound to the bucket.
// 2. If you do not access OBS using a user domain name, this field is in the format of `/bucket/object`.
//
// Please refer to this doc for more details:
// https://support.huaweicloud.com/intl/en-us/api-obs/obs_04_0010.html
let signer = HuaweicloudObsSigner::new({
if is_obs_default {
&bucket
} else {
&endpoint
}
});
debug!("backend build finished");
Ok(ObsBackend {
core: Arc::new(ObsCore {
info: {
let am = AccessorInfo::default();
am.set_scheme(Scheme::Obs)
.set_root(&root)
.set_name(&bucket)
.set_native_capability(Capability {
stat: true,
stat_with_if_match: true,
stat_with_if_none_match: true,
stat_has_cache_control: true,
stat_has_content_length: true,
stat_has_content_type: true,
stat_has_content_encoding: true,
stat_has_content_range: true,
stat_has_etag: true,
stat_has_content_md5: true,
stat_has_last_modified: true,
stat_has_content_disposition: true,
stat_has_user_metadata: true,
read: true,
read_with_if_match: true,
read_with_if_none_match: true,
write: true,
write_can_empty: true,
write_can_append: true,
write_can_multi: true,
write_with_content_type: true,
write_with_cache_control: true,
// The min multipart size of OBS is 5 MiB.
//
// ref: <https://support.huaweicloud.com/intl/en-us/ugobs-obs/obs_41_0021.html>
write_multi_min_size: Some(5 * 1024 * 1024),
// The max multipart size of OBS is 5 GiB.
//
// ref: <https://support.huaweicloud.com/intl/en-us/ugobs-obs/obs_41_0021.html>
write_multi_max_size: if cfg!(target_pointer_width = "64") {
Some(5 * 1024 * 1024 * 1024)
} else {
Some(usize::MAX)
},
write_with_user_metadata: true,
delete: true,
copy: true,
list: true,
list_with_recursive: true,
list_has_content_length: true,
presign: true,
presign_stat: true,
presign_read: true,
presign_write: true,
shared: true,
..Default::default()
});
// allow deprecated api here for compatibility
#[allow(deprecated)]
if let Some(client) = self.http_client {
am.update_http_client(|_| client);
}
am.into()
},
bucket,
root,
endpoint: format!("{}://{}", &scheme, &endpoint),
signer,
loader,
}),
})
}