[英]Go: How to send a File from AWS S3 via GIN-Router as binary stream to the browser?
How can I send a file, that I've got received from S3, to Gin as binary response?我如何将从 S3 收到的文件作为二进制响应发送到 Gin?
Lets say, I have the following code to obtain an image from S3 bucket:比方说,我有以下代码从 S3 存储桶中获取图像:
response, err := i.s3Client.GetObject(context.TODO(), &s3.GetObjectInput{
Bucket: aws.String("test"),
Key: aws.String(filename),
})
How can I pipe this response into the response context of Gin-router?我如何将 pipe 这个响应放入 Gin-router 的响应上下文中?
I could do something like:我可以做类似的事情:
body, err := ioutil.ReadAll(response.Body)
if err != nil {
ctx.JSON(http.StatusBadRequest, err)
return
}
But how to tell gin to serve this as output?但是如何告诉 gin 将其作为 output 提供呢? What comes in my mind but won't work is:我想到但行不通的是:
ctx.Header("Content-Type", *response.ContentType)
ctx.Header("Cache-control", "max-age="+strconv.Itoa(60*60*24*365))
ctx.Write(body)
Anything I can do here?有什么我可以在这里做的吗?
You're almost done:你几乎已经完成:
Instead of ctx.Write
use this one:而不是ctx.Write
使用这个:
ctx.DataFromReader(200, response.ContentLength, *response.ContentType, response.Body, nil)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.