aws apigateway import-rest-api returns "Invalid base64" 错误
aws apigateway import-rest-api returns "Invalid base64" error
我正在尝试导出而不是导入 AWS 网关 API,按照中的说明
https://aws.amazon.com/premiumsupport/knowledge-center/api-gateway-migrate-accounts-regions/.
正在导出:
aws apigateway get-export --parameters extensions='apigateway' --rest-api-id MY_REST_API_ID --stage-name Prod --export-type swagger my-api-apigateway.json
我的 API 的相同文件是用 --parameters extensions='apigateway'
生成的
和 --parameters extensions='integrations'
.
但是当我尝试从导出的文件导入时:
aws apigateway import-rest-api --fail-on-warnings --body file://%cd%/my-api-gateway.json
,我总是收到 "Invalid base64: " 错误。像这样:
Invalid base64: "{
"swagger" : "2.0",
"info" : {
"version" : "1.0",
"title" : "my-stack-name"
},
"host" : "MY_REST_API_ID.execute-api.eu-central-1.amazonaws.com",
"basePath" : "/Prod",
...
google 上没有文档和示例说明正文应该是 Base64。
当我通过 UI(操作 -> 导入 API)导入时,同样的 JSON 似乎也有效。
我也尝试过使用 --cli-input-json
:
my-api-apigateway-cli-json.json
文件(根据 aws apigateway import-rest-api --generate-cli-skeleton
):
{
"failOnWarnings": true,
"parameters": {
"endpointConfigurationTypes": "REGIONAL"
},
"body": {... JSON FROM EXPORT ...}
}
导入命令:
aws apigateway import-rest-api --cli-input-json file://./my-api-apigateway-cli-json.json
,但它说
Parameter validation failed:
Invalid type for parameter body, value: {'swagger': '2.0', ...
所以,问题是:
- 我们应该将 json 编码为 base64 吗?
- 为什么没有记录这个错误和行为(或者如果是,那么在哪里?)?
- 如何从 cli 成功执行导入?
有用的链接
None 的链接说正文响应应该是 Base64
- https://docs.aws.amazon.com/apigateway/api-reference/link-relation/restapi-import/ -- 这里
body
明确记录为 // raw byte array representing the api definition
使用 AWS CLI 将 rest-api 导入 AWS API 网关时,不强制将 json 编码为 base64。
我怀疑您正在使用 AWS CLI v2 并且您面临的这个问题,我认为这是 AWS CLI version 2 中引入的更改的结果。 即
AWS CLI version 2 now passes all binary input and binary output
parameters as base64-encoded strings by default
解析:
您需要添加 --cli-binary-format raw-in-base64-out 以便它告诉 AWS CLI v2恢复到 AWS CLI v1 行为:
aws apigateway import-rest-api --cli-binary-format raw-in-base64-out --body file://my-api-apigateway.json
使用 fileb://
而不是 file://
对我有用。例如,
aws apigateway import-rest-api --body fileb://my-api.json
我正在尝试导出而不是导入 AWS 网关 API,按照中的说明 https://aws.amazon.com/premiumsupport/knowledge-center/api-gateway-migrate-accounts-regions/.
正在导出:
aws apigateway get-export --parameters extensions='apigateway' --rest-api-id MY_REST_API_ID --stage-name Prod --export-type swagger my-api-apigateway.json
我的 API 的相同文件是用 --parameters extensions='apigateway'
生成的
和 --parameters extensions='integrations'
.
但是当我尝试从导出的文件导入时:
aws apigateway import-rest-api --fail-on-warnings --body file://%cd%/my-api-gateway.json
,我总是收到 "Invalid base64: " 错误。像这样:
Invalid base64: "{
"swagger" : "2.0",
"info" : {
"version" : "1.0",
"title" : "my-stack-name"
},
"host" : "MY_REST_API_ID.execute-api.eu-central-1.amazonaws.com",
"basePath" : "/Prod",
...
google 上没有文档和示例说明正文应该是 Base64。
当我通过 UI(操作 -> 导入 API)导入时,同样的 JSON 似乎也有效。
我也尝试过使用 --cli-input-json
:
my-api-apigateway-cli-json.json
文件(根据 aws apigateway import-rest-api --generate-cli-skeleton
):
{
"failOnWarnings": true,
"parameters": {
"endpointConfigurationTypes": "REGIONAL"
},
"body": {... JSON FROM EXPORT ...}
}
导入命令:
aws apigateway import-rest-api --cli-input-json file://./my-api-apigateway-cli-json.json
,但它说
Parameter validation failed:
Invalid type for parameter body, value: {'swagger': '2.0', ...
所以,问题是:
- 我们应该将 json 编码为 base64 吗?
- 为什么没有记录这个错误和行为(或者如果是,那么在哪里?)?
- 如何从 cli 成功执行导入?
有用的链接
None 的链接说正文响应应该是 Base64
- https://docs.aws.amazon.com/apigateway/api-reference/link-relation/restapi-import/ -- 这里
body
明确记录为// raw byte array representing the api definition
使用 AWS CLI 将 rest-api 导入 AWS API 网关时,不强制将 json 编码为 base64。
我怀疑您正在使用 AWS CLI v2 并且您面临的这个问题,我认为这是 AWS CLI version 2 中引入的更改的结果。 即
AWS CLI version 2 now passes all binary input and binary output parameters as base64-encoded strings by default
解析:
您需要添加 --cli-binary-format raw-in-base64-out 以便它告诉 AWS CLI v2恢复到 AWS CLI v1 行为:
aws apigateway import-rest-api --cli-binary-format raw-in-base64-out --body file://my-api-apigateway.json
使用 fileb://
而不是 file://
对我有用。例如,
aws apigateway import-rest-api --body fileb://my-api.json