2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-12-22 03:11:24 +00:00

Add vhost into endpoint URL if it isn't already specified

This commit is contained in:
asonix 2024-12-08 23:37:40 -06:00
parent 62bb25ac44
commit 3879a45b32

View file

@ -29,6 +29,9 @@ pub(crate) enum ObjectError {
#[error("Failed to build object store client")] #[error("Failed to build object store client")]
BuildClient(#[source] object_store::Error), BuildClient(#[source] object_store::Error),
#[error("Failed to set hostname for object storage")]
SetHost,
#[error("Task cancelled")] #[error("Task cancelled")]
Canceled, Canceled,
} }
@ -36,7 +39,9 @@ pub(crate) enum ObjectError {
impl ObjectError { impl ObjectError {
pub(super) const fn error_code(&self) -> ErrorCode { pub(super) const fn error_code(&self) -> ErrorCode {
match self { match self {
Self::BuildClient(_) | Self::Request(_) => ErrorCode::OBJECT_REQUEST_ERROR, Self::SetHost | Self::BuildClient(_) | Self::Request(_) => {
ErrorCode::OBJECT_REQUEST_ERROR
}
Self::IO(_) => ErrorCode::OBJECT_IO_ERROR, Self::IO(_) => ErrorCode::OBJECT_IO_ERROR,
Self::Canceled => ErrorCode::PANIC, Self::Canceled => ErrorCode::PANIC,
} }
@ -305,7 +310,7 @@ impl ObjectStore {
#[tracing::instrument(skip(endpoint, access_key, secret_key, session_token), fields(endpoint = %endpoint))] #[tracing::instrument(skip(endpoint, access_key, secret_key, session_token), fields(endpoint = %endpoint))]
pub(crate) async fn new( pub(crate) async fn new(
crate::config::ObjectStorage { crate::config::ObjectStorage {
endpoint, mut endpoint,
bucket_name, bucket_name,
use_path_style, use_path_style,
region, region,
@ -323,10 +328,23 @@ impl ObjectStore {
.with_timeout(Duration::from_secs(client_timeout)) .with_timeout(Duration::from_secs(client_timeout))
.with_allow_http(!https); .with_allow_http(!https);
let use_vhost_style = !use_path_style;
if use_vhost_style {
if let Some(host) = endpoint.host() {
if !host.to_string().starts_with(&bucket_name) {
let new_host = format!("{bucket_name}.{host}");
endpoint
.set_host(Some(&new_host))
.map_err(|_| ObjectError::SetHost)?;
}
}
}
let builder = AmazonS3Builder::new() let builder = AmazonS3Builder::new()
.with_endpoint(endpoint.as_str().trim_end_matches('/')) .with_endpoint(endpoint.as_str().trim_end_matches('/'))
.with_bucket_name(bucket_name) .with_bucket_name(bucket_name)
.with_virtual_hosted_style_request(!use_path_style) .with_virtual_hosted_style_request(use_vhost_style)
.with_region(region) .with_region(region)
.with_access_key_id(access_key) .with_access_key_id(access_key)
.with_secret_access_key(secret_key) .with_secret_access_key(secret_key)