首页 > 文章列表 > Go语言中如何将AWS S3对象下载到内存并发送请求?

Go语言中如何将AWS S3对象下载到内存并发送请求?

193 2024-02-07
问题内容

有人可以帮我解决尝试从 s3 下载文件时遇到的这个错误吗?

所以我想通过 go gin 创建存储服务 api,并且我想要一个使用 s3 对象密钥下载对象然后将此对象发送回客户端的路线。

cfg,_ := config.loaddefaultconfig(context.todo())

// create an amazon s3 service client
s3client := s3.newfromconfig(cfg)
downloader := manager.newdownloader(s3client)
router.post("/download-s3", func(ctx *gin.context) {
        var data map[string]string

        if err := ctx.shouldbindjson(&data); err != nil {
            log.println("error: bind error")
            ctx.json(http.statusbadrequest, gin.h{"error": err.error()})
            return
        }

        log.println("body request data (filekey): ", data["filekey"])

        filekey := data["filekey"]

        /// buffer read
        buf := make([]byte, 100)
        // wrap with aws.writeatbuffer
        w := manager.newwriteatbuffer(buf)
        // download file into the memory
        numbytesdownloaded, err := downloader.download(ctx, w, &s3.getobjectinput{
            bucket: aws.string(bucketname),
            key:    aws.string(filekey),
        })
        if err != nil {
            log.println("error: download error")
            ctx.json(http.statusbadrequest, gin.h{"error": err.error()})
            return
        }

        ctx.json(http.statusaccepted,
            gin.h{
                "message":            fmt.sprintf("'%s' downloaded!", "test.jpg"),
                "numbytesdownloaded": numbytesdownloaded,
            },
        )

        ctx.data(http.statusok, "application/octet-stream", w.bytes())

    })

上面的代码给了我这个错误: 操作错误s3:getobject,https响应错误statuscode:403,requestid:8khyw3rcs2za7d95,hostid:kolo + gy / dtzrrovswsb1bxinln8w + xdl0lnmysuwjnhupmbvk4itdfp + mq2xuo8ehasnx / fai4i =,api错误accessdenied:访问被拒绝phpcnendc phpcn

请求正文是这样的:

{
    "filekey": "https://xxx-xxx.s3.ap-southeast-1.amazonaws.com/agencies/photo/06%3A03%3A2023_17%3A42%3A42_gggi_db_er.png"
}

我将所有这些 aws_s3_bucket_name、aws_region、aws_access_key_id、aws_secret_access_key 存储在 .env 文件中,而且我认为我将它们设置正确,因为我有另一条将文件上传到 s3 的路线(效果很好)。


正确答案


您的 filekey 是这样的 url:

"https://xxx-xxx.s3.ap-southeast-1.amazonaws.com/agencies/photo/06%3A03%3A2023_17%3A42%3A42_gggi_db_er.png"

这只能从 http 客户端使用,例如网络浏览器或curl。

具体来说,它不是有效的 s3 对象密钥。使用 getobject 或类似的 s3 api 时,它需要是有效的密钥,例如 agcies/photo/xyz.png 并且不应进行 url 编码。