我有一个使用 Next/React 构建的前端应用程序。 我正在尝试通过 v3 Javascript AWS SDK 使用 AWS Rekognition 创建图像审核功能。
我配置了一个 AWS Cognito 身份池,它挂钩到用户池。 用户登录后,使用 Rekognition 的界面即可使用。
我已经检查了 v3 文档,但请参阅 冲突 代码示例,了解如何使用
fromCognitoIdentityPool()
实例化客户端凭据,这两者都会给我带来输入错误。
以下代码在传递给
client
时给我带来了 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;
出现错误:
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'.
实例化 Rekognition 客户端的正确方法是什么?
尝试从
fromCognitoIdentityPool
而不是 @aws-sdk/credential-providers
获取 @aws-sdk/credential-provider-cognito-identity
。不推荐后者。
我在新文档中找不到该建议,但在旧文档中找到了。 https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_credential_provider_cognito_identity.html
这是
credential-providers
版本的文档。 https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-credential-providers/Variable/fromCognitoIdentityPool/
如果您在本地配置了凭据,则可以完全省略
client
参数。但是,如果没有,credential-provider
版本只需要 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" },
}),
});