Aws-lambda-dotnet: 在蓝图 ASP.NET Core Web App 中获得二进制支持

创建于 2018-03-08  ·  5评论  ·  资料来源: aws/aws-lambda-dotnet

我在使用 ASP.NET Core 2 集成获得二进制支持时遇到了一些问题。

所以我尝试了“ASP.NET Core Web App”蓝图,它实际上包含一个favicon.ico文件,发布时不显示。

我认为蓝图应该像在您自己的机器上运行一样开箱即用。 或者也许您应该删除 favicon.ico。

我在这里阅读了关于二进制响应内容的说明: https :

所以我LambdaEntryPoint.Init()下行添加到RegisterResponseContentEncodingForContentType("image/x-icon", ResponseContentEncoding.Base64);

我在控制台中添加了二进制媒体类型:
image

Lambda 日志正确显示响应是 base64 编码的:

START RequestId: 11a7e691-22d3-11e8-9b13-ef274abd9c03 Version: $LATEST
Incoming GET requests to /favicon.ico
ASP.NET Core Request PathBase: /Prod, Path: /favicon.ico
[Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting GET https://XXX.execute-api.eu-west-1.amazonaws.com/Prod/favicon.ico 
[Information] Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware: Sending file. Request path: '/favicon.ico'. Physical path: '/var/task/wwwroot/favicon.ico' 
[Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 0.5675ms 200 image/x-icon 
Response Base 64 Encoded: True
END RequestId: 11a7e691-22d3-11e8-9b13-ef274abd9c03

API网关日志:

Starting execution for request: 577e5bb3-22d4-11e8-b061-1775958b34a4
HTTP Method: GET, Resource Path: /favicon.ico
Successfully completed execution
Method completed with status: 200

我缺少什么部分?

我发现很多帖子都没有使用 lambda 代理集成进行二进制工作。 所以我怀疑有一个普遍的问题。

guidance

最有用的评论

我只想把这个留在这里,希望能帮其他人节省一个小时......

.png 文件请求中的默认“接受”标头是“图像/一个png”而不是“图像/png”! 我添加后一切正常:

        protected override void Init(IWebHostBuilder builder)
        {
            //NB: Serverless WebAPI needs to have special config to serve binary file types: 
            RegisterResponseContentEncodingForContentType("image/png", ResponseContentEncoding.Base64);
            RegisterResponseContentEncodingForContentType("image/jpeg", ResponseContentEncoding.Base64);
            RegisterResponseContentEncodingForContentType("image/gif", ResponseContentEncoding.Base64);
            RegisterResponseContentEncodingForContentType("image/apng", ResponseContentEncoding.Base64);
            RegisterResponseContentEncodingForContentType("image/webp", ResponseContentEncoding.Base64);
            RegisterResponseContentEncodingForContentType("image/x-icon", ResponseContentEncoding.Base64);

            builder
                .UseStartup<Startup>();
        }

编辑:Chrome 和 IE11 的接受标头似乎默认使用image/apng ,但 Firefox 发送*/*用于<img>标签! 您可以*/*到上面的列表中,它_似乎_可以工作,但是 YMMV。

所有5条评论

我在与 AWS Support 交谈时解决了这个问题。

解决方案:
更改“二进制媒体类型”后,您必须点击“部署 API”:

image

嘿兄弟,我按照您的步骤操作,但仍然无法获取图像/图标。

你做了比你说的更多的事情吗?

谢谢!

是的,实际上您需要在尝试在浏览器中显示图像时将 BinaryMediaTypes 设置为*/*

BinaryMediaType image/x-icon仅在客户端发送Accept标头时才有效。 下面是一种测试方法:

$ curl -sH "Accept: image/x-icon" https://XXX.execute-api.eu-west-1.amazonaws.com/Prod/favicon.ico -o /tmp/favicon.ico
$ file /tmp/favicon.ico
/tmp/favicon.ico: MS Windows icon resource - 3 icons, 16x16, 256-colors

忽略它 :)

我只想把这个留在这里,希望能帮其他人节省一个小时......

.png 文件请求中的默认“接受”标头是“图像/一个png”而不是“图像/png”! 我添加后一切正常:

        protected override void Init(IWebHostBuilder builder)
        {
            //NB: Serverless WebAPI needs to have special config to serve binary file types: 
            RegisterResponseContentEncodingForContentType("image/png", ResponseContentEncoding.Base64);
            RegisterResponseContentEncodingForContentType("image/jpeg", ResponseContentEncoding.Base64);
            RegisterResponseContentEncodingForContentType("image/gif", ResponseContentEncoding.Base64);
            RegisterResponseContentEncodingForContentType("image/apng", ResponseContentEncoding.Base64);
            RegisterResponseContentEncodingForContentType("image/webp", ResponseContentEncoding.Base64);
            RegisterResponseContentEncodingForContentType("image/x-icon", ResponseContentEncoding.Base64);

            builder
                .UseStartup<Startup>();
        }

编辑:Chrome 和 IE11 的接受标头似乎默认使用image/apng ,但 Firefox 发送*/*用于<img>标签! 您可以*/*到上面的列表中,它_似乎_可以工作,但是 YMMV。

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

matsola picture matsola  ·  4评论

ghost picture ghost  ·  3评论

scionwest picture scionwest  ·  4评论

ljacobsson picture ljacobsson  ·  7评论

martincostello picture martincostello  ·  4评论