无法从类路径上的 /AwsCredentials.properties 文件加载 AWS 凭证

2022-08-31 22:28:10

使用此代码设置类路径

AWSCredentialsProvider credentialsProvider = new ClasspathPropertiesFileCredentialsProvider();
ec2 = new AmazonEC2Client(credentialsProvider);

以下是 AwsCredentials.properties 文件的格式

# Fill in your AWS Access Key ID and Secret Access Key
# http://aws.amazon.com/security-credentials
accessKey = keyHere
secretKey = secretKeyHere

以下是我得到的例外

Exception in thread "main" com.amazonaws.AmazonClientException: Unable to load AWS credentials from the /AwsCredentials.properties file on the classpath

    at com.amazonaws.auth.ClasspathPropertiesFileCredentialsProvider.getCredentials(ClasspathPropertiesFileCredentialsProvider.java:81)
    at com.amazonaws.services.ec2.AmazonEC2Client.invoke(AmazonEC2Client.java:8359)

答案 1

我使用不同的方法建立了连接:

BasicAWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
AmazonDynamoDBClient client = new AmazonDynamoDBClient(credentials).withRegion(Regions.US_EAST_1);
DynamoDB dynamoDB = new DynamoDB(client);

访问密钥和私有密钥可以在身份和访问管理控制台中创建。我希望它有帮助...


答案 2

您可以使用 ,根据文档,它按以下顺序查找凭据:DefaultAwsCredentialsProviderChain()

  1. 环境变量 - 和(建议使用,因为它们可被除 .NET 之外的所有 AWS 开发工具包和 CLI 识别)或和(仅由 Java 开发工具包识别)AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYAWS_ACCESS_KEYAWS_SECRET_KEY
  2. Java 系统属性 - 和aws.accessKeyIdaws.secretAccessKey
  3. 默认位置的凭证配置文件文件 () 由所有 AWS 开发工具包和 AWS CLI 共享~/.aws/credentials
  4. 通过 Amazon EC2 元数据服务提供的实例配置文件凭证

推荐