-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
list_tables.go
62 lines (51 loc) · 1.61 KB
/
list_tables.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
func main() {
// Initialize a session in us-west-2 that the SDK will use to load
// credentials from the shared credentials file ~/.aws/credentials.
sess, _ := session.NewSession(&aws.Config{
Region: aws.String("us-west-2")},
)
// Create DynamoDB client
svc := dynamodb.New(sess)
// 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
}
}
}