我目前正在使用 Eclipse 和 AWS Toolkit for Eclipse。我的项目已经运行,它正在完成它的工作,即连接到 RDS 实例并将 JSON 对象返回给 API Gateway 调用。
我刚刚有一个新需求,我们要使用服务 SecretsManager 来自动轮换 RDS 配置,例如用户、密码等。
问题是当我尝试导入诸如 之类的类时GetSecretValueResponse
,我得到了一个The import com.amazonaws.services.secretsmanager cannot be resolved
. 当我浏览文档和 SDK 时,存在 aGetSecretValueRequest
但不存在 a GetSecretValueResponse
,所以我无法理解我应该做什么,也没有找到与我可以研究的示例类似的任何东西。
以下代码是我正在尝试实现的,由 Amazon 自己提供(在 Secrets Manager 页面中有一个按钮,您可以单击以查看它在这种情况下如何与 Java 一起使用),并且它在没有任何修改的情况下呈现但是因为正如我所说,我不知道如何导入几个类:
// Use this code snippet in your app.
public static void getSecret() {
String secretName = "secretName";
String endpoint = "secretEndpoint";
String region = "region";
AwsClientBuilder.EndpointConfiguration config = new AwsClientBuilder.EndpointConfiguration(endpoint, region);
AWSSecretsManagerClientBuilder clientBuilder = AWSSecretsManagerClientBuilder.standard();
clientBuilder.setEndpointConfiguration(config);
AWSSecretsManager client = clientBuilder.build();
String secret;
ByteBuffer binarySecretData;
GetSecretValueRequest getSecretValueRequest = GetSecretValueRequest.builder()
.withSecretId(secretName)
.build();
GetSecretValueResponse getSecretValueResponse = null;
try {
getSecretValueResponse = client.getSecretValue(getSecretValueRequest);
} catch(ResourceNotFoundException e) {
System.out.println("The requested secret " + secretName + " was not found");
} catch (InvalidRequestException e) {
System.out.println("The request was invalid due to: " + e.getMessage());
} catch (InvalidParameterException e) {
System.out.println("The request had invalid params: " + e.getMessage());
}
if(getSecretValueResponse == null) {
return;
}
// Decrypted secret using the associated KMS CMK
// Depending on whether the secret was a string or binary, one of these fields will be populated
if(getSecretValueResponse.getSecretString() != null) {
secret = getSecretValueResponse.getSecretString();
}
else {
binarySecretData = getSecretValueResponse.getSecretBinary();
}
// Your code goes here.
}