1

我的任务是获取 aws procuct 定价数据,所以我执行以下操作:

  1. 设置环境变量(AWS_ACCESS_KEY_ID 和 AWS_SECRET_ACCESS_KEY) 在此处输入图像描述

  2. 用代码填充 pom.xml(在 Eclipse 中):

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-pricing</artifactId>
    <version>1.11.513</version>
</dependency>
  1. 然后我像代码一样编写测试方法:
public static void testDescribeServices() {
    System.out.println("\n\n begin ....");
    AWSPricing client = AWSPricingClientBuilder.standard().withRegion("us-east-1").build();
    System.out.println("step 0001");
    DescribeServicesRequest servicesRequest = new DescribeServicesRequest();
    servicesRequest.setServiceCode("AmazonEC2");
    DescribeServicesResult result = client.describeServices(servicesRequest);
    List<Service> serviceList = result.getServices();
}
  1. 运行 testDescribeServices() 方法,控制台上的消息是这样的:(我很失望)
begin ....
17:08:21.166 [main] DEBUG com.amazonaws.AmazonWebServiceClient - Internal logging successfully configured to commons logger: true
17:08:21.831 [main] DEBUG com.amazonaws.monitoring.CsmConfigurationProviderChain - Unable to load configuration from com.amazonaws.monitoring.EnvironmentVariableCsmConfigurationProvider@130d63be: Unable to load Client Side Monitoring configurations from environment variables!
17:08:21.831 [main] DEBUG com.amazonaws.monitoring.CsmConfigurationProviderChain - Unable to load configuration from com.amazonaws.monitoring.SystemPropertyCsmConfigurationProvider@42a48628: Unable to load Client Side Monitoring configurations from system properties variables!
17:08:21.831 [java-sdk-http-connection-reaper] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Closing connections idle longer than 60000 MILLISECONDS
17:08:21.839 [main] DEBUG com.amazonaws.monitoring.CsmConfigurationProviderChain - Unable to load configuration from com.amazonaws.monitoring.ProfileCsmConfigurationProvider@689604d9: The 'default' profile does not define all the required properties!
Exception in thread "main" java.lang.NoSuchMethodError: com.amazonaws.client.AwsSyncClientParams.getAdvancedConfig()Lcom/amazonaws/client/builder/AdvancedConfig;
    at com.amazonaws.services.pricing.AWSPricingClient.<init>(AWSPricingClient.java:158)
    at com.amazonaws.services.pricing.AWSPricingClient.<init>(AWSPricingClient.java:142)
    at com.amazonaws.services.pricing.AWSPricingClientBuilder.build(AWSPricingClientBuilder.java:61)
    at com.amazonaws.services.pricing.AWSPricingClientBuilder.build(AWSPricingClientBuilder.java:27)
    at com.amazonaws.client.builder.AwsSyncClientBuilder.build(AwsSyncClientBuilder.java:46)
    at com.yunion.apps.metadata.TestAWSDemoAPI.testDescribeServices(TestAWSDemoAPI.java:31)
    at com.yunion.apps.metadata.TestAWSDemoAPI.main(TestAWSDemoAPI.java:26)
4

1 回答 1

0

在尝试了一些设置后,我取得了一些进展,新方法是:

public static void testDescribeServices(String accessKey, String secretKey) {
    BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKey, secretKey);
    AWSCredentialsProvider credentialsProvider = new AWSStaticCredentialsProvider(basicAWSCredentials);
    AWSPricingClientBuilder builder = AWSPricingClientBuilder.standard();
    builder.setCredentials(credentialsProvider);
    builder.withRegion("us-east-1");
    CsmConfigurationProvider csmConfigurationProvider = new EnvironmentVariableCsmConfigurationProvider();
    builder.setClientSideMonitoringConfigurationProvider(csmConfigurationProvider);
    System.out.println("step 0001");
    AWSPricing awsPricing = builder.build();
    System.out.println("step 0002");
    DescribeServicesRequest servicesRequest = new DescribeServicesRequest();
    servicesRequest.setServiceCode("AmazonEC2");
    System.out.println("step 0003");
    DescribeServicesResult result = awsPricing.describeServices(servicesRequest);
    System.out.println("\n\n step 0004");
    List<Service> serviceList = result.getServices();
    System.out.println("\n\n step 0005");
}

然而,结果是

Exception in thread "main" com.amazonaws.services.pricing.model.AWSPricingException: User: arn:aws:iam::285906155448:user/dev1 is not authorized to perform: pricing:DescribeServices **(Service: AWSPricing; Status Code: 400; Error Code: AccessDeniedException; Request ID: 13155463-4198-11e9-8f92-f1f731c320f2)**
于 2019-03-08T11:55:44.557 回答