gpt4 book ai didi

aws-api-gateway - 如何在 AWS API Gateway 中传递 Content-Type?

转载 作者:行者123 更新时间:2023-12-04 07:00:54 25 4
gpt4 key购买 nike

我已经设置了 AWS API Gateway 来将请求传递给返回图像的服务。

The endpoint as it appears in the AWS UI

当我在 UI 中使用“测试”功能时,日志显示方法响应中返回的 PNG 数据以及`Content-Type=image/png:

The AWS log of the "Test" UI showing Content-Type being correct

但是,当您实际在浏览器中访问端点时,Content-Typeapplication/json .
我原以为“测试”UI 的日志中显示的“方法响应 header ”与实际返回的内容相匹配。

如何强制 API Gateway 将上游的 Content-Type(在本例中为 image/png,但其他更一般)返回到浏览器?

这是 Swagger 2.0 语法中定义的端点:

"/format/{id}/image.png": {
"get": {
"tags": [],
"summary": "",
"deprecated": true,
"operationId": "get-png",
"produces": [
"image/png"
],
"parameters": [
{
"name": "id",
"in": "path",
"description": "My Description",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "Successful operation",
"schema": {
"type": "file"
},
"headers": {
"Access-Control-Allow-Origin": {
"type": "string",
"description": "URI that may access the resource"
},
"Content-Type": {
"type": "string",
"description": "Response MIME type"
}
}
}
},
"x-amazon-apigateway-integration": {
"responses": {
"default": {
"statusCode": "200",
"responseParameters": {
"method.response.header.Access-Control-Allow-Origin": "'*'",
"method.response.header.Content-Type": "integration.response.header.Content-Type"
}
}
},
"requestParameters": {
"integration.request.path.id": "method.request.path.id"
},
"uri": "https://[image_service]/{id}.png",
"passthroughBehavior": "when_no_match",
"httpMethod": "GET",
"type": "http"
}
}
}

笔记:
  • 这个端点有些简化(但仍然说明了问题)。但实际上,端点还有更多内容(即,我不仅代理请求,还重写路径 + 查询参数)。
  • this answer 中所述,如果您的终端节点只是将请求代理到图像服务器,则您可能应该改用 AWS CloudFront。它的价格中包含边缘缓存,并且便宜约 3 倍。
  • 最佳答案

    事实证明,我错过了两件事:

    首先,我需要更改 AWS 将在“接受” header 中发送到上游的类型列表

    "x-amazon-apigateway-binary-media-types" : [
    "image/png"
    ]

    其次,我需要将集成响应设置为“转换为二进制(如果需要)”:
    "contentHandling": "CONVERT_TO_BINARY"

    这是修改后的配置:
    {
    "swagger": "2.0",
    "info": {
    "description": "My description",
    "title": "My Title",
    "version": "1.0.0"
    },
    "schemes": [
    "https",
    "http"
    ],
    "paths": {
    "/format/{id}/image.png": {
    "get": {
    "tags": [],
    "summary": "My Description",
    "deprecated": true,
    "operationId": "get-png",
    "produces": [
    "image/png"
    ],
    "parameters": [
    {
    "name": "id",
    "in": "path",
    "description": "",
    "required": true,
    "type": "string"
    }
    ],
    "responses": {
    "200": {
    "description": "Successful operation",
    "schema": {
    "type": "file"
    },
    "headers": {
    "Access-Control-Allow-Origin": {
    "type": "string",
    "description": "URI that may access the resource"
    },
    "Content-Type": {
    "type": "string",
    "description": "Response MIME type"
    }
    }
    }
    },
    "x-amazon-apigateway-integration": {
    "responses": {
    "default": {
    "statusCode": "200",
    "responseParameters": {
    "method.response.header.Content-Type": "integration.response.header.Content-Type",
    "method.response.header.Access-Control-Allow-Origin": "'*'"
    },
    "contentHandling": "CONVERT_TO_BINARY"
    }
    },
    "requestParameters": {
    "integration.request.path.id": "method.request.path.id"
    },
    "uri": "https://img.shields.io/pypi/format/{id}.png",
    "passthroughBehavior": "when_no_match",
    "httpMethod": "GET",
    "type": "http"
    }
    }
    }

    },
    "definitions": {},
    "x-amazon-apigateway-binary-media-types" : [
    "image/png"
    ]

    }

    关于aws-api-gateway - 如何在 AWS API Gateway 中传递 Content-Type?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49367551/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com