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

如何使用AWS JavaScript SDK v3向Kinesis Data Firehose发送数据?

解决AWS SDK v3向Kinesis Data Firehose发送JSON数据的问题

AWS SDK v3与v2在处理Kinesis Data Firehose的Record.Data字段时存在差异,这是导致你遇到异常的核心原因:

  • SDK v2会自动将传入的字符串转换为Base64编码后发送
  • SDK v3要求Data必须是Uint8Array类型,直接传入字符串会触发错误编码,生成全A的无效Base64值

修正后的代码方案

浏览器/通用环境

import { FirehoseClient, PutRecordCommand } from "@aws-sdk/client-firehose";

const config = {
  region: "us-east-1"
};

const client = new FirehoseClient(config);
const payload = JSON.stringify({
  key: "test",
  value: "hello world"
});

const input = {
  DeliveryStreamName: "MY-STREAM-NAME",
  Record: {
    // 使用TextEncoder将字符串转为Uint8Array
    Data: new TextEncoder().encode(payload)
  },
};

const command = new PutRecordCommand(input);
const response = await client.send(command);
console.log(response);

Node.js环境

Node.js中可以用Buffer简化转换,Buffer.from()返回的实例兼容Uint8Array要求:

import { FirehoseClient, PutRecordCommand } from "@aws-sdk/client-firehose";

const config = {
  region: "us-east-1"
};

const client = new FirehoseClient(config);
const payload = JSON.stringify({
  key: "test",
  value: "hello world"
});

const input = {
  DeliveryStreamName: "MY-STREAM-NAME",
  Record: {
    Data: Buffer.from(payload)
  },
};

const command = new PutRecordCommand(input);
const response = await client.send(command);
console.log(response);

上述两种写法都会将JSON字符串正确转换为SDK v3要求的格式,发送后HTTP请求中的Data值会和SDK v2一致,正常交付到S3存储桶。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 17:52:10