[英]AWS Lambda GO and image response broken
currently I'm trying to proxy images through a go lambda function from S3 backend.目前我正在尝试通过来自 S3 后端的 go lambda function 代理图像。 basically i retrieve the image as []byte from S3 and try to pass it to the browser as image/png
基本上我从 S3 检索图像作为 []byte 并尝试将它作为图像/png 传递给浏览器
imgData := repository.GetBinary(cType, cId, uuid)
b64img := base64.StdEncoding.EncodeToString(imgData)
return events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
Body: b64img,
IsBase64Encoded: true,
Headers: map[string]string{
"Content-Type": "image/png",
},
}, nil
I tried it as base64 encoded as well as not.我尝试将其编码为 base64,也可以不编码。 The image won't be rendered.
图像不会被渲染。
When i do it without lambda as go server it works fine.当我在没有 lambda 作为 go 服务器的情况下执行此操作时,它工作正常。
lib.Init()
// disable validation here
lib.App.ValidateHeader = false
b := repository.GetBinary(cType, cId, uuid)
w.Header().Set("Content-Type", "text/plain")
img := bytes.NewReader(b)
w.Header().Set("Content-Type", "image/png")
w.WriteHeader(http.StatusOK)
io.Copy(w,img)
I don't know what I'm doing wrong...我不知道我做错了什么......
I got it working after adding the Content-Length
header:添加
Content-Length
header 后我开始工作了:
buf := new(bytes.Buffer)
if err := png.Encode(buf, image); err != nil {
return errorResponse(err)
}
b64img := base64.StdEncoding.EncodeToString(buf.Bytes())
return &events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
Headers: map[string]string{
"Content-Type": "image/png",
"Content-Length": fmt.Sprintf("%d", len(b64img)),
},
Body: b64img,
IsBase64Encoded: true,
}, nil
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.