当前位置: > > > > 如何从AWS S3下载对象到内存中并通过Go中的请求发送它?
来源:stackoverflow
2024-04-25 14:27:33
0浏览
收藏
编程并不是一个机械性的工作,而是需要有思考,有创新的工作,语法是固定的,但解决问题的思路则是依靠人的思维,这就需要我们坚持学习和更新自己的知识。今天就整理分享《如何从AWS S3下载对象到内存中并通过Go中的请求发送它?》,文章讲解的知识点主要包括,如果你对Golang方面的知识点感兴趣,就不要错过,在这可以对大家的知识积累有所帮助,助力开发能力的提升。
问题内容
有人可以帮我解决尝试从 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 编码。
文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《如何从AWS S3下载对象到内存中并通过Go中的请求发送它?》文章吧,也可关注公众号了解相关技术文章。