MCPcopy Create free account
hub / github.com/apache/datafusion / try_new

Method try_new

datafusion-cli/src/object_storage.rs:175–222  ·  view source on GitHub ↗

Attempt find AWS S3 credentials via the AWS SDK

()

Source from the content-addressed store, hash-verified

173impl 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)]

Callers

nothing calls this directly

Calls 7

newFunction · 0.85
loadMethod · 0.45
mapMethod · 0.45
to_stringMethod · 0.45
cloneMethod · 0.45
intoMethod · 0.45
sourceMethod · 0.45

Tested by

no test coverage detected