How to read the contents of a file in awsS3 with golang

Asked

Viewed 88 times

0

I’m trying to create an API in golang

This will return the read data from a file in AWS (S3)

For example, in c# I do this way to read the data:

GetObjectRequest request = new GetObjectRequest{
        BucketName = "<Bucket>",
        Key = "<key>.json"
    };

using (GetObjectResponse response = _s3Client.GetObject (request)){
    using (StreamReader reader = new StreamReader (response.ResponseStream){
        var json = reader.ReadToEnd ();
    }
}

That way I have the contents of the file

Suppose I have a file in Bucket "ABC" with Key "123/file.json"

and that this file contains:

{
    "glossary": {
        "title": "example glossary"
    }
}

When I run the method in the API I want it to return this json

1 answer

1

I ended up asking in the stack in English too, but my English is terrible so I did not pass a good interpretation of what I wanted to users there... But after 20 minutes talking I managed to get through what I wanted and they helped me find an answer:

"Buffer with golang"

according to the answer all I need to do is:

requestInput := s3.GetObjectInput{
    Bucket: aws.String(bucket),
    Key:    aws.String(key),
}

buf := aws.NewWriteAtBuffer([]byte{})
downloader.Download(buf, &requestInput)

fmt.Printf("Downloaded %v bytes", len(buf.Bytes()))

this way I have an open/downloaded file in memory and I can treat it

Browser other questions tagged

You are not signed in. Login or sign up in order to post.