gpt4 book ai didi

java - 如何从文件加载 AWS 凭证?

转载 作者:行者123 更新时间:2023-11-30 07:02:46 31 4
gpt4 key购买 nike

我正在使用 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

如何解决这个异常?谢谢。

最佳答案

好吧,我在源文件中弄乱了几个小时后弄清楚了这一点。您可以创建自己的提供程序类,您可以在其中将凭据作为字符串参数传递。

这是我的自定义凭证类“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

}



}

我就是这样称呼我的类(class)的

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

关于java - 如何从文件加载 AWS 凭证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28882564/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com