当使用`aws-sdk-go-v2`时,为什么还能从DynamoDB本地容器中看到表?

发布于 2025-02-14 00:47:34 字数 1310 浏览 1 评论 0原文

我正在启动一个运行在笔记本电脑上的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简单爱 2025-02-21 00:47:34

我遇到了同一问题。

最初,我使用此命令启动DynamoDB Docker容器,

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

这将使我可以与CLI的Dynamo进行交互,但是当我从SDK与Dynamo进行交互时,我看不到我从CLI创建的表。

最终,我偶然发现了 dynamodb本地用法。从那里,我意识到我需要运行其他命令来启动DynamoDB。

# This worked for me
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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文