From 67c6c0ba4054881fc8fa99c59a27b6c5aef71da6 Mon Sep 17 00:00:00 2001 From: Nikolay Kurtov Date: Tue, 11 Apr 2023 15:55:15 +0200 Subject: [PATCH] Remove `unwrap()`s when creating an S3 bucket. --- chain/client/src/sync/state.rs | 35 +++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/chain/client/src/sync/state.rs b/chain/client/src/sync/state.rs index 44123357e12..924afa065ee 100644 --- a/chain/client/src/sync/state.rs +++ b/chain/client/src/sync/state.rs @@ -151,21 +151,13 @@ impl StateSync { num_s3_requests_per_shard: u64, ) -> Self { let inner = if state_sync_from_s3_enabled { - // `unwrap()` here is fine, because the config validation has already - // ensured those fields are present. - let mut bucket = s3::Bucket::new( - s3_bucket, - s3_region.parse::().unwrap(), - s3::creds::Credentials::anonymous().unwrap(), - ) - .unwrap(); - // Ensure requests finish in finite amount of time. - bucket.set_request_timeout(Some(timeout)); - let bucket = Arc::new(bucket); - + let bucket = create_bucket(s3_bucket, s3_region, timeout); + if let Err(err) = bucket { + panic!("Failed to create an S3 bucket: {}", err); + } StateSyncInner::PartsFromExternal { chain_id: chain_id.to_string(), - bucket, + bucket: Arc::new(bucket.unwrap()), requests_remaining: Arc::new(AtomicI64::new(num_s3_requests_per_shard as i64)), } } else { @@ -1075,6 +1067,23 @@ impl StateSync { } } +fn create_bucket( + bucket: &str, + region: &str, + timeout: TimeDuration, +) -> Result { + let mut bucket = s3::Bucket::new( + bucket, + region.parse::().map_err(|err| near_chain::Error::Other(err.to_string()))?, + s3::creds::Credentials::anonymous() + .map_err(|err| near_chain::Error::Other(err.to_string()))?, + ) + .map_err(|err| near_chain::Error::Other(err.to_string()))?; + // Ensure requests finish in finite amount of time. + bucket.set_request_timeout(Some(timeout)); + Ok(bucket) +} + /// Returns parts that still need to be fetched. fn parts_to_fetch( new_shard_sync_download: &mut ShardSyncDownload,