Attempt find AWS S3 credentials via the AWS SDK
()
| 173 | impl CredentialsFromConfig { |
| 174 | /// Attempt find AWS S3 credentials via the AWS SDK |
| 175 | pub async fn try_new() -> Result<Self> { |
| 176 | let config = aws_config::defaults(BehaviorVersion::latest()).load().await; |
| 177 | let region = config.region().map(|r| r.to_string()); |
| 178 | |
| 179 | let credentials = config |
| 180 | .credentials_provider() |
| 181 | .ok_or_else(|| { |
| 182 | DataFusionError::ObjectStore(Box::new(Generic { |
| 183 | store: "S3", |
| 184 | source: "Failed to get S3 credentials aws_config".into(), |
| 185 | })) |
| 186 | })? |
| 187 | .clone(); |
| 188 | |
| 189 | // The credential provider is lazy, so it does not fetch credentials |
| 190 | // until they are needed. To ensure that the credentials are valid, |
| 191 | // we can call `provide_credentials` here. |
| 192 | let credentials = match credentials.provide_credentials().await { |
| 193 | Ok(_) => Some(credentials), |
| 194 | Err(CredentialsError::CredentialsNotLoaded(_)) => { |
| 195 | debug!("Could not use AWS SDK to get credentials"); |
| 196 | None |
| 197 | } |
| 198 | // other errors like `CredentialsError::InvalidConfiguration` |
| 199 | // should be returned to the user so they can be fixed |
| 200 | Err(e) => { |
| 201 | // Pass back underlying error to the user, including underlying source |
| 202 | let source_message = if let Some(source) = e.source() { |
| 203 | format!(": {source}") |
| 204 | } else { |
| 205 | String::new() |
| 206 | }; |
| 207 | |
| 208 | let message = format!( |
| 209 | "Error getting credentials from provider: {e}{source_message}", |
| 210 | ); |
| 211 | |
| 212 | return Err(DataFusionError::ObjectStore(Box::new(Generic { |
| 213 | store: "S3", |
| 214 | source: message.into(), |
| 215 | }))); |
| 216 | } |
| 217 | }; |
| 218 | Ok(Self { |
| 219 | region, |
| 220 | credentials, |
| 221 | }) |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | #[derive(Debug)] |