Reading files

In AWS you often work with events, which are stored as JSON (J ava S cript O bject N otation) data. Reading and manipulation these data is a part of working with AWS.

We start reading a JSON “Lambda S3 Event” data stored in a file.

In the directory (get it from github) we have two files:

.
├── main.go
└── s3event.json
  1 package main
  2
  3 import (
  4         "fmt"
  5         "os"
  6 )
  7
  8 func main() {
  9         content, err := os.ReadFile("s3event.json")
 10         if err != nil {
 11                 fmt.Println("File read error:", err)
 12         }
 13         fmt.Println(string(content))
 14 }
package main

import (
        "fmt"
        "os"
)

func main() {
        content, err := os.ReadFile("s3event.json")
        if err != nil {
                fmt.Println("File read error:", err)
        }
        fmt.Println(string(content))
}
{
  "Records": [
    {
      "eventVersion": "2.1",
      "eventSource": "aws:s3",
      "awsRegion": "us-east-2",
      "eventTime": "2019-09-03T19:37:27.192Z",
      "eventName": "ObjectCreated:Put",
      "userIdentity": {
        "principalId": "AWS:AIDAINPONIXQXHT3IKHL2"
      },
      "requestParameters": {
        "sourceIPAddress": "205.255.255.255"
      },
      "responseElements": {
        "x-amz-request-id": "D82B88E5F771F645",
        "x-amz-id-2": "vlR7PnpV2Ce81l0PRw6jlUpck7Jo5ZsQjryTjKlc5aLWGVHPZLj5NeC6qMa0emYBDXOo6QBU0Wo="
      },
      "s3": {
        "s3SchemaVersion": "1.0",
        "configurationId": "828aa6fc-f7b5-4305-8584-487c791949c1",
        "bucket": {
          "name": "lambda-artifacts-deafc19498e3f2df",
          "ownerIdentity": {
            "principalId": "A3I5XTEXAMAI3E"
          },
          "arn": "arn:aws:s3:::lambda-artifacts-deafc19498e3f2df"
        },
        "object": {
          "key": "b21b84d653bb07b05b1e6b33684dc11b",
          "size": 1305107,
          "eTag": "b21b84d653bb07b05b1e6b33684dc11b",
          "sequencer": "0C0F6F405D6ED209E1"
        }
      }
    }
  ]
}

Import Standard Library Package

The file functions are defined in the standard library package “os”, so in line 5 os is imported:

  5         "os"

In older implementations you would see the package ioutil. The use for file functions with ioutil is depcrecated since go 1.1.6

Read whole file

With ReadFile you read the whole file at once into a variable.

  9         content, err := os.ReadFile("s3event.json")

ReadFile is defined as follows:

func os.ReadFile(name string) ([]byte, error)

So you get an array of bytes, which you have to convert to a string to print it:

 13         fmt.Println(string(content))

Now I want to work with the data in the file. To do this, the string has to be converted in a structure in the next chapter.

See also

Source

See the full source on github.

Sources