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

如何在AWS SDK for JavaScript v3中配置自定义服务客户端?

在AWS SDK for JavaScript v3中配置自定义Personalize及PersonalizeRuntime客户端

AWS SDK v3采用模块化设计,和v2的apiLoader实现逻辑不同,我们需要通过加载本地API模型并注入到服务客户端配置中完成自定义,以下是具体实现步骤:

1. 安装依赖

先安装所需的SDK模块化包:

npm install @aws-sdk/client-personalize @aws-sdk/client-personalize-runtime @aws-sdk/credential-providers

2. 加载本地自定义API模型

假设你已拥有本地的API模型JSON文件(比如personalize-custom-model.json和personalize-runtime-custom-model.json),先读取这些文件:

const fs = require('fs');
const path = require('path');

// 读取Personalize自定义API模型
const personalizeCustomModel = JSON.parse(
  fs.readFileSync(path.resolve(__dirname, './personalize-custom-model.json'), 'utf8')
);

// 读取PersonalizeRuntime自定义API模型
const runtimeCustomModel = JSON.parse(
  fs.readFileSync(path.resolve(__dirname, './personalize-runtime-custom-model.json'), 'utf8')
);

3. 创建自定义服务客户端

Personalize客户端

const { PersonalizeClient } = require('@aws-sdk/client-personalize');
const { fromNodeProviderChain } = require('@aws-sdk/credential-providers');

async function getCustomPersonalizeClient() {
  // 自动加载凭证(和v2默认凭证链逻辑一致)
  const credentials = await fromNodeProviderChain()();

  return new PersonalizeClient({
    credentials,
    region: 'us-east-1', // 替换为你的AWS区域
    // 注入自定义API模型
    apiConfig: personalizeCustomModel,
    // 若需自定义端点,取消注释并替换
    // endpoint: 'https://your-custom-personalize-endpoint'
  });
}

PersonalizeRuntime客户端

const { PersonalizeRuntimeClient } = require('@aws-sdk/client-personalize-runtime');

async function getCustomPersonalizeRuntimeClient() {
  const credentials = await fromNodeProviderChain()();

  return new PersonalizeRuntimeClient({
    credentials,
    region: 'us-east-1',
    apiConfig: runtimeCustomModel,
    // endpoint: 'https://your-custom-runtime-endpoint'
  });
}

注意事项

  • 本地API模型的结构必须与AWS SDK v3官方API模型格式匹配,可参考node_modules/@aws-sdk/client-personalize/models/api-models/下的文件格式
  • fromNodeProviderChain会自动从环境变量、~/.aws/credentials文件、IAM角色等渠道获取凭证,和v2默认凭证加载逻辑一致
  • 如果需要指定自定义服务端点,直接在客户端配置中添加endpoint字段即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 10:22:36