AWS_AUTH_CREDENTIALS_PROVIDER_IMDS_SOURCE_FAILURE

0

I'm building an IOS app using Swift on Xcode for a project, and wanted to connect to AWS S3 bucket, list objects, then display content from each individual files. I've AWS CLI settings setting up already: aws configure: AWS Access Key ID [****************GPMB]: AWS Secret Access Key [****************2JCF]: Default region name [us-east-1]

This is the full error I got from Xcode whenever I tried to connect to S3 using SDK: crtError(AwsCommonRuntimeKit.CRTError(code: 6153, message: "Valid credentials could not be sourced by the IMDS provider", name: "AWS_AUTH_CREDENTIALS_PROVIDER_IMDS_SOURCE_FAILURE"))

Following is the code:

public class ServiceHandler : ObservableObject { private var client: S3Client? init() { Task(priority: .high) { do { client = try S3Client(region: "us-east-2") } catch { print(error) } } } public func listBucketFiles(bucket: String) async throws -> [String] { if let clientInstance = client { let input = ListObjectsV2Input( bucket: bucket ) let output = try await clientInstance.listObjectsV2(input: input) var names: [String] = [] guard let objList = output.contents else { return [] }
for obj in objList { if let objName = obj.key { names.append(objName) } }
return names } else { print("Client has not been initialized!") return String } } struct ContentView: View { @StateObject private var s3 = ServiceHandler() var body: some View { let tmpStrAry = try await s3.listBucketFiles(bucket: "my_bucket")


Let me state my question more specific, ON https://docs.aws.amazon.com/sdk-for-swift/latest/developer-guide/using-configuration.html#using-custom-configurations The document states: By default, AWS SDK for Swift uses a basic default configuration for each AWS service. This configuration automatically looks for credentials to use in a predictable, standard way: The environment variables AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY_ID, and AWS_SESSION_TOKEN. The default AWS profile, as described in the AWS configuration file (located at ~/.aws/config on Linux and macOS and at C:\Users\USERNAME.aws\config on Windows), and the credentials found in the file ~/.aws/credentials on Linux and macOS and in C:\Users\USERNAME.aws\credentials on Windows.

  • Please provide the code in a code block (i.e. within ```) for readability.

1 Answer
0

Hello.

This error may occur if the access key cannot be obtained for some reason.
Does this occur even if I embed the access key directly in the code like below?

public class ServiceHandler: ObservableObject {
    private var client: S3Client?
    
    init() {
        Task(priority: .high) {
            do {
                client = try S3Client(
                    accessKeyId: "your-access-key",
                    secretAccessKey: "your-secret-key",
                    region: "us-east-2"
                )
            } catch {
                print(error)
            }
        }
    }
    
    public func listBucketFiles(bucket: String) async throws -> [String] {
        if let clientInstance = client {
            let input = ListObjectsV2Input(bucket: bucket)
            let output = try await clientInstance.listObjectsV2(input: input)
            var names: [String] = []
            
            guard let objList = output.contents else {
                return []
            }
            
            for obj in objList {
                if let objName = obj.key {
                    names.append(objName)
                }
            }
            
            return names
        } else {
            print("Client has not been initialized!")
            return []
        }
    }
}

struct ContentView: View {
    @StateObject private var s3 = ServiceHandler()
    
    var body: some View {
        let tmpStrAry = try await s3.listBucketFiles(bucket: "my_bucket")
    }
}

profile picture
EXPERT
answered 6 months ago
profile picture
EXPERT
reviewed 6 months ago
profile pictureAWS
EXPERT
reviewed 6 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions