Skip to content

Commit 752fcec

Browse files
authored
Making region optional (#1658)
1 parent 8b16513 commit 752fcec

File tree

6 files changed

+34
-24
lines changed

6 files changed

+34
-24
lines changed

server/src/blob_store/mod.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub mod registry;
2828
#[derive(Debug, Clone, Serialize, Deserialize)]
2929
pub struct BlobStorageConfig {
3030
pub path: String,
31-
pub region: String,
31+
pub region: Option<String>,
3232
}
3333

3434
impl Default for BlobStorageConfig {
@@ -43,7 +43,7 @@ impl Default for BlobStorageConfig {
4343
);
4444
BlobStorageConfig {
4545
path: blob_store_path,
46-
region: "".to_string(),
46+
region: None,
4747
}
4848
}
4949
}
@@ -67,7 +67,7 @@ impl BlobStorage {
6767
pub fn new(config: BlobStorageConfig) -> Result<Self> {
6868
let url = &config.path.clone();
6969
debug!("using blob store path: {}", url);
70-
let (object_store, path) = Self::build_object_store(url, &config.region)?;
70+
let (object_store, path) = Self::build_object_store(url, config.region.clone())?;
7171
Ok(Self {
7272
object_store: Arc::new(object_store),
7373
url_scheme: url.parse::<Url>()?.scheme().to_string(),
@@ -77,22 +77,28 @@ impl BlobStorage {
7777
})
7878
}
7979

80-
pub fn build_object_store(url_str: &str, region: &str) -> Result<(Box<dyn ObjectStore>, Path)> {
80+
pub fn build_object_store(
81+
url_str: &str,
82+
region: Option<String>,
83+
) -> Result<(Box<dyn ObjectStore>, Path)> {
8184
let url = &url_str.parse::<Url>()?;
8285
let (scheme, _) = ObjectStoreScheme::parse(url)?;
8386
match scheme {
8487
ObjectStoreScheme::AmazonS3 => {
8588
// inject AWS environment variables to prioritize keys over instance metadata
8689
// credentials.
87-
let s3_builder = AmazonS3Builder::from_env()
90+
let mut s3_builder = AmazonS3Builder::from_env()
8891
.with_url(url_str)
8992
.with_allow_http(true)
90-
.with_conditional_put(S3ConditionalPut::ETagMatch)
91-
.with_region(region)
92-
.build()
93-
.expect("failed to create object store");
93+
.with_conditional_put(S3ConditionalPut::ETagMatch);
94+
95+
if let Some(region) = region {
96+
s3_builder = s3_builder.with_region(region);
97+
}
98+
99+
let obj_store = s3_builder.build().expect("failed to create object store");
94100
let (_, path) = parse_url(url)?;
95-
Ok((Box::new(s3_builder), path))
101+
Ok((Box::new(obj_store), path))
96102
}
97103
_ => Ok(parse_url(url)?),
98104
}

server/src/blob_store/registry.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,29 @@ pub struct BlobStorageRegistry {
1313
}
1414

1515
impl BlobStorageRegistry {
16-
pub fn new(default_blob_storage_path: &str, default_blob_storage_region: &str) -> Result<Self> {
16+
pub fn new(
17+
default_blob_storage_path: &str,
18+
default_blob_storage_region: Option<String>,
19+
) -> Result<Self> {
1720
let default_blob_storage = BlobStorage::new(BlobStorageConfig {
1821
path: default_blob_storage_path.to_string(),
19-
region: default_blob_storage_region.to_string(),
22+
region: default_blob_storage_region,
2023
})?;
2124
Ok(Self {
2225
blob_storage_buckets: Mutex::new(HashMap::new()),
2326
default_blob_storage: Arc::new(default_blob_storage),
2427
})
2528
}
2629

27-
pub fn create_new_blob_store(&self, namespace: &str, path: &str, region: &str) -> Result<()> {
30+
pub fn create_new_blob_store(
31+
&self,
32+
namespace: &str,
33+
path: &str,
34+
region: Option<String>,
35+
) -> Result<()> {
2836
let blob_storage = BlobStorage::new(BlobStorageConfig {
2937
path: path.to_string(),
30-
region: region.to_string(),
38+
region,
3139
})?;
3240
self.blob_storage_buckets
3341
.lock()

server/src/routes_internal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,15 +312,15 @@ async fn create_namespace(
312312
Json(namespace): Json<CreateNamespace>,
313313
) -> Result<(), IndexifyAPIError> {
314314
if let Some(blob_storage_bucket) = &namespace.blob_storage_bucket {
315-
let Some(blob_storage_region) = &namespace.blob_storage_region else {
315+
let Some(_blob_storage_region) = &namespace.blob_storage_region else {
316316
return Err(IndexifyAPIError::bad_request(
317317
"blob storage region is required",
318318
));
319319
};
320320
if let Err(e) = state.blob_storage.create_new_blob_store(
321321
&namespace.name,
322322
blob_storage_bucket,
323-
&blob_storage_region,
323+
namespace.blob_storage_region.clone(),
324324
) {
325325
error!("failed to create blob storage bucket: {:?}", e);
326326
return Err(IndexifyAPIError::internal_error(e));

server/src/service.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,16 @@ impl Service {
6666

6767
let blob_storage_registry = Arc::new(BlobStorageRegistry::new(
6868
config.blob_storage.path.as_str(),
69-
config.blob_storage.region.as_str(),
69+
config.blob_storage.region.clone(),
7070
)?);
7171

7272
let namespaces = indexify_state.reader().get_all_namespaces()?;
7373
for namespace in namespaces {
7474
if let Some(blob_storage_bucket) = namespace.blob_storage_bucket {
75-
let blob_storage_region = namespace.blob_storage_region.unwrap_or_else(|| {
76-
std::env::var("AWS_REGION").unwrap_or_else(|_| "us-east-1".to_string())
77-
});
78-
7975
blob_storage_registry.create_new_blob_store(
8076
&namespace.name,
8177
&blob_storage_bucket,
82-
&blob_storage_region,
78+
namespace.blob_storage_region.clone(),
8379
)?;
8480
}
8581
}

server/src/state_store/kv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ mod tests {
9191
"file://{}",
9292
temp_dir.path().join("blob_store").to_str().unwrap()
9393
),
94-
region: "local".to_string(),
94+
region: None,
9595
})?),
9696
"test",
9797
)

server/src/testing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl TestService {
6666
"file://{}",
6767
temp_dir.path().join("blob_store").to_str().unwrap()
6868
),
69-
region: "local".to_string(),
69+
region: None,
7070
},
7171
..Default::default()
7272
};

0 commit comments

Comments
 (0)