跳转到内容
搜索文档

Amazon Bedrock

最后更新 查看 MarkdownAgent 设置

Amazon Bedrock 使你能够使用基础模型构建并扩展生成式 AI 应用。

端点

https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/aws-bedrock

前提条件

向 Amazon Bedrock 发送请求时,请确保具备以下条件:

  • AI Gateway Account ID
  • AI Gateway gateway 名称
  • 具有 Amazon Bedrock 权限的 AWS 凭证(accessKeyIdsecretAccessKeyregion
  • 要使用的 Amazon Bedrock 模型名称

URL 结构

向 Amazon Bedrock 发送请求时,将当前使用的 URL 中的 https://bedrock-runtime.us-east-1.amazonaws.com/ 替换为 https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/aws-bedrock/bedrock-runtime/us-east-1/,然后追加要使用的模型。

例如,要在 us-east-1 中调用 Anthropic Claude 模型:

https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/aws-bedrock/bedrock-runtime/us-east-1/model/us.anthropic.claude-haiku-4-5-20251001-v1:0/invoke

使用 Amazon Bedrock 进行身份验证

Amazon Bedrock 使用 AWS Signature Version 4 (SigV4) 对 API 请求进行身份验证。与 OpenAI 或 Anthropic 等使用简单 API 密钥的提供商不同,AWS 要求使用你的凭证对每个请求进行加密签名。

AI Gateway 会为你处理这一复杂性。当你使用 BYOK 存储 AWS 凭证时,gateway 会在将请求转发到 AWS 之前自动为每个请求签名。

身份验证方式对比

方式 cf-aig-authorization 标头 Authorization 标头 签名
BYOK(推荐) Bearer {CF_AIG_TOKEN} 不需要 Gateway 自动签名
客户端签名 Bearer {CF_AIG_TOKEN} 预签名的 AWS 标头 你使用 aws4fetch 或 AWS SDK 签名

选项 1:BYOK(推荐)

推荐的方式是使用 AI Gateway 的 Bring Your Own Keys (BYOK) 功能存储你的 AWS 凭证。这样可以保护凭证安全,并消除客户端请求签名的需要。

  1. 在 Cloudflare 仪表板中,转到 AI > AI Gateway > 你的 gateway > Provider Keys(提供商密钥)

  2. 选择 Add API Key(添加 API 密钥),并将提供商选为 Amazon Bedrock

  3. 以如下结构的 JSON 对象输入你的 AWS 凭证:

    {
     "accessKeyId": "AKIAIOSFODNN7EXAMPLE",
     "secretAccessKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
     "region": "us-east-1"
    }
  4. 选择 Save(保存)

如果你使用来自 AWS STS 的临时凭证(例如通过担任 IAM 角色获得),请包含 sessionToken 字段:

{
	"accessKeyId": "ASIAIOSFODNN7EXAMPLE",
	"secretAccessKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
	"region": "us-east-1",
	"sessionToken": "FwoGZXIvYXdzEBY..."
}

配置 BYOK 后,你只需在请求中包含 cf-aig-authorization 标头。AI Gateway 会自动处理 AWS SigV4 签名。

选项 2:客户端签名

如果你希望自行签名请求,可以使用 aws4fetch 库或任何 AWS SDK,在通过 AI Gateway 发送之前对请求进行签名。请参阅下方的 客户端签名示例

示例

使用 BYOK 的 cURL

在将 AWS 凭证 存储为提供商密钥 后,请求会变得很简单——无需 AWS 签名:

curl "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/aws-bedrock/bedrock-runtime/us-east-1/model/us.anthropic.claude-haiku-4-5-20251001-v1:0/invoke" \
  -H "cf-aig-authorization: Bearer {CF_AIG_TOKEN}" \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [
      {
        "role": "user",
        "content": "What is Cloudflare?"
      }
    ],
    "max_tokens": 256,
    "anthropic_version": "bedrock-2023-05-31"
  }'

使用 aws4fetch 进行客户端签名

如果未使用 BYOK,则必须在通过 AI Gateway 发送之前对请求进行签名。以下示例在 Cloudflare Worker 中使用 aws4fetch 库:

import { AwsClient } from "aws4fetch";

interface Env {
	accessKey: string;
	secretAccessKey: string;
}

export default {
	async fetch(
		request: Request,
		env: Env,
		ctx: ExecutionContext,
	): Promise<Response> {
		const cfAccountId = "{account_id}";
		const gatewayName = "{gateway_id}";
		const region = "us-east-1";

		const awsClient = new AwsClient({
			accessKeyId: env.accessKey,
			secretAccessKey: env.secretAccessKey,
			region: region,
			service: "bedrock",
		});

		const body = JSON.stringify({
			messages: [{ role: "user", content: "What does ethereal mean?" }],
			max_tokens: 256,
			anthropic_version: "bedrock-2023-05-31",
		});

		// Sign against the original AWS URL
		const awsUrl = `https://bedrock-runtime.${region}.amazonaws.com/model/us.anthropic.claude-haiku-4-5-20251001-v1:0/invoke`;

		const presignedRequest = await awsClient.sign(awsUrl, {
			method: "POST",
			headers: { "Content-Type": "application/json" },
			body: body,
		});

		// Send through AI Gateway
		const gatewayUrl = `https://gateway.ai.cloudflare.com/v1/${cfAccountId}/${gatewayName}/aws-bedrock/bedrock-runtime/${region}/model/us.anthropic.claude-haiku-4-5-20251001-v1:0/invoke`;

		const response = await fetch(gatewayUrl, {
			method: "POST",
			headers: presignedRequest.headers,
			body: body,
		});

		if (
			response.ok &&
			response.headers.get("content-type")?.includes("application/json")
		) {
			const data = await response.json();
			return new Response(JSON.stringify(data));
		}

		return new Response("Invalid response", { status: 500 });
	},
};

使用 Unified API(兼容 OpenAI)

AI Gateway 提供 Unified API,可让你使用 OpenAI chat completions 格式访问 Bedrock 模型。目前支持 Anthropic ClaudeAmazon Nova 模型系列。你可以使用 OpenAI SDK 访问运行在 Bedrock 上的这些模型,而无需更改请求格式。

端点

https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions

cURL

在将 AWS 凭证 存储为提供商密钥 后,使用 aws-bedrock/{model} 格式指定模型:

curl "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions" \
  -H "cf-aig-authorization: Bearer {CF_AIG_TOKEN}" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "aws-bedrock/us.anthropic.claude-haiku-4-5-20251001-v1:0",
    "messages": [
      {
        "role": "user",
        "content": "What is Cloudflare?"
      }
    ]
  }'

OpenAI SDK

import OpenAI from "openai";

const client = new OpenAI({
	apiKey: "{CF_AIG_TOKEN}",
	baseURL:
		"https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat",
});

const response = await client.chat.completions.create({
	model: "aws-bedrock/us.anthropic.claude-haiku-4-5-20251001-v1:0",
	messages: [
		{
			role: "user",
			content: "What is Cloudflare?",
		},
	],
});

console.log(response.choices[0].message.content);

这篇文档对您有帮助吗?