You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Rekognition Node.js代码Region配置缺失问题排查

解决AWS Rekognition人脸对比代码中"missing region on config"错误

你遇到的这个错误核心原因是:代码里虽然创建了包含区域信息的config对象,但没有把它传递给Rekognition客户端实例,导致客户端无法读取区域配置,从而抛出"missing region on config"错误。

两种修复方法:

方法1:创建客户端时直接传入config

修改创建Rekognition客户端的代码,把config作为参数传入:

const client = new AWS.Rekognition(config);

方法2:设置为AWS全局配置

如果你的项目里多个AWS服务客户端都需要用这套配置,可以把config设置为AWS的全局配置,这样后续所有客户端都会自动使用:

const config = new AWS.Config({
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
  region: process.env.AWS_REGION
})
// 新增这行,设置全局配置
AWS.config = config;
const client = new AWS.Rekognition();

额外检查点

  • 确认环境变量AWS_REGION已经正确设置,值必须是AWS有效的区域标识符(比如us-east-1、ap-southeast-1),不能为空字符串
  • 如果你的代码是在AWS服务(比如EC2、ECS、Lambda)上运行,建议使用IAM角色而非硬编码密钥,AWS SDK会自动从环境中获取角色凭证,此时可以简化配置,只保留region部分甚至依赖服务的默认区域

修改后的完整代码示例(方法1)

//Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//PDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-rekognition-developer-guide/blob/master/LICENSE-SAMPLECODE.)

const AWS = require('aws-sdk')
const bucket        = 'bucket' // the bucketname without s3://
const photo_source  = 'source.jpg'
const photo_target  = 'target.jpg'
const config = new AWS.Config({
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
  region: process.env.AWS_REGION
})
// 这里传入config
const client = new AWS.Rekognition(config);
const params = {
  SourceImage: {
    S3Object: {
      Bucket: bucket,
      Name: photo_source
    },
  },
  TargetImage: {
    S3Object: {
      Bucket: bucket,
      Name: photo_target
    },
  },
  SimilarityThreshold: 70
}
client.compareFaces(params, function(err, response) {
  if (err) {
    console.log(err, err.stack); // an error occurred
  } else {
    response.FaceMatches.forEach(data => {
      let position   = data.Face.BoundingBox
      let similarity = data.Similarity
      console.log(`The face at: ${position.Left}, ${position.Top} matches with ${similarity} % confidence`)
    }) // for response.faceDetails
  } // if
});

内容的提问来源于stack exchange,提问作者John George

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 05:54:10