I have a front-end app built with Next/React. I'm attempting to create an image moderation feature using AWS Rekognition via the v3 Javascript AWS SDK.
I have an AWS Cognito Identity Pool configured which hooks into a User Pool. The interface using Rekognition becomes available once the user has signed in.
I've checked the v3 documentation, but see conflicting code examples for how to instantiate client credentials using fromCognitoIdentityPool()
, both of which give me typing errors.
The following code is giving me a type error for the client
when passed to fromCognitoIdentityPool()
:
import {
RekognitionClient,
DetectModerationLabelsCommand,
} from '@aws-sdk/client-rekognition';
import { fromCognitoIdentityPool } from '@aws-sdk/credential-providers';
import { CognitoIdentityClient } from '@aws-sdk/client-cognito-identity';
const AWS_REGION = process.env.NEXT_PUBLIC_AWS_REGION;
const AWS_IDENTITY_POOL_ID = process.env.NEXT_PUBLIC_COGNITO_IDENTITY_POOL_ID;
const moderateImage = async (imagePath: string) => {
const client = new RekognitionClient({
region: AWS_REGION,
credentials: fromCognitoIdentityPool({
identityPoolId: AWS_IDENTITY_POOL_ID!,
client: new CognitoIdentityClient({ region: AWS_REGION }),
}),
});
const base64Image = imagePath.split(';base64,').pop();
const imageBuffer = Buffer.from(base64Image as string, 'base64');
const params = {
Image: {
Bytes: imageBuffer,
},
MinConfidence: 70,
};
const command = new DetectModerationLabelsCommand(params);
const response = await client.send(command);
return response;
};
export default moderateImage;
With the error:
Argument of type '{ identityPoolId: string; client: CognitoIdentityClient; }' is not assignable to parameter of type 'FromCognitoIdentityPoolParameters'.
Object literal may only specify known properties, and 'client' does not exist in type 'FromCognitoIdentityPoolParameters'.ts(2345)
Argument of type '{ identityPoolId: string; client: CognitoIdentityClient; }' is not assignable to parameter of type 'FromCognitoIdentityPoolParameters'.
What's the correct way to instantiate the Rekognition client?
Try getting fromCognitoIdentityPool
from @aws-sdk/credential-providers
instead of @aws-sdk/credential-provider-cognito-identity
. The latter is not recommended.
I can't find that recommendation in the new docs, but it's in the old ones. https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_credential_provider_cognito_identity.html
Here's the docs on the credential-providers
version. https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-credential-providers/Variable/fromCognitoIdentityPool/
You can omit the client
parameter altogether if you have your credentials configured locally. But, if not, the credential-provider
version only takes a clientConfig
.
import { fromCognitoIdentityPool } from "@aws-sdk/credential-providers";
import { RekognitionClient } from "@aws-sdk/client-rekognition";
const rekClient = new RekognitionClient({
region: "us-east-1",
credentials: fromCognitoIdentityPool({
identityPoolId: "redacted",
clientConfig: { region: "us-east-1" },
}),
});