在Java中更改AWS开发工具包的用户代理

时间:2019-04-24 08:55:13

标签: java amazon-web-services amazon-s3 amazon-iam

当我们启动S3或IAM之类的服务时,是否可以更改AWS开发工具包服务的用户代理?原因是使用SDK时,我应用程序中的任何活动都将记录为“ Java 1.8 .....”。取而代之的是,我想将其更改为“ Awesome Apps”。

我的代码是这样的

public static AmazonS3 initS3() throws IOException{
    InputStream input = AWSS3.class.getClassLoader().getResourceAsStream("awscred.properties");
    Properties prop = new Properties();
    prop.load(input);
    BasicAWSCredentials  credentials  = new BasicAWSCredentials(prop.getProperty("provider.aws01.username"), prop.getProperty("provider.aws01.password"));
    AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion(Regions.EU_CENTRAL_1).withCredentials(new AWSStaticCredentialsProvider(credentials)).build();
    return s3Client;
}
public static AmazonIdentityManagement initIAM() throws IOException{
    InputStream input = AWSIAM.class.getClassLoader().getResourceAsStream("awscred.properties"); //entah kenapa ga muncul resources disini ga langsung kedetect
    Properties prop = new Properties();
    prop.load(input);
    BasicAWSCredentials  credentials  = new BasicAWSCredentials(prop.getProperty("provider.aws06.username"), prop.getProperty("provider.aws06.password"));
    AmazonIdentityManagement iam = AmazonIdentityManagementClientBuilder.standard().withRegion(Regions.EU_CENTRAL_1).withCredentials(new AWSStaticCredentialsProvider(credentials)).build();
    return iam;
}

1 个答案:

答案 0 :(得分:0)

结果证明可以使用ClientConfiguration完成。这是S3的示例:

public static AmazonS3 initS3() throws IOException{
        ClientConfiguration config = new ClientConfiguration();
        config.setUserAgentPrefix("CloudRAID Management");
        config.setUserAgentSuffix("006b8507-b815-47b9-bce0-08b91981f17a");

        InputStream input = AWSS3.class.getClassLoader().getResourceAsStream("awscred.properties");
        Properties prop = new Properties();
        prop.load(input);
        BasicAWSCredentials  credentials  = new BasicAWSCredentials(prop.getProperty("provider.aws01.username"), prop.getProperty("provider.aws01.password"));
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion(Regions.EU_CENTRAL_1).withCredentials(new AWSStaticCredentialsProvider(credentials)).withClientConfiguration(config).build();

        return s3Client;

}