如何从文件加载AWS凭据?

时间:2015-03-05 16:17:24

标签: java amazon-web-services speech

我正在使用IVONA SpeachCloud SDK(创建语音示例):https://github.com/IvonaSoftware/ivona-speechcloud-sdk-java/blob/master/src/samples/IvonaSpeechCloudCreateSpeech/SampleIvonaSpeechCloudCreateSpeech.java

使用此代码设置类路径

private static IvonaSpeechCloudClient speechCloud;

private static void init() {
    speechCloud = new IvonaSpeechCloudClient(
            new ClasspathPropertiesFileCredentialsProvider("resources/IvonaCredentials.properties"));
    speechCloud.setEndpoint("https://tts.eu-west-1.ivonacloud.com");
}

以下是ivona.properties文件的格式。文件位于资源目​​录中。我在SpeechCloud帐户中获得的所需凭据

accessKey = mykey 
secretKey = mysecretKey

以下是我得到的例外

Exception in thread "main" com.amazonaws.AmazonClientException: Unable to load AWS credentials from the /resources/ivona.properties file on the classpath
at com.amazonaws.auth.ClasspathPropertiesFileCredentialsProvider.getCredentials(ClasspathPropertiesFileCredentialsProvider.java:81)
at com.ivona.services.tts.IvonaSpeechCloudClient.prepareRequest(IvonaSpeechCloudClient.java:279)
at com.ivona.services.tts.IvonaSpeechCloudClient.prepareRequest(IvonaSpeechCloudClient.java:272)
at com.ivona.services.tts.IvonaSpeechCloudClient.invoke(IvonaSpeechCloudClient.java:259)
at com.ivona.services.tts.IvonaSpeechCloudClient.createSpeech(IvonaSpeechCloudClient.java:148)
at SampleIvonaSpeechCloudCreateSpeech.main(SampleIvonaSpeechCloudCreateSpeech.java:45

如何解决此异常?感谢。

2 个答案:

答案 0 :(得分:3)

好吧,我在源文件中搞乱几个小时之后想出来了。您可以创建自己的Provider Class,您可以在其中将凭据作为String Parameters传递。

这是我的自定义凭证类" IvonaCredentials"

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;

public class IvonaCredentials implements AWSCredentialsProvider{

public IvonaCredentials(String mSecretKey, String mAccessKey) {
    super();
    this.mSecretKey = mSecretKey;
    this.mAccessKey = mAccessKey;
}

private String mSecretKey;
private String mAccessKey;

@Override
public AWSCredentials getCredentials() {
    AWSCredentials awsCredentials = new AWSCredentials() {

        @Override
        public String getAWSSecretKey() {
            // TODO Auto-generated method stub
            return mSecretKey;
        }

        @Override
        public String getAWSAccessKeyId() {
            // TODO Auto-generated method stub
            return mAccessKey;
        };
    };
    return awsCredentials;
}

@Override
public void refresh() {
    // TODO Auto-generated method stub

}



}

这就是我打电话给我的课程

private static void init() {
    speechCloud = new IvonaSpeechCloudClient(new IvonaCredentials("secretKey", "accessKey"));
    speechCloud.setEndpoint("https://tts.eu-west-1.ivonacloud.com");
}

答案 1 :(得分:1)

我已经解决了我的问题!

我使用我的密钥和accessKey

自定义了AWS Credentials类的实现