如何解决AWS ALB 未解码二进制响应
我在负载均衡器后面有一个 lambda。我正在尝试从 lambda 返回一个 zip 文件(二进制内容)。 由于响应类型是 application/octet-stream,因此响应是由代码编码的 base 64,并且 isbase64encoded 标志设置为 true。
期望是当此标志打开时,ALB 将在将响应发送到浏览器之前对其进行解码,但事实并非如此。
请注意,我使用 AWS 无服务器 Java 容器库 (https://github.com/awslabs/aws-serverless-java-container/tree/master/aws-serverless-java-container-springboot2) 在 ALB 后面的 lambda 中运行网络服务
对于 Lambda 日志、请求、响应标头和代码,请参见下文
Lambda 日志:-
[2021-05-27/13:43:51.805/UTC][INFO ][main][c.a.s.p.i.LambdaContainerHandler] ; 127.0.0.1 null- null [27/05/2021:13:43:51Z] "GET /downloadZip null" 200 403 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/90.0.4430.212 Safari/537.36" combined
[2021-05-27/13:43:51.805/UTC][INFO ][main][c.a.s.p.i.LambdaContainerHandler] ; 127.0.0.1 null- null [27/05/2021:13:43:51Z] "GET /downloadZip null" 200 403 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/90.0.4430.212 Safari/537.36" combined
响应头:-
content-disposition: attachment;filename=download.zip
content-length: 740
content-type: application/octet-stream
date: Thu,27 May 2021 13:43:51 GMT
server: awselb/2.0
请求头(浏览器):-
:authority: ddvs-ci-binary-lambda.ihsmvals-dev.com
:method: GET
:path: /downloadZip
:scheme: https
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
accept-encoding: gzip,deflate,br
accept-language: en-US,en;q=0.9
sec-ch-ua: " Not A;Brand";v="99","Chromium";v="90","Google Chrome";v="90"
sec-ch-ua-mobile: ?0
sec-fetch-dest: document
sec-fetch-mode: navigate
sec-fetch-site: none
sec-fetch-user: ?1
upgrade-insecure-requests: 1
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/90.0.4430.212 Safari/537.36
代码:-
@GetMapping("/downloadZip")
public void downloadFile(HttpServletResponse response) {
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition","attachment;filename=download.zip");
response.setStatus(HttpServletResponse.SC_OK);
try (ZipOutputStream zippedOut = new ZipOutputStream(response.getoutputStream())) {
Resource resource = new ClassPathResource("logback.xml");
//FileSystemResource resource = new FileSystemResource(file);
ZipEntry e = new ZipEntry(resource.getFilename());
// Configure the zip entry,the properties of the file
e.setSize(resource.contentLength());
e.setTime(System.currentTimeMillis());
// etc.
zippedOut.putNextEntry(e);
// And the content of the resource:
StreamUtils.copy(resource.getInputStream(),zippedOut);
zippedOut.closeEntry();
zippedOut.finish();
} catch (Exception e) {
// Exception handling goes here
}
}
这是对二进制响应类型的响应进行编码的位
if (!this.isBinary(containerResponse.getContentType()) && this.isValidUtf8(containerResponse.getAwsResponseBodyBytes())) { responseString = containerResponse.getAwsResponseBodyString(); } 别的 { responseString = Base64.getEncoder().encodetoString(containerResponse.getAwsResponseBodyBytes()); awsProxyResponse.setBase64Encoded(true); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。