AWS API Gateway base64Decode产生乱码的二进制文件? [英] AWS API Gateway base64Decode produces garbled binary?

查看:152
本文介绍了AWS API Gateway base64Decode产生乱码的二进制文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从AWS API Gateway方法返回1px gif.

I'm trying to return a 1px gif from an AWS API Gateway method.

由于现在支持二进制数据,因此我使用以下集成响应"映射返回图像/gif:

Since binary data is now supported, I return an image/gif using the following 'Integration Response' mapping:

$util.base64Decode("R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7")

但是,当我在Chrome浏览器中查看此代码时,我看到返回了以下二进制文件:

However, when I look at this in Chrome, I see the following binary being returned:

代替:

有人可以帮助我理解为什么这是乱码且长度错误吗?或者我该怎么做才能返回正确的二进制文件?在不使用base64Decode函数的情况下,我还能始终返回此1px gif吗?

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!

编辑

这个人变得陌生.看来问题不在于base64Decode,而在于二进制的一般处理.我在此博客文章堆栈溢出问题 .我根据此文档页面.

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.

这使我可以通过网关API从Lambda传递以下图像/bmp像素,并且可以正常工作:

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 = "\x42\x4d\x3c\x00\x00\x00\x00\x00\x00\x00\x36\x00\x00\x00\x28\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x18\x00\x00\x00\x00\x00\x06\x00\x00\x00\x27\x00\x00\x00\x27\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\x00\x00";
  context.done(null, { "body":imageHex });

};

但是,以下表示image/png或image/gif的图像在通过时会出现乱码:

However the following images representing an image/png or a image/gif get garbled when passed through:

exports.handler = function(event, context) {

//var imageHex = "\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\xff\xff\xff\x21\xf9\x04\x01\x00\x00\x00\x00\x2c\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x01\x44\x00\x3b";
//var imageHex = "\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x00\x00\xff\xff\xff\x00\x00\x00\x21\xf9\x04\x01\x00\x00\x00\x00\x2c\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x44\x01\x00\x3b";
  var imageHex = "\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x21\xf9\x04\x01\x00\x00\x00\x00\x2c\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x44\x01\x00\x3b\x0a"
  context.done(null, { "body":imageHex });

};

这似乎与另一个堆栈溢出问题,但我希望通过Gateway API二进制支持可以解决此问题.不幸的是,image/bmp不适用于我的用例,因为它不能透明...

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...

万一它对任何人都有帮助,这是在base64和hex之间进行转换的好工具. >

In case it helps anyone, this has been a good tool for converting between base64 and hex.

推荐答案

以前看来这是一个已知问题: https://forums.aws.amazon.com/thread.jspa?messageID=668306&# 668306

It looks like this was a known issue previously: https://forums.aws.amazon.com/thread.jspa?messageID=668306&#668306

但是现在他们已经添加了对二进制数据的支持,这应该是可能的: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings.html

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

这似乎是我们需要的:将IntegrationResponse资源的contentHandling属性设置为 CONVERT_TO_BINARY ,以将响应有效载荷从Base64编码的字符串转换为其二进制blob".那么我们就不需要base64Decode()函数.

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.

编辑:我终于能够使它正常工作.您可以在此处查看二进制图像: https://chtskiuz10.execute-api.us-east-1.amazonaws. com/prod/rest/image

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

这是我的Lambda函数,该函数以字符串形式返回base64编码的PNG: https://gist.github.com/davemaple/73ce3c2c69d5310331395a0210069263

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:

我更新了集成响应,以包括一个硬编码的image/png标头:

I updated the integration response to include a hard-coded image/png header:

最后一步很棘手:将contentHandling属性设置为"CONVERT_TO_BINARY".我不知道如何在AWS控制台中进行操作.我必须使用CLI API来完成此任务:

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"}]'

我希望这会有所帮助.

这篇关于AWS API Gateway base64Decode产生乱码的二进制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆