github.com/aavshr/aws-sdk-go@v1.41.3/example/service/dynamodb/scanItems/scanItems.go (about)

     1  //go:build example
     2  // +build example
     3  
     4  package main
     5  
     6  import (
     7  	"flag"
     8  	"fmt"
     9  	"os"
    10  
    11  	"github.com/aavshr/aws-sdk-go/aws"
    12  	"github.com/aavshr/aws-sdk-go/aws/session"
    13  	"github.com/aavshr/aws-sdk-go/service/dynamodb"
    14  	"github.com/aavshr/aws-sdk-go/service/dynamodb/dynamodbattribute"
    15  )
    16  
    17  func exitWithError(err error) {
    18  	fmt.Fprintln(os.Stderr, err)
    19  	os.Exit(1)
    20  }
    21  
    22  func main() {
    23  	cfg := Config{}
    24  	if err := cfg.Load(); err != nil {
    25  		exitWithError(fmt.Errorf("failed to load config, %v", err))
    26  	}
    27  
    28  	// Create the config specifying the Region for the DynamoDB table.
    29  	// If Config.Region is not set the region must come from the shared
    30  	// config or AWS_REGION environment variable.
    31  	awscfg := &aws.Config{}
    32  	if len(cfg.Region) > 0 {
    33  		awscfg.WithRegion(cfg.Region)
    34  	}
    35  
    36  	// Create the session that the DynamoDB service will use.
    37  	sess := session.Must(session.NewSession(awscfg))
    38  
    39  	// Create the DynamoDB service client to make the query request with.
    40  	svc := dynamodb.New(sess)
    41  
    42  	// Build the query input parameters
    43  	params := &dynamodb.ScanInput{
    44  		TableName: aws.String(cfg.Table),
    45  	}
    46  	if cfg.Limit > 0 {
    47  		params.Limit = aws.Int64(cfg.Limit)
    48  	}
    49  
    50  	// Make the DynamoDB Query API call
    51  	result, err := svc.Scan(params)
    52  	if err != nil {
    53  		exitWithError(fmt.Errorf("failed to make Query API call, %v", err))
    54  	}
    55  
    56  	items := []Item{}
    57  
    58  	// Unmarshal the Items field in the result value to the Item Go type.
    59  	err = dynamodbattribute.UnmarshalListOfMaps(result.Items, &items)
    60  	if err != nil {
    61  		exitWithError(fmt.Errorf("failed to unmarshal Query result items, %v", err))
    62  	}
    63  
    64  	// Print out the items returned
    65  	for i, item := range items {
    66  		fmt.Printf("%d: Key: %d, Desc: %s\n", i, item.Key, item.Desc)
    67  		fmt.Printf("\tNum Data Values: %d\n", len(item.Data))
    68  		for k, v := range item.Data {
    69  			fmt.Printf("\t- %q: %v\n", k, v)
    70  		}
    71  	}
    72  }
    73  
    74  type Item struct {
    75  	Key  int
    76  	Desc string
    77  	Data map[string]interface{}
    78  }
    79  
    80  type Config struct {
    81  	Table  string // required
    82  	Region string // optional
    83  	Limit  int64  // optional
    84  
    85  }
    86  
    87  func (c *Config) Load() error {
    88  	flag.Int64Var(&c.Limit, "limit", 0, "Limit is the max items to be returned, 0 is no limit")
    89  	flag.StringVar(&c.Table, "table", "", "Table to Query on")
    90  	flag.StringVar(&c.Region, "region", "", "AWS Region the table is in")
    91  	flag.Parse()
    92  
    93  	if len(c.Table) == 0 {
    94  		flag.PrintDefaults()
    95  		return fmt.Errorf("table name is required.")
    96  	}
    97  
    98  	return nil
    99  }