如何在java中为inMemory DynamoDBLocal设置aws.access.key和aws.secret.key?

时间:2017-09-19 08:21:12

标签: java junit amazon-dynamodb dynamo-local

如何为java中的inMemory DynamoDBLocal设置aws.access.key和aws.secret.key以用于junit目的。

1 个答案:

答案 0 :(得分:0)

实际上,当您连接到本地DynamoDB实例时,API不会期望访问和密钥。您只需设置本地端点URL并连接到DynamoDB。

这是相同的代码。

public static void main(String[] args) {

        AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.standard()
                .withEndpointConfiguration(new EndpointConfiguration("http://localhost:8000", "us-east-1")).build();

        Map<String, AttributeValue> keys = new HashMap<>();

        keys.put("yearkey", new AttributeValue().withN("2016"));
        keys.put("title", new AttributeValue("The Big New Movie 1"));

        GetItemRequest getItemRequest = new GetItemRequest().withTableName("Movies").withKey(keys);

        GetItemResult result = ddb.getItem(getItemRequest);
        System.out.println(result.getItem().toString());

    }

使用虚拟凭证的代码: -

如上所述,您无需为本地DynamoDB设置凭据。如果您正在寻找其他目的的样本,您可以设置如下所述的凭据。

BasicAWSCredentials awsCreds = new BasicAWSCredentials("access_key_id", "secret_key_id");
        AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
                .withEndpointConfiguration(new EndpointConfiguration("http://localhost:8000", "us-east-1")).build();