From d062e02b7d5978d1379a77c5bfa400f07595dfe2 Mon Sep 17 00:00:00 2001 From: xxchan Date: Sun, 12 Feb 2023 16:50:33 +0100 Subject: [PATCH] feat(oli): support s3 uri without profile (#1328) --- binaries/oli/src/utils/location.rs | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/binaries/oli/src/utils/location.rs b/binaries/oli/src/utils/location.rs index b77f0e0b5db2..bed32d947467 100644 --- a/binaries/oli/src/utils/location.rs +++ b/binaries/oli/src/utils/location.rs @@ -37,11 +37,25 @@ pub fn parse_location(s: &str) -> Result<(Operator, &str)> { let s = s.splitn(2, "://").collect::>(); debug_assert!(s.len() == 2); - Ok((parse_profile(s[0])?, s[1])) + + if let Ok(scheme) = Scheme::from_str(s[0]) { + // If name is a valid scheme, we will load from env directly. + match scheme { + Scheme::S3 => { + let (bucket, location) = parse_s3_uri(s[1]); + let mut builder = services::S3::default(); + builder.bucket(bucket); + Ok((Operator::create(builder)?.finish(), location)) + } + _ => todo!(), + } + } else { + // Otherwise, try to load profile. + Ok((parse_profile(s[0])?, s[1])) + } } -/// If name is a valid scheme, we will load from env directly. -/// Or, we will try to get env from `OLI_PROFILE_{NAME}_XXX`. +/// Try to get env from `OLI_PROFILE_{NAME}_XXX`. /// /// Especially, the type is specified by `OLI_PROFILE_{NAME}_TYPE` pub fn parse_profile(name: &str) -> Result { @@ -67,3 +81,12 @@ pub fn parse_profile(name: &str) -> Result { Ok(op) } + +/// Parses bucket and key from [`S3Uri`](https://docs.aws.amazon.com/cli/latest/reference/s3/#path-argument-type) +fn parse_s3_uri(s3uri: &str) -> (&str, &str) { + // TODO: support ARN + + let s = s3uri.splitn(2, "/").collect::>(); + debug_assert!(s.len() == 2); + return (s[0], s[1]); +}