gpt4 book ai didi

amazon-web-services - 使用 golang 的 http.ResponseWriter 进行 AWS S3 大文件反向代理

转载 作者:IT王子 更新时间:2023-10-29 01:27:07 27 4
gpt4 key购买 nike

我有一个名为 Download 的请求处理程序,我想从 Amazon S3 访问一个大文件并将其推送到用户的浏览器。我的目标是:

  • 在授予用户访问文件之前记录一些请求信息
  • 不要将文件过多地缓冲到内存中。文件可能会变得太大。

这是我到目前为止探索的内容:

func Download(w http.ResponseWriter, r *http.Request) {

sess := session.New(&aws.Config{
Region: aws.String("eu-west-1"),
Endpoint: aws.String("s3-eu-west-1.amazonaws.com"),
S3ForcePathStyle: aws.Bool(true),
Credentials: cred,
})

downloader := s3manager.NewDownloader(sess)
// I can't write directly into the ResponseWriter. It doesn't implement WriteAt.
// Besides, it doesn't seem like the right thing to do.
_, err := downloader.Download(w, &s3.GetObjectInput{
Bucket: aws.String(BUCKET),
Key: aws.String(filename),
})
if err != nil {
log.Error(4, err.Error())
return
}

}

我想知道是否有更好的方法(鉴于我正在努力实现的目标)。

欢迎提出任何建议。提前谢谢你:-)

最佳答案

如果您确实想通过您的服务流式传输文件(而不是按照接受的答案中的建议直接下载)-

import (
...

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
)

func StreamDownloadHandler(w http.ResponseWriter, r *http.Request) {

sess, awsSessErr := session.NewSession(&aws.Config{
Region: aws.String("eu-west-1"),
Credentials: credentials.NewStaticCredentials("my-aws-id", "my-aws-secret", ""),
})
if awsSessErr != nil {
http.Error(w, fmt.Sprintf("Error creating aws session %s", awsSessErr.Error()), http.StatusInternalServerError)
return
}

result, err := s3.New(sess).GetObject(&s3.GetObjectInput{
Bucket: aws.String("my-bucket"),
Key: aws.String("my-file-id"),
})
if err != nil {
http.Error(w, fmt.Sprintf("Error getting file from s3 %s", err.Error()), http.StatusInternalServerError)
return
}

w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", "my-file.csv"))
w.Header().Set("Cache-Control", "no-store")

bytesWritten, copyErr := io.Copy(w, result.Body)
if copyErr != nil {
http.Error(w, fmt.Sprintf("Error copying file to the http response %s", copyErr.Error()), http.StatusInternalServerError)
return
}
log.Printf("Download of \"%s\" complete. Wrote %s bytes", "my-file.csv", strconv.FormatInt(bytesWritten, 10))
}

关于amazon-web-services - 使用 golang 的 http.ResponseWriter 进行 AWS S3 大文件反向代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35245649/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com