为什么在使用`aws-sdk-go-v2`时,无法从 DynamoDB 本地容器中查看表格?

huangapple go评论83阅读模式
英文:

Why can't it see tables from dynamodb local container when using `aws-sdk-go-v2`?

问题

我正在启动一个在我的笔记本上运行的 DynamoDB Docker 容器(amazon/dynamodb-local:1.16.0)。我通过 aws dynamodb create-table 在实例上创建了一个表。

我可以从本地命令行看到这个表:

AWS_ACCESS_KEY_ID=test AWS_SECRET_ACCESS_KEY=test aws dynamodb list-tables --region local-env --endpoint-url http://localhost:8000

{
    "TableNames": [
        "test"
    ]
}

但是当我使用 github.com/aws/aws-sdk-go-v2/service/dynamodb 库在 Go 应用程序中时,找不到这个表。

我的 Go 代码如下:

cfg, err := config.LoadDefaultConfig(context.TODO(),
		config.WithRegion("local-env"),
		config.WithEndpointResolver(aws.EndpointResolverFunc(
			func(service, region string) (aws.Endpoint, error) {
				return aws.Endpoint{URL: "http://localhost:8000"}, nil
			})),
		config.WithCredentialsProvider(credentials.StaticCredentialsProvider{
			Value: aws.Credentials{
				AccessKeyID: "test", SecretAccessKey: "test",
			},
		}),
	)
	if err != nil {
		panic(err)
	}
	db := dynamodb.NewFromConfig(cfg)
    tables, _ :=  db.ListTables(context.TODO(), &dynamodb.ListTablesInput{})
    fmt.Println("tables", tables.TableNames)

// 输出结果为:
tables []

我使用的是与命令行相同的端点和凭证,但不明白我做错了什么。

英文:

I am launching a dynamodb docker container (amazon/dynamodb-local:1.16.0) running on my laptop. And I created a table on the instance via aws dynamodb create-table.

I am able to see the table from my local command line:

AWS_ACCESS_KEY_ID=test AWS_SECRET_ACCESS_KEY=test aws dynamodb list-tables --region local-env --endpoint-url http://localhost:8000

{
    "TableNames": [
        "test"
    ]
}

But I can't find the table when using github.com/aws/aws-sdk-go-v2/service/dynamodb library from a go application.

The code I have in go is:

cfg, err := config.LoadDefaultConfig(context.TODO(),
		config.WithRegion("local-env"),
		config.WithEndpointResolver(aws.EndpointResolverFunc(
			func(service, region string) (aws.Endpoint, error) {
				return aws.Endpoint{URL: "http://localhost:8000"}, nil
			})),
		config.WithCredentialsProvider(credentials.StaticCredentialsProvider{
			Value: aws.Credentials{
				AccessKeyID: "test", SecretAccessKey: "test",
			},
		}),
	)
	if err != nil {
		panic(err)
	}
	db := dynamodb.NewFromConfig(cfg)
    tables, _ :=  db.ListTables(context.TODO(), &dynamodb.ListTablesInput{})
    fmt.Println("tables", tables.TableNames)

// output is:
tables []

I am using the same endpoint, credential as the command line but don't understand what I did wrong.

答案1

得分: 3

我遇到了同样的问题。

最初,我使用以下命令启动Dynamodb Docker容器:

# 不起作用
docker run -p 8000:8000 amazon/dynamodb-local

这样可以让我通过CLI与Dynamo交互,但是当我通过SDK与Dynamo交互时,无法看到我从CLI创建的表。

最后我偶然发现了DynamoDB本地使用说明。从那里我意识到我需要运行一个不同的命令来启动Dynamodb。

# 这对我起作用
docker run -p 8000:8000 amazon/dynamodb-local -jar DynamoDBLocal.jar -sharedDb

不同之处在于我们告诉Dynamo使用一个共享的数据库文件。

我怀疑的是,通过CLI连接时使用了我的本地AWS凭证,从而创建了一个定制的数据库文件。当我尝试使用SDK连接时,我的凭证不可用,因此Dynamo会创建一个新的数据库文件。

英文:

I ran into the same issue.

Initially I was starting the Dynamodb Docker container with this command

# Does not work
docker run -p 8000:8000 amazon/dynamodb-local

This would allow me to interact with Dynamo from the CLI, but when I interacted with Dynamo from the SDK, I could not see the tables I had created from the CLI.

Eventually I stumbled upon DynamoDB local usage notes. From there I realized I need to run a different command to start Dynamodb.

# This worked for me
docker run -p 8000:8000 amazon/dynamodb-local -jar DynamoDBLocal.jar -sharedDb

The difference is that we are telling Dynamo to use a shared database file.

I suspect what is happening is that connecting through the CLI uses my local AWS credentials and thus creates a bespoke database file. When I try to connect using the SDK, my credentials are not available, so Dynamo creates a new database file.

huangapple
  • 本文由 发表于 2022年7月7日 14:44:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/72893345.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定