【问题标题】:How to load AWS credentials from file?如何从文件中加载 AWS 凭证?
【发布时间】:2016-11-04 18:45:19
【问题描述】:

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

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

【问题讨论】:

  • 您确定它正在打开该文件吗?尝试“破坏”文件名以确保它给出不同的错误消息。
  • 出现同样的异常

标签: java amazon-web-services speech


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我的问题已经解决了!

    我使用我的 secretKey 和 accessKey 自定义实现了 AWSCredentials 类

    【讨论】:

    • 所以.. 你在硬编码密钥?
    • 您如何以及在何处对密钥进行硬编码
    • 好吧,至少从环境变量中获取凭据:例如:System.getenv("AWS_ACCESS_KEY")
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 2022-09-23
    • 2023-02-03
    • 2021-01-15
    • 2019-01-15
    • 2017-06-15
    相关资源
    最近更新 更多