Home > Backend Development > Golang > How to download an object from AWS S3 into memory and send it via request in Go?

How to download an object from AWS S3 into memory and send it via request in Go?

WBOY
Release: 2024-02-08 20:51:09
forward
868 people have browsed it

如何从AWS S3下载对象到内存中并通过Go中的请求发送它?

Question content

Can someone help me resolve this error I'm getting when trying to download a file from s3?

So I want to create a storage service api via go gin, and I want a route that uses an s3 object key to download an object and then send this object back to the client.

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

// create an amazon s3 service client
s3client := s3.newfromconfig(cfg)
downloader := manager.newdownloader(s3client)
Copy after login
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())

    })
Copy after login

The above code gives me this error: Operation error s3: getobject, https response error statuscode: 403, requestid: 8khyw3rcs2za7d95, hostid: kolo gy/dtzrrovswsb1bxinln8w xdl0lnmysuwjnhupmbvk4itdfp mq2xuo8ehasnx/fai4i=, api error accessdenied: Access denied phpcnendc phpcn</p> <p>The request body is like this: </p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">{ &quot;filekey&quot;: &quot;https://xxx-xxx.s3.ap-southeast-1.amazonaws.com/agencies/photo/06%3A03%3A2023_17%3A42%3A42_gggi_db_er.png&quot; }</pre><div class="contentsignin">Copy after login</div></div> <p>I have all these aws_s3_bucket_name, aws_region, aws_access_key_id, aws_secret_access_key stored in .env files, and I think I have them set correctly because I have another route that uploads files to s3 (which works great). </p><br/><h2 class="daan">Correct answer</h2><br/><p>Your filekey is this url:</p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">&quot;https://xxx-xxx.s3.ap-southeast-1.amazonaws.com/agencies/photo/06%3A03%3A2023_17%3A42%3A42_gggi_db_er.png&quot;</pre><div class="contentsignin">Copy after login</div></div> <p>This can only be used from an http client, such as a web browser or curl. </p> <p>Specifically, it is not a valid s3 object key. When using getobject or similar s3 api it needs to be a valid key such as <code>agcies/photo/xyz.png and should not be url encoded.

The above is the detailed content of How to download an object from AWS S3 into memory and send it via request in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template