AWS API Gateway base64Decode 产生乱码二进制? [英] AWS API Gateway base64Decode produces garbled binary?
问题描述
我正在尝试从 AWS API Gateway 方法返回一个 1px 的 gif.
由于现在支持二进制数据,我使用以下集成响应"映射返回图像/gif:
$util.base64Decode("R0lGODlhAQABAIAAAAAAAP//yH5BAEAAAAALAAAAAABAAEAAAIBRAA7")
但是,当我在 Chrome 中查看此内容时,我看到返回以下二进制文件:
代替:
谁能帮我理解为什么这是乱码和错误的长度?或者我可以做些什么来返回正确的二进制文件?有没有其他什么我可以在不使用 base64Decode 函数的情况下返回这个 1px gif 的?
非常感谢,这让我很痛苦!
编辑
这个人变得陌生了.看起来问题不在于 base64Decode,而在于二进制的一般处理.我在
我更新了集成响应以包含一个硬编码的图像/png 标头:
最后一步很棘手:将 contentHandling
属性设置为CONVERT_TO_BINARY".我无法弄清楚如何在 AWS 控制台中执行操作.我必须使用 CLI API 来完成此操作:
aws apigateway 更新集成响应 --profile davemaple --rest-api-id chtskiuzxx --resource-id ki1lxx --http-method GET --状态码 200 --patch-operations '[{"op" : "replace", "path" : "/contentHandling", "value" : "CONVERT_TO_BINARY"}]'
我希望这会有所帮助.
I'm trying to return a 1px gif from an AWS API Gateway method.
Since binary data is now supported, I return an image/gif using the following 'Integration Response' mapping:
$util.base64Decode("R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7")
However, when I look at this in Chrome, I see the following binary being returned:
Instead of:
Could anyone help me understand why this is garbled and the wrong length? Or what I could do to return the correct binary? Is there some other what I could always return this 1px gif without using the base64Decode function?
Many thanks in advance, this has being causing me a lot of pain!
EDIT
This one gets stranger. It looks like the issue is not with base64Decode, but with the general handling of binary. I added a Lambda backend (previously I was using Firehose) following this blog post and this Stack Overflow question. I set images as binaryMediaType as per this documentation page.
This has let me pass the following image/bmp pixel from Lambda through the Gateway API, and it works correctly:
exports.handler = function(event, context) {
var imageHex = "x42x4dx3cx00x00x00x00x00x00x00x36x00x00x00x28x00x00x00x01x00x00x00x01x00x00x00x01x00x18x00x00x00x00x00x06x00x00x00x27x00x00x00x27x00x00x00x00x00x00x00x00x00x00x00xffxffxffxffx00x00";
context.done(null, { "body":imageHex });
};
However the following images representing an image/png or a image/gif get garbled when passed through:
exports.handler = function(event, context) {
//var imageHex = "x47x49x46x38x39x61x01x00x01x00x80x00x00x00x00x00xffxffxffx21xf9x04x01x00x00x00x00x2cx00x00x00x00x01x00x01x00x00x02x01x44x00x3b";
//var imageHex = "x47x49x46x38x39x61x01x00x01x00x80x00x00xffxffxffx00x00x00x21xf9x04x01x00x00x00x00x2cx00x00x00x00x01x00x01x00x00x02x02x44x01x00x3b";
var imageHex = "x47x49x46x38x39x61x01x00x01x00x80x00x00x00x00x00x00x00x00x21xf9x04x01x00x00x00x00x2cx00x00x00x00x01x00x01x00x00x02x02x44x01x00x3bx0a"
context.done(null, { "body":imageHex });
};
This seems to be the same issue as another Stack Overflow question, but I was hoping this would be fixed with the Gateway API binary support. Unfortunately image/bmp doesn't work for my use case as it can't be transparent...
In case it helps anyone, this has been a good tool for converting between base64 and hex.
It looks like this was a known issue previously: https://forums.aws.amazon.com/thread.jspa?messageID=668306򣊒
But it should be possible now that they've added support for binary data: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings.html
It looks like this is the bit we need: "Set the contentHandling property of the IntegrationResponse resource to CONVERT_TO_BINARY to have the response payload converted from a Base64-encoded string to its binary blob". Then we shouldn't need the base64Decode()
function.
Working on a test now to see if this works.
EDIT: I was finally able to get this working. You can see the binary image here: https://chtskiuz10.execute-api.us-east-1.amazonaws.com/prod/rest/image
Here's my Lambda function that returns the base64 encoded PNG as a string: https://gist.github.com/davemaple/73ce3c2c69d5310331395a0210069263
I updated the method response as follows:
I updated the integration response to include a hard-coded image/png header:
The last step was tricky: setting the contentHandling
property to "CONVERT_TO_BINARY". I couldn't figure out how to do in the AWS console. I had to use the CLI API to accomplish this:
aws apigateway update-integration-response
--profile davemaple
--rest-api-id chtskiuzxx
--resource-id ki1lxx
--http-method GET
--status-code 200
--patch-operations '[{"op" : "replace", "path" : "/contentHandling", "value" : "CONVERT_TO_BINARY"}]'
I hope this helps.
这篇关于AWS API Gateway base64Decode 产生乱码二进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!