我如何將從 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)有什么我可以在這里做的嗎?
1 回答

森林海
TA貢獻2011條經驗 獲得超2個贊
你幾乎已經完成:
而不是ctx.Write
使用這個:
ctx.DataFromReader(200, response.ContentLength, *response.ContentType, response.Body, nil)
- 1 回答
- 0 關注
- 175 瀏覽
添加回答
舉報
0/150
提交
取消