当使用`aws-sdk-go-v2`时,为什么还能从DynamoDB本地容器中看到表?
我正在启动一个运行在笔记本电脑上的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中拥有的代码是:
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了同一问题。
最初,我使用此命令启动DynamoDB Docker容器,
这将使我可以与CLI的Dynamo进行交互,但是当我从SDK与Dynamo进行交互时,我看不到我从CLI创建的表。
最终,我偶然发现了 dynamodb本地用法。从那里,我意识到我需要运行其他命令来启动DynamoDB。
不同之处在于,我们告诉Dynamo使用共享数据库文件。
我怀疑发生的事情是,通过CLI连接使用我的本地AWS凭据,从而创建一个定制数据库文件。当我尝试使用SDK连接时,我的凭据不可用,因此Dynamo会创建一个新的数据库文件。
I ran into the same issue.
Initially I was starting the Dynamodb Docker container with this command
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.
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.