无法加载AWS凭据使用java访问dynamoDB(本地)时出错

时间:2017-10-12 20:07:49

标签: java amazon-dynamodb

我已经安装了本地版本的dynamoDB,并设置了一个maven java项目来访问数据库。当我运行代码时,我得到以下错误。由于我已在本地安装服务器(它运行子localhost:8000),我没有任何凭据提供... 知道怎么解决吗?

import java.util.Iterator;

import org.apache.commons.cli.ParseException;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.dynamodbv2.document.TableCollection;
import com.amazonaws.services.dynamodbv2.exceptions.DynamoDBLocalServiceException;
import com.amazonaws.services.dynamodbv2.local.embedded.DynamoDBEmbedded;
import com.amazonaws.services.dynamodbv2.local.main.ServerRunner;
import com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer;
import com.amazonaws.services.dynamodbv2.model.ListTablesResult;

public class Test {

    public static void main(String[] args) {

         AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(
                    // we can use any region here
                    new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2"))
                    .build();
         DynamoDB dynamoDB = new DynamoDB(client);
         //dynamoDB.listTables();
         TableCollection<ListTablesResult> list = dynamoDB.listTables();

         Iterator<Table> iterator = list.iterator();

            System.out.println("Listing table names");

            while (iterator.hasNext()) {
                Table table = iterator.next();
                System.out.println(table.getTableName());
            }


         System.out.println("over");
    }



}

错误是

Exception in thread "main" com.amazonaws.SdkClientException: Unable to load AWS credentials from any provider in the chain
    at com.amazonaws.auth.AWSCredentialsProviderChain.getCredentials(AWSCredentialsProviderChain.java:131)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.getCredentialsFromContext(AmazonHttpClient.java:1115)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.runBeforeRequestHandlers(AmazonHttpClient.java:764)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:728)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:721)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:704)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:672)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:654)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:518)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.doInvoke(AmazonDynamoDBClient.java:1831)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:1807)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.listTables(AmazonDynamoDBClient.java:1123)
    at com.amazonaws.services.dynamodbv2.document.internal.ListTablesCollection.firstPage(ListTablesCollection.java:46)
    at com.amazonaws.services.dynamodbv2.document.internal.PageIterator.next(PageIterator.java:45)
    at com.amazonaws.services.dynamodbv2.document.internal.IteratorSupport.nextResource(IteratorSupport.java:87)
    at com.amazonaws.services.dynamodbv2.document.internal.IteratorSupport.hasNext(IteratorSupport.java:55)

3 个答案:

答案 0 :(得分:6)

当我在寻找同样的问题时偶然发现了这一点。经过半天的浪费时间,设法解决了这个问题。在这里张贴以防万一有人再遇到这种情况。

最糟糕的部分?我经历了成千上万页后必须通过并进行实验的解决方案,你会期望有关于这个问题的一些信息。至少,文档应该提到一些注意事项!

解决方案:

Configuring AWS Credentials:通过它来设置一些凭据。将其配置为任何随机的东西,它并不重要。

是的,就是这样!!

对于那些仍然懒惰的人(像我;-))来说,只需遵循最简单的方法:

  1. 打开默认配置文件:~/.aws/credentials
  2. 将其中的值更改为任何内容(例如此处为空字符串)

    [default]
    aws_access_key_id=''
    aws_secret_access_key=''
    
  3. 运行程序。你可以稍后再次感谢我:D

答案 1 :(得分:1)

我有一个类似的问题。为了避免在本地运行测试时遇到的麻烦,我添加了几行来设置java系统属性。

System.setProperty(ACCESS_KEY_SYSTEM_PROPERTY,“ accesskey”); System.setProperty(SECRET_KEY_SYSTEM_PROPERTY,“ secretkey”);

答案 2 :(得分:0)

根据Amazon Web Services文档,Working with AWS Credentials

官方支持的Java系统属性是:

  • aws.accessKeyId
  • aws.secretKey

以下设置了这些系统属性:

System.setProperty("aws.accessKeyId", "super-access-key");
System.setProperty("aws.secretKey", "super-secret-key");

需要在创建Amazon DynamoDB客户端之前进行设置。