Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RekognitionClient doesn't accept credentials #1813

Open
oanav21 opened this issue Nov 6, 2024 · 1 comment
Open

RekognitionClient doesn't accept credentials #1813

oanav21 opened this issue Nov 6, 2024 · 1 comment
Labels
documentation This is a problem with documentation. needs-triage This issue or PR still needs to be triaged.

Comments

@oanav21
Copy link

oanav21 commented Nov 6, 2024

Describe the issue

To create the RekognitionClient, in an older documentation, this was the way to do it:

  let rekognitionClient = RekognitionClient(
        credentialsProvider: credentialsProvider,
        region: REGION
    )

But now, it only accepts the region as a parameter and I don't know how to pass the credentials to it.
Please, redirect me to the proper documentation.

Example of usage:

How we get the credentials:

@MainActor
    func authenticateWithCognito(idToken: String?) async throws -> Bool {
        
        guard let idToken = idToken else {
            print("ID Token is null")
            throw AuthenticationError.idTokenNull
        }
        
        let cognitoClient = try CognitoIdentityClient(region: REGION)
        
        let getIdRequest = GetIdInput(
            identityPoolId: IDENTITY_POOL_ID,
            logins: [PROVIDER_NAME: idToken]
        )
        
        do {
            
            let identityResponse = try await cognitoClient.getId(input: getIdRequest)
            guard let identityId = identityResponse.identityId else {
                throw AuthenticationError.failedToRetrieveIdentityId
            }
            
            awsCredentials = try await getAwsCredentials(identityPoolId: IDENTITY_POOL_ID, idToken: idToken, region: REGION)
            
            return true

        } catch {
            
            let errorMessage = error.localizedDescription.lowercased()
            if !errorMessage.isEmpty,
               errorMessage.contains("notauthorizedexception") {
                print("Token expired or invalid, refreshing Firebase token")
                let newToken = try await refreshFirebaseTokenAndAuthenticate()
                return try await authenticateWithCognito(idToken: newToken)
            } else {
                print("Error during Cognito authentication: \(error.localizedDescription)")
                throw AuthenticationError.cognitoAuthenticationFailed(error.localizedDescription)
            }
        }
        
    }

How we tried to use rekognition based on aws-sdk-kotlin, which we've managed to implement on android already:

@MainActor
    func startPhotoRekognition(isImageFromGallery: Bool, sourceImage: UIImage) async throws -> Bool {
        
        guard let imageData = sourceImage.jpegData(compressionQuality: 0.8) else {
            throw PhotoRekognitionError.imageProcessingFailed
        }
        
        let rekognitionClient = RekognitionClient(
        credentialsProvider: credentialsProvider,
        region: REGION
    )
        let detectModerationLabelsRequest = DetectModerationLabelsInput(
            image: RekognitionClientTypes.Image(bytes: imageData),
            minConfidence: 90
        )
        
        let response = try await rekognitionClient.detectModerationLabels(input: detectModerationLabelsRequest)
        
        if let labels = response.moderationLabels, !labels.isEmpty {
           return false
        } else {
            return true
        }
    }

Thank you!

Links

Sources/Services/AWSRekognition/Sources/AWSRekognition/RekognitionClient.swift

@oanav21 oanav21 added documentation This is a problem with documentation. needs-triage This issue or PR still needs to be triaged. labels Nov 6, 2024
@sichanyoo
Copy link
Contributor

Hello! Thank you for submitting the ticket.

The API for providing crednetials provider to the client has changed.
The parameter name is now awsCredentialIdentityResolver instead of credentialsProvider, as you can see here.

Please let me know if that helped, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation This is a problem with documentation. needs-triage This issue or PR still needs to be triaged.
Projects
None yet
Development

No branches or pull requests

2 participants