Skip to content

Commit b6d9107

Browse files
alamminsaloalamminsalo
alamminsalo
authored and
alamminsalo
committed
Try AWS_PROFILE profile before fallbacking to default credentials
1 parent 5b28f6b commit b6d9107

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

martin/src/pmtiles/mod.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,21 @@ impl_pmtiles_source!(PmtS3Source, S3Backend, Url, identity, InvalidUrlMetadata);
310310
impl PmtS3Source {
311311
pub async fn new(cache: PmtCache, id: String, url: Url) -> FileResult<Self> {
312312
let bucket_name = url.host_str().ok_or_else(|| FileError::S3SourceError(format!("failed to parse bucket name from {url}")))?;
313-
let credentials = Credentials::default().map_err(|_| FileError::S3SourceError(format!("failed to read AWS credentials for {url}")))?;
313+
314+
// Try to get profile from AWS_PROFILE environment variable first,
315+
// then proceed to try Credentials::default() that attempts default profile or environment variables.
316+
let credentials = {
317+
let profile = env::var("AWS_PROFILE").ok();
318+
319+
let mut credentials = Credentials::new(None,None,None,None, profile.as_deref());
320+
if credentials.is_err() {
321+
credentials = Credentials::default();
322+
}
323+
credentials
324+
}.map_err(|_| FileError::S3SourceError(format!("failed to read AWS credentials")))?;
325+
314326
let region: String =
315-
env::var("AWS_REGION").map_err(|_| FileError::S3SourceError(format!("failed to get AWS_REGION environment variable for {url}")))?;
327+
env::var("AWS_REGION").map_err(|_| FileError::S3SourceError(format!("failed to read AWS_REGION environment variable")))?;
316328
let bucket = Bucket::new(bucket_name, region.parse().map_err(|_| FileError::S3SourceError(format!("failed to parse region")))? , credentials)
317329
.map_err(|_| FileError::S3SourceError(format!("failed to instantiate bucket for {url}")))?;
318330

0 commit comments

Comments
 (0)