简体   繁体   中英

AWS Java SDK credentials

I am using the AWS Java SDK and trying to run some tests; getting:

Unable to load AWS credentials from the /AwsCredentials.properties file on the classpath

The credentials file @ ~/.aws/ is correct per AWS specs; 777'd it to ensure no access issues.

I am not using any IDE plug-ins; per AWS docs, having a credentials file @ ~/.aws/ should suffice. Anyone have this working with just the SDK installed? If I hard-code the file path into the ClasspathPropertiesFileCredentialsProvider() request it spits the error back with the path instead of the AwsCredentials.properties string, which doesn't exist anywhere (yes, tried making one of those in ~/.aws/ as well).

Thanks much for any insights, code is below straight from Amazon:

import com.amazonaws.auth.ClasspathPropertiesFileCredentialsProvider;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.sns.AmazonSNSClient;
import com.amazonaws.services.sns.model.PublishRequest;
import com.amazonaws.services.sns.model.PublishResult;

public class SNS {

    public static void main(String[] args) {
        AmazonSNSClient snsClient = new AmazonSNSClient(new ClasspathPropertiesFileCredentialsProvider());
        snsClient.setRegion(Region.getRegion(Regions.US_EAST_1));

        String msg = "this is a test";
        PublishRequest publishRequest = new PublishRequest("my arn", msg);
        PublishResult publishResult = snsClient.publish(publishRequest);
        System.out.println("MessageId - " + publishResult.getMessageId());
    }
}

If you use DefaultAWSCredentialsProviderChain instead of ClasspathPropertiesFileCredentialsProvider , it will automatically check various default locations for AWS credentials. ( Documentation )

Have you verified that your $HOME environment variable is set for the process you are running? The AWS SDK relies on $HOME to determine the proper location of your .aws folder.

Well that didn't work the way I'd planned it; couldn't get the .aws path as a classpath (tried adding as an external class folder).

Ran the below to find the actual classpaths in my project:

    public static void main (String args[]) {

        ClassLoader cl = ClassLoader.getSystemClassLoader();

        URL[] urls = ((URLClassLoader)cl).getURLs();

        for(URL url: urls){
            System.out.println(url.getFile());
        }

   }

and then dropped my AWS credentials into a new AwsCredentials.properties file in one of the dirs from above (I had one; the rest were jar files).

Changed the tag values in the file to "accessKey" and "secretKey" from what was there (aws_access_key, aws_secret_access_key) and it worked.

Thanks to everyone for their inputs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM