duanji1056 2016-02-06 19:48
浏览 110
已采纳

使用golang的http.ResponseWriter的AWS S3大文件反向代理

I have a request handler named Download which I want to access a large file from Amazon S3 and push it to the user's browser. My goals are:

  • To record some request information before granting the user access to the file
  • To not buffer the file into memory too much. Files may become too large.

Here is what I've explored so far:

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
    }

}

I'm wondering if there isn't a better approach (given the goals I'm trying to achieve).

Any suggestions are welcome. Thank you in advance :-)

  • 写回答

2条回答 默认 最新

  • doumi1884 2016-02-07 00:54
    关注

    If the file is potentially large, you don't want it to go trough your own server. The best approach (in my opinion) is to have the user download it directly from S3.

    You can do this by generating a presigned url:

    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,
        })
    
        s3svc := s3.New(sess)
        req, _ := s3svc.GetObjectRequest(&s3.GetObjectInput{
            Bucket: aws.String(BUCKET),
            Key: aws.String(filename),
        })
    
        url, err := req.Presign(5 * time.Minute)
        if err != nil {
            //handle error
        }
    
        http.Redirect(w, r, url, http.StatusTemporaryRedirect)
    }
    

    The presigned url is only valid for a limited time (5 minutes in this example, adjust to your needs) and takes the user directly to S3. No need to worry about downloads anymore!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化