Example usage for com.amazonaws.auth DefaultAWSCredentialsProviderChain getCredentials

List of usage examples for com.amazonaws.auth DefaultAWSCredentialsProviderChain getCredentials

Introduction

In this page you can find the example usage for com.amazonaws.auth DefaultAWSCredentialsProviderChain getCredentials.

Prototype

@Override
    public AWSCredentials getCredentials() 

Source Link

Usage

From source file:org.apache.bookkeeper.mledger.offload.jcloud.impl.BlobStoreManagedLedgerOffloader.java

License:Apache License

public static Credentials getCredentials(String driver, TieredStorageConfigurationData conf)
        throws IOException {
    // credentials:
    //   for s3, get by DefaultAWSCredentialsProviderChain.
    //   for gcs, use downloaded file 'google_creds.json', which contains service account key by
    //     following instructions in page https://support.google.com/googleapi/answer/6158849

    if (isGcsDriver(driver)) {
        String gcsKeyPath = conf.getGcsManagedLedgerOffloadServiceAccountKeyFile();
        if (Strings.isNullOrEmpty(gcsKeyPath)) {
            throw new IOException("The service account key path is empty for GCS driver");
        }//from ww w. j  a  v  a 2  s  . c o m
        try {
            String gcsKeyContent = Files.toString(new File(gcsKeyPath), Charset.defaultCharset());
            return new GoogleCredentialsFromJson(gcsKeyContent).get();
        } catch (IOException ioe) {
            log.error("Cannot read GCS service account credentials file: {}", gcsKeyPath);
            throw new IOException(ioe);
        }
    } else if (isS3Driver(driver)) {
        AWSCredentials credentials = null;
        try {
            DefaultAWSCredentialsProviderChain creds = DefaultAWSCredentialsProviderChain.getInstance();
            credentials = creds.getCredentials();
        } catch (Exception e) {
            // allowed, some mock s3 service not need credential
            log.warn("Exception when get credentials for s3 ", e);
        }

        String id = "accesskey";
        String key = "secretkey";
        if (credentials != null) {
            id = credentials.getAWSAccessKeyId();
            key = credentials.getAWSSecretKey();
        }
        return new Credentials(id, key);
    } else {
        throw new IOException("Not support this kind of driver: " + driver);
    }
}