/
DynamoDBListTables.go
71 lines (59 loc) · 2.04 KB
/
DynamoDBListTables.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[dynamodb.go.list_tables]
package main
// snippet-start:[dynamodb.go.list_tables.imports]
import (
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"fmt"
)
// snippet-end:[dynamodb.go.list_tables.imports]
func main() {
// snippet-start:[dynamodb.go.list_tables.session]
// 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)
// snippet-end:[dynamodb.go.list_tables.session]
// snippet-start:[dynamodb.go.list_tables.call]
// create the input configuration instance
input := &dynamodb.ListTablesInput{}
fmt.Printf("Tables:\n")
for {
// Get the list of tables
result, err := svc.ListTables(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case dynamodb.ErrCodeInternalServerError:
fmt.Println(dynamodb.ErrCodeInternalServerError, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
for _, n := range result.TableNames {
fmt.Println(*n)
}
// assign the last read tablename as the start for our next call to the ListTables function
// the maximum number of table names returned in a call is 100 (default), which requires us to make
// multiple calls to the ListTables function to retrieve all table names
input.ExclusiveStartTableName = result.LastEvaluatedTableName
if result.LastEvaluatedTableName == nil {
break
}
}
// snippet-end:[dynamodb.go.list_tables.call]
}
// snippet-end:[dynamodb.go.list_tables]