如何將我從 S3 收到的檔案作為二進制回應發送到 Gin?
可以說,我有以下代碼從 S3 存盤桶獲取影像:
response, err := i.s3Client.GetObject(context.TODO(), &s3.GetObjectInput{
Bucket: aws.String("test"),
Key: aws.String(filename),
})
如何將此回應通過管道傳輸到 Gin-router 的回應背景關系中?
我可以做類似的事情:
body, err := ioutil.ReadAll(response.Body)
if err != nil {
ctx.JSON(http.StatusBadRequest, err)
return
}
但是如何告訴杜松子酒將其作為輸出?我想到但不起作用的是:
ctx.Header("Content-Type", *response.ContentType)
ctx.Header("Cache-control", "max-age=" strconv.Itoa(60*60*24*365))
ctx.Write(body)
有什么我可以在這里做的嗎?
uj5u.com熱心網友回復:
你幾乎完成:
而不是ctx.Write使用這個:
ctx.DataFromReader(200, response.ContentLength, *response.ContentType, response.Body, nil)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/456610.html
