doudiyu1639 2015-12-26 07:12
浏览 35
已采纳

安全地从S3提供内容

I'm writing a web application in go and I'm using amazon s3 to store all the user files, this include the profile pics and text and audio files.

However I'm struggling to understand what will be the correct way to do this securely. What I was thinking is after making sure the request comes from an authenticated user with access to the requested file, just use the aws go library to fetch the file and write it to the http request.

Something like this:

import (
    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/s3"
    "io"
)

func serveFile(file string, w http.ResponseWriter) {
    svc := s3.New(session.New(), aws.NewConfig().WithRegion(config.AWS.Region))

    params := &s3.GetObjectInput{
    Bucket: aws.String("Bucket name"),
    Key:    aws.String(file),
    }

    resp, err := svc.GetObject(params)
    n, err := io.Copy(w, resp.Body)

}

I don't have much experience working with AWS so I'm not sure this is even a good approach, but it works. Is this a good way to do it, for a production server? Is there a better, more reliable way?

  • 写回答

2条回答 默认 最新

  • dtn36013 2015-12-26 08:17
    关注

    This method can work. But it relies on requests being proxied through your client.

    The recommend way is to use signed urls. This creates a url that is valid for a time period that you specify that allows the end user to download a file directly from S3.

    Your key is not exposed as the url contains just a signature.

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

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决