我在负载均衡器后面有一个 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)在 lambda 中运行 Web 服务在 ALB 后面
对于 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(); } else { responseString = Base64.getEncoder().encodeToString(containerResponse.getAwsResponseBodyBytes()); awsProxyResponse.setBase64Encoded(true); }