AWS Code Sample
Catalog

The AWS Documentation website is getting a new look!
Try it now and let us know what you think. Switch to the new look >>

You can return to the original look by selecting English in the language selector above.

DynamoDBLoadItems.go

DynamoDBLoadItems.go adds items from a JSON file to an Amazon DynamoDB table.

/* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. This file is licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at http://aws.amazon.com/apache2.0/ This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package main import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/dynamodb" "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute" "encoding/json" "fmt" "io/ioutil" "os" "strconv" ) // Create struct to hold info about new item type Item struct { Year int Title string Plot string Rating float64 } // Get table items from JSON file func getItems() []Item { raw, err := ioutil.ReadFile("./movie_data.json") if err != nil { fmt.Println(err.Error()) os.Exit(1) } var items []Item json.Unmarshal(raw, &items) return items } func main() { // Initialize a session that the SDK will use to load // credentials from the shared credentials file ~/.aws/credentials // and region from the shared configuration file ~/.aws/config. sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) // Create DynamoDB client svc := dynamodb.New(sess) // Get table items from movie_data.json items := getItems() // Add each item to Movies table: tableName := "Movies" for _, item := range items { av, err := dynamodbattribute.MarshalMap(item) if err != nil { fmt.Println("Got error marshalling map:") fmt.Println(err.Error()) os.Exit(1) } // Create item in table Movies input := &dynamodb.PutItemInput{ Item: av, TableName: aws.String(tableName), } _, err = svc.PutItem(input) if err != nil { fmt.Println("Got error calling PutItem:") fmt.Println(err.Error()) os.Exit(1) } year := strconv.Itoa(item.Year) fmt.Println("Successfully added '" + item.Title + "' (" + year + ") to table " + tableName) } }

Sample Details

Service: dynamodb

Last tested: 2019-03-19

Author: Doug-AWS

Type: full-example

On this page: