Example usage for com.amazonaws Protocol HTTPS

List of usage examples for com.amazonaws Protocol HTTPS

Introduction

In this page you can find the example usage for com.amazonaws Protocol HTTPS.

Prototype

Protocol HTTPS

To view the source code for com.amazonaws Protocol HTTPS.

Click Source Link

Document

HTTPS Protocol - Using the HTTPS protocol is more secure than using the HTTP protocol, but may use slightly more system resources.

Usage

From source file:org.apache.hadoop.fs.s3a.S3AFileSystem.java

License:Apache License

/** Called after a new FileSystem instance is constructed.
 * @param name a uri whose authority section names the host, port, etc.
 *   for this FileSystem/*  w ww .  ja va 2s  .c o  m*/
 * @param conf the configuration
 */
public void initialize(URI name, Configuration conf) throws IOException {
    super.initialize(name, conf);

    uri = URI.create(name.getScheme() + "://" + name.getAuthority());
    workingDir = new Path("/user", System.getProperty("user.name")).makeQualified(this.uri,
            this.getWorkingDirectory());

    // Try to get our credentials or just connect anonymously
    String accessKey = conf.get(NEW_ACCESS_KEY, conf.get(OLD_ACCESS_KEY, null));
    String secretKey = conf.get(NEW_SECRET_KEY, conf.get(OLD_SECRET_KEY, null));

    String userInfo = name.getUserInfo();
    if (userInfo != null) {
        int index = userInfo.indexOf(':');
        if (index != -1) {
            accessKey = userInfo.substring(0, index);
            secretKey = userInfo.substring(index + 1);
        } else {
            accessKey = userInfo;
        }
    }

    AWSCredentialsProviderChain credentials = new AWSCredentialsProviderChain(
            new BasicAWSCredentialsProvider(accessKey, secretKey), new InstanceProfileCredentialsProvider(),
            new AnonymousAWSCredentialsProvider());

    bucket = name.getHost();

    ClientConfiguration awsConf = new ClientConfiguration();
    awsConf.setMaxConnections(conf.getInt(NEW_MAXIMUM_CONNECTIONS,
            conf.getInt(OLD_MAXIMUM_CONNECTIONS, DEFAULT_MAXIMUM_CONNECTIONS)));
    awsConf.setProtocol(conf.getBoolean(NEW_SECURE_CONNECTIONS,
            conf.getBoolean(OLD_SECURE_CONNECTIONS, DEFAULT_SECURE_CONNECTIONS)) ? Protocol.HTTPS
                    : Protocol.HTTP);
    awsConf.setMaxErrorRetry(
            conf.getInt(NEW_MAX_ERROR_RETRIES, conf.getInt(OLD_MAX_ERROR_RETRIES, DEFAULT_MAX_ERROR_RETRIES)));
    awsConf.setSocketTimeout(
            conf.getInt(NEW_SOCKET_TIMEOUT, conf.getInt(OLD_SOCKET_TIMEOUT, DEFAULT_SOCKET_TIMEOUT)));

    s3 = new AmazonS3Client(credentials, awsConf);

    maxKeys = conf.getInt(NEW_MAX_PAGING_KEYS, conf.getInt(OLD_MAX_PAGING_KEYS, DEFAULT_MAX_PAGING_KEYS));
    partSize = conf.getLong(NEW_MULTIPART_SIZE, conf.getLong(OLD_MULTIPART_SIZE, DEFAULT_MULTIPART_SIZE));
    partSizeThreshold = conf.getLong(NEW_MIN_MULTIPART_THRESHOLD,
            conf.getLong(OLD_MIN_MULTIPART_THRESHOLD, DEFAULT_MIN_MULTIPART_THRESHOLD));

    if (partSize < 5 * 1024 * 1024) {
        LOG.error(NEW_MULTIPART_SIZE + " must be at least 5 MB");
        partSize = 5 * 1024 * 1024;
    }

    if (partSizeThreshold < 5 * 1024 * 1024) {
        LOG.error(NEW_MIN_MULTIPART_THRESHOLD + " must be at least 5 MB");
        partSizeThreshold = 5 * 1024 * 1024;
    }

    String cannedACLName = conf.get(NEW_CANNED_ACL, conf.get(OLD_CANNED_ACL, DEFAULT_CANNED_ACL));
    if (!cannedACLName.isEmpty()) {
        cannedACL = CannedAccessControlList.valueOf(cannedACLName);
    } else {
        cannedACL = null;
    }

    if (!s3.doesBucketExist(bucket)) {
        throw new IOException("Bucket " + bucket + " does not exist");
    }

    boolean purgeExistingMultipart = conf.getBoolean(NEW_PURGE_EXISTING_MULTIPART,
            conf.getBoolean(OLD_PURGE_EXISTING_MULTIPART, DEFAULT_PURGE_EXISTING_MULTIPART));
    long purgeExistingMultipartAge = conf.getLong(NEW_PURGE_EXISTING_MULTIPART_AGE,
            conf.getLong(OLD_PURGE_EXISTING_MULTIPART_AGE, DEFAULT_PURGE_EXISTING_MULTIPART_AGE));

    if (purgeExistingMultipart) {
        TransferManager transferManager = new TransferManager(s3);
        Date purgeBefore = new Date(new Date().getTime() - purgeExistingMultipartAge * 1000);

        transferManager.abortMultipartUploads(bucket, purgeBefore);
        transferManager.shutdownNow(false);
    }

    serverSideEncryptionAlgorithm = conf.get(SERVER_SIDE_ENCRYPTION_ALGORITHM, null);

    setConf(conf);
}

From source file:org.apache.hadoop.fs.s3r.S3RFileSystem.java

License:Apache License

/** Called after a new FileSystem instance is constructed.
 * @param name a uri whose authority section names the host, port, etc.
 *   for this FileSystem// w ww  .  j av a 2 s .c  o  m
 * @param conf the configuration
 */
public void initialize(URI name, Configuration conf) throws IOException {
    super.initialize(name, conf);

    uri = URI.create(name.getScheme() + "://" + name.getAuthority());
    workingDir = new Path("/user", System.getProperty("user.name")).makeQualified(this.uri,
            this.getWorkingDirectory());

    // Try to get our credentials or just connect anonymously
    String accessKey = conf.get(ACCESS_KEY, null);
    String secretKey = conf.get(SECRET_KEY, null);

    String userInfo = name.getUserInfo();
    if (userInfo != null) {
        int index = userInfo.indexOf(':');
        if (index != -1) {
            accessKey = userInfo.substring(0, index);
            secretKey = userInfo.substring(index + 1);
        } else {
            accessKey = userInfo;
        }
    }

    AWSCredentialsProviderChain credentials = new AWSCredentialsProviderChain(
            new BasicAWSCredentialsProvider(accessKey, secretKey), new InstanceProfileCredentialsProvider(),
            new AnonymousAWSCredentialsProvider());

    bucket = name.getHost();

    ClientConfiguration awsConf = new ClientConfiguration();
    awsConf.setMaxConnections(conf.getInt(MAXIMUM_CONNECTIONS, DEFAULT_MAXIMUM_CONNECTIONS));
    boolean secureConnections = conf.getBoolean(SECURE_CONNECTIONS, DEFAULT_SECURE_CONNECTIONS);
    awsConf.setProtocol(secureConnections ? Protocol.HTTPS : Protocol.HTTP);
    awsConf.setMaxErrorRetry(conf.getInt(MAX_ERROR_RETRIES, DEFAULT_MAX_ERROR_RETRIES));
    awsConf.setConnectionTimeout(conf.getInt(ESTABLISH_TIMEOUT, DEFAULT_ESTABLISH_TIMEOUT));
    awsConf.setSocketTimeout(conf.getInt(SOCKET_TIMEOUT, DEFAULT_SOCKET_TIMEOUT));

    String proxyHost = conf.getTrimmed(PROXY_HOST, "");
    int proxyPort = conf.getInt(PROXY_PORT, -1);
    if (!proxyHost.isEmpty()) {
        awsConf.setProxyHost(proxyHost);
        if (proxyPort >= 0) {
            awsConf.setProxyPort(proxyPort);
        } else {
            if (secureConnections) {
                LOG.warn("Proxy host set without port. Using HTTPS default 443");
                awsConf.setProxyPort(443);
            } else {
                LOG.warn("Proxy host set without port. Using HTTP default 80");
                awsConf.setProxyPort(80);
            }
        }
        String proxyUsername = conf.getTrimmed(PROXY_USERNAME);
        String proxyPassword = conf.getTrimmed(PROXY_PASSWORD);
        if ((proxyUsername == null) != (proxyPassword == null)) {
            String msg = "Proxy error: " + PROXY_USERNAME + " or " + PROXY_PASSWORD + " set without the other.";
            LOG.error(msg);
            throw new IllegalArgumentException(msg);
        }
        awsConf.setProxyUsername(proxyUsername);
        awsConf.setProxyPassword(proxyPassword);
        awsConf.setProxyDomain(conf.getTrimmed(PROXY_DOMAIN));
        awsConf.setProxyWorkstation(conf.getTrimmed(PROXY_WORKSTATION));
        if (LOG.isDebugEnabled()) {
            LOG.debug(
                    "Using proxy server {}:{} as user {} with password {} on " + "domain {} as workstation {}",
                    awsConf.getProxyHost(), awsConf.getProxyPort(), String.valueOf(awsConf.getProxyUsername()),
                    awsConf.getProxyPassword(), awsConf.getProxyDomain(), awsConf.getProxyWorkstation());
        }
    } else if (proxyPort >= 0) {
        String msg = "Proxy error: " + PROXY_PORT + " set without " + PROXY_HOST;
        LOG.error(msg);
        throw new IllegalArgumentException(msg);
    }

    s3 = new AmazonS3Client(credentials, awsConf);
    String endPoint = conf.getTrimmed(ENDPOINT, "");
    if (!endPoint.isEmpty()) {
        try {
            s3.setEndpoint(endPoint);
        } catch (IllegalArgumentException e) {
            String msg = "Incorrect endpoint: " + e.getMessage();
            LOG.error(msg);
            throw new IllegalArgumentException(msg, e);
        }
    }

    maxKeys = conf.getInt(MAX_PAGING_KEYS, DEFAULT_MAX_PAGING_KEYS);
    partSize = conf.getLong(MULTIPART_SIZE, DEFAULT_MULTIPART_SIZE);
    multiPartThreshold = conf.getInt(MIN_MULTIPART_THRESHOLD, DEFAULT_MIN_MULTIPART_THRESHOLD);

    if (partSize < 5 * 1024 * 1024) {
        LOG.error(MULTIPART_SIZE + " must be at least 5 MB");
        partSize = 5 * 1024 * 1024;
    }

    if (multiPartThreshold < 5 * 1024 * 1024) {
        LOG.error(MIN_MULTIPART_THRESHOLD + " must be at least 5 MB");
        multiPartThreshold = 5 * 1024 * 1024;
    }

    int maxThreads = conf.getInt(MAX_THREADS, DEFAULT_MAX_THREADS);
    int coreThreads = conf.getInt(CORE_THREADS, DEFAULT_CORE_THREADS);
    if (maxThreads == 0) {
        maxThreads = Runtime.getRuntime().availableProcessors() * 8;
    }
    if (coreThreads == 0) {
        coreThreads = Runtime.getRuntime().availableProcessors() * 8;
    }
    long keepAliveTime = conf.getLong(KEEPALIVE_TIME, DEFAULT_KEEPALIVE_TIME);
    LinkedBlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(
            maxThreads * conf.getInt(MAX_TOTAL_TASKS, DEFAULT_MAX_TOTAL_TASKS));
    threadPoolExecutor = new ThreadPoolExecutor(coreThreads, maxThreads, keepAliveTime, TimeUnit.SECONDS,
            workQueue, newDaemonThreadFactory("s3a-transfer-shared-"));
    threadPoolExecutor.allowCoreThreadTimeOut(true);

    TransferManagerConfiguration transferConfiguration = new TransferManagerConfiguration();
    transferConfiguration.setMinimumUploadPartSize(partSize);
    transferConfiguration.setMultipartUploadThreshold(multiPartThreshold);

    transfers = new TransferManager(s3, threadPoolExecutor);
    transfers.setConfiguration(transferConfiguration);

    String cannedACLName = conf.get(CANNED_ACL, DEFAULT_CANNED_ACL);
    if (!cannedACLName.isEmpty()) {
        cannedACL = CannedAccessControlList.valueOf(cannedACLName);
    } else {
        cannedACL = null;
    }

    if (!s3.doesBucketExist(bucket)) {
        throw new IOException("Bucket " + bucket + " does not exist");
    }

    boolean purgeExistingMultipart = conf.getBoolean(PURGE_EXISTING_MULTIPART,
            DEFAULT_PURGE_EXISTING_MULTIPART);
    long purgeExistingMultipartAge = conf.getLong(PURGE_EXISTING_MULTIPART_AGE,
            DEFAULT_PURGE_EXISTING_MULTIPART_AGE);

    if (purgeExistingMultipart) {
        Date purgeBefore = new Date(new Date().getTime() - purgeExistingMultipartAge * 1000);

        transfers.abortMultipartUploads(bucket, purgeBefore);
    }

    serverSideEncryptionAlgorithm = conf.get(SERVER_SIDE_ENCRYPTION_ALGORITHM);

    setConf(conf);
}

From source file:org.apache.heron.uploader.s3.S3Uploader.java

License:Apache License

@Override
public void initialize(Config config) {
    bucket = S3Context.bucket(config);
    String accessKey = S3Context.accessKey(config);
    String accessSecret = S3Context.secretKey(config);
    String awsProfile = S3Context.awsProfile(config);
    String proxy = S3Context.proxyUri(config);
    String endpoint = S3Context.uri(config);
    String customRegion = S3Context.region(config);
    AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard();

    if (Strings.isNullOrEmpty(bucket)) {
        throw new RuntimeException("Missing heron.uploader.s3.bucket config value");
    }/*  w w w. j  a v a 2s .c  o m*/

    // If an accessKey is specified, use it. Otherwise check if an aws profile
    // is specified. If neither was set just use the DefaultAWSCredentialsProviderChain
    // by not specifying a CredentialsProvider.
    if (!Strings.isNullOrEmpty(accessKey) || !Strings.isNullOrEmpty(accessSecret)) {

        if (!Strings.isNullOrEmpty(awsProfile)) {
            throw new RuntimeException("Please provide access_key/secret_key " + "or aws_profile, not both.");
        }

        if (Strings.isNullOrEmpty(accessKey)) {
            throw new RuntimeException("Missing heron.uploader.s3.access_key config value");
        }

        if (Strings.isNullOrEmpty(accessSecret)) {
            throw new RuntimeException("Missing heron.uploader.s3.secret_key config value");
        }
        builder.setCredentials(
                new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, accessSecret)));
    } else if (!Strings.isNullOrEmpty(awsProfile)) {
        builder.setCredentials(new ProfileCredentialsProvider(awsProfile));
    }

    if (!Strings.isNullOrEmpty(proxy)) {
        URI proxyUri;

        try {
            proxyUri = new URI(proxy);
        } catch (URISyntaxException e) {
            throw new RuntimeException("Invalid heron.uploader.s3.proxy_uri config value: " + proxy, e);
        }

        ClientConfiguration clientCfg = new ClientConfiguration();
        clientCfg.withProtocol(Protocol.HTTPS).withProxyHost(proxyUri.getHost())
                .withProxyPort(proxyUri.getPort());

        if (!Strings.isNullOrEmpty(proxyUri.getUserInfo())) {
            String[] info = proxyUri.getUserInfo().split(":", 2);
            clientCfg.setProxyUsername(info[0]);
            if (info.length > 1) {
                clientCfg.setProxyPassword(info[1]);
            }
        }

        builder.setClientConfiguration(clientCfg);
    }

    s3Client = builder.withRegion(customRegion).withPathStyleAccessEnabled(true)
            .withChunkedEncodingDisabled(true).withPayloadSigningEnabled(true).build();

    if (!Strings.isNullOrEmpty(endpoint)) {
        s3Client.setEndpoint(endpoint);
    }

    final String topologyName = Context.topologyName(config);
    final String topologyPackageLocation = Context.topologyPackageFile(config);

    pathPrefix = S3Context.pathPrefix(config);
    packageFileHandler = new File(topologyPackageLocation);

    // The path the packaged topology will be uploaded to
    remoteFilePath = generateS3Path(pathPrefix, topologyName, packageFileHandler.getName());

    // Generate the location of the backup file incase we need to revert the deploy
    previousVersionFilePath = generateS3Path(pathPrefix, topologyName,
            "previous_" + packageFileHandler.getName());
}

From source file:org.apache.storm.s3.output.UploaderFactory.java

License:Apache License

public static Uploader buildUploader(Map conf) {
    Protocol protocol = Protocol.HTTPS;
    String proxy = null;/*  w w w.j  ava2s .  c  om*/
    int proxyPort = 0;
    if (conf.containsKey(S3_PROTOCOL)) {
        protocol = Protocol.valueOf((String) conf.get(S3_PROTOCOL));
    }
    if (conf.containsKey(S3_PROXY)) {
        proxy = (String) conf.get(S3_PROXY);
    }
    if (conf.containsKey(S3_PROXY_PORT)) {
        proxyPort = ((Long) conf.get(S3_PROXY_PORT)).intValue();
    }
    AWSCredentialsProvider provider = new DefaultAWSCredentialsProviderChain();
    AWSCredentials credentials = provider.getCredentials();
    ClientConfiguration config = new ClientConfiguration().withProtocol(protocol);
    if (proxy != null) {
        config.withProxyHost(proxy);
    }
    if (proxyPort != 0) {
        config.withProxyPort(proxyPort);
    }
    AmazonS3 client = new AmazonS3Client(credentials, config);
    if (conf.containsKey(S3_ENDPOINT)) {
        client.setEndpoint((String) conf.get(S3_ENDPOINT));
    }
    return getUploader(conf, client);
}

From source file:org.apache.tajo.storage.s3.S3TableSpace.java

License:Apache License

@Override
public void init(TajoConf tajoConf) throws IOException {
    super.init(tajoConf);

    try {/*from   w w w .j  a v  a  2  s  .  c  o m*/
        // Try to get our credentials or just connect anonymously
        String accessKey = conf.get(ACCESS_KEY, null);
        String secretKey = conf.get(SECRET_KEY, null);

        String userInfo = uri.getUserInfo();
        if (userInfo != null) {
            int index = userInfo.indexOf(':');
            if (index != -1) {
                accessKey = userInfo.substring(0, index);
                secretKey = userInfo.substring(index + 1);
            } else {
                accessKey = userInfo;
            }
        }

        AWSCredentialsProviderChain credentials = new AWSCredentialsProviderChain(
                new BasicAWSCredentialsProvider(accessKey, secretKey), new InstanceProfileCredentialsProvider(),
                new AnonymousAWSCredentialsProvider());

        ClientConfiguration awsConf = new ClientConfiguration();
        awsConf.setMaxConnections(conf.getInt(MAXIMUM_CONNECTIONS, DEFAULT_MAXIMUM_CONNECTIONS));
        boolean secureConnections = conf.getBoolean(SECURE_CONNECTIONS, DEFAULT_SECURE_CONNECTIONS);
        awsConf.setProtocol(secureConnections ? Protocol.HTTPS : Protocol.HTTP);
        awsConf.setMaxErrorRetry(conf.getInt(MAX_ERROR_RETRIES, DEFAULT_MAX_ERROR_RETRIES));
        awsConf.setConnectionTimeout(conf.getInt(ESTABLISH_TIMEOUT, DEFAULT_ESTABLISH_TIMEOUT));
        awsConf.setSocketTimeout(conf.getInt(SOCKET_TIMEOUT, DEFAULT_SOCKET_TIMEOUT));

        String proxyHost = conf.getTrimmed(PROXY_HOST, "");
        int proxyPort = conf.getInt(PROXY_PORT, -1);
        if (!proxyHost.isEmpty()) {
            awsConf.setProxyHost(proxyHost);
            if (proxyPort >= 0) {
                awsConf.setProxyPort(proxyPort);
            } else {
                if (secureConnections) {
                    LOG.warn("Proxy host set without port. Using HTTPS default 443");
                    awsConf.setProxyPort(443);
                } else {
                    LOG.warn("Proxy host set without port. Using HTTP default 80");
                    awsConf.setProxyPort(80);
                }
            }
            String proxyUsername = conf.getTrimmed(PROXY_USERNAME);
            String proxyPassword = conf.getTrimmed(PROXY_PASSWORD);
            if ((proxyUsername == null) != (proxyPassword == null)) {
                String msg = "Proxy error: " + PROXY_USERNAME + " or " + PROXY_PASSWORD
                        + " set without the other.";
                LOG.error(msg);
            }
            awsConf.setProxyUsername(proxyUsername);
            awsConf.setProxyPassword(proxyPassword);
            awsConf.setProxyDomain(conf.getTrimmed(PROXY_DOMAIN));
            awsConf.setProxyWorkstation(conf.getTrimmed(PROXY_WORKSTATION));
            if (LOG.isDebugEnabled()) {
                LOG.debug(String.format(
                        "Using proxy server %s:%d as user %s with password %s on domain %s as workstation "
                                + "%s",
                        awsConf.getProxyHost(), awsConf.getProxyPort(), awsConf.getProxyUsername(),
                        awsConf.getProxyPassword(), awsConf.getProxyDomain(), awsConf.getProxyWorkstation()));
            }
        } else if (proxyPort >= 0) {
            String msg = "Proxy error: " + PROXY_PORT + " set without " + PROXY_HOST;
            LOG.error(msg);
        }

        s3 = new AmazonS3Client(credentials, awsConf);
        String endPoint = conf.getTrimmed(ENDPOINT, "");
        if (!endPoint.isEmpty()) {
            try {
                s3.setEndpoint(endPoint);
            } catch (IllegalArgumentException e) {
                String msg = "Incorrect endpoint: " + e.getMessage();
                LOG.error(msg);
            }
        }

        maxKeys = conf.getInt(MAX_PAGING_KEYS, DEFAULT_MAX_PAGING_KEYS);
        s3Enabled = true;
    } catch (NoClassDefFoundError e) {
        // If the version of hadoop is less than 2.6.0, hadoop doesn't include aws dependencies because it doesn't provide
        // S3AFileSystem. In this case, tajo never uses aws s3 api directly.
        LOG.warn(e);
        s3Enabled = false;
    } catch (Exception e) {
        throw new TajoInternalError(e);
    }
}

From source file:org.apache.usergrid.chop.api.store.amazon.AmazonUtils.java

License:Apache License

/**
 * @param accessKey//from www .j  av  a2 s  .  c o m
 * @param secretKey
 * @return
 */
public static AmazonEC2Client getEC2Client(String accessKey, String secretKey) {
    AWSCredentialsProvider provider;
    if (accessKey != null && secretKey != null) {
        AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
        provider = new StaticCredentialsProvider(credentials);
    } else {
        provider = new DefaultAWSCredentialsProviderChain();
    }

    AmazonEC2Client client = new AmazonEC2Client(provider);

    ClientConfiguration configuration = new ClientConfiguration();
    configuration.setProtocol(Protocol.HTTPS);
    client.setConfiguration(configuration);
    return client;
}

From source file:org.elasticsearch.cloud.aws.AwsEc2Service.java

License:Apache License

public synchronized AmazonEC2 client() {
    if (client != null) {
        return client;
    }//from  w  w w  .  j  a  v  a 2s.  c  o  m

    ClientConfiguration clientConfiguration = new ClientConfiguration();
    String protocol = componentSettings.get("protocol", "http").toLowerCase();
    if ("http".equals(protocol)) {
        clientConfiguration.setProtocol(Protocol.HTTP);
    } else if ("https".equals(protocol)) {
        clientConfiguration.setProtocol(Protocol.HTTPS);
    } else {
        throw new ElasticsearchIllegalArgumentException(
                "No protocol supported [" + protocol + "], can either be [http] or [https]");
    }
    String account = componentSettings.get("access_key", settings.get("cloud.account"));
    String key = componentSettings.get("secret_key", settings.get("cloud.key"));

    String proxyHost = componentSettings.get("proxy_host");
    if (proxyHost != null) {
        String portString = componentSettings.get("proxy_port", "80");
        Integer proxyPort;
        try {
            proxyPort = Integer.parseInt(portString, 10);
        } catch (NumberFormatException ex) {
            throw new ElasticsearchIllegalArgumentException(
                    "The configured proxy port value [" + portString + "] is invalid", ex);
        }
        clientConfiguration.withProxyHost(proxyHost).setProxyPort(proxyPort);
    }

    AWSCredentialsProvider credentials;

    if (account == null && key == null) {
        credentials = new AWSCredentialsProviderChain(new EnvironmentVariableCredentialsProvider(),
                new SystemPropertiesCredentialsProvider(), new InstanceProfileCredentialsProvider());
    } else {
        credentials = new AWSCredentialsProviderChain(
                new StaticCredentialsProvider(new BasicAWSCredentials(account, key)));
    }

    this.client = new AmazonEC2Client(credentials, clientConfiguration);

    if (componentSettings.get("ec2.endpoint") != null) {
        String endpoint = componentSettings.get("ec2.endpoint");
        logger.debug("using explicit ec2 endpoint [{}]", endpoint);
        client.setEndpoint(endpoint);
    } else if (componentSettings.get("region") != null) {
        String region = componentSettings.get("region").toLowerCase();
        String endpoint;
        if (region.equals("us-east-1") || region.equals("us-east")) {
            endpoint = "ec2.us-east-1.amazonaws.com";
        } else if (region.equals("us-west") || region.equals("us-west-1")) {
            endpoint = "ec2.us-west-1.amazonaws.com";
        } else if (region.equals("us-west-2")) {
            endpoint = "ec2.us-west-2.amazonaws.com";
        } else if (region.equals("ap-southeast") || region.equals("ap-southeast-1")) {
            endpoint = "ec2.ap-southeast-1.amazonaws.com";
        } else if (region.equals("ap-southeast-2")) {
            endpoint = "ec2.ap-southeast-2.amazonaws.com";
        } else if (region.equals("ap-northeast") || region.equals("ap-northeast-1")) {
            endpoint = "ec2.ap-northeast-1.amazonaws.com";
        } else if (region.equals("eu-west") || region.equals("eu-west-1")) {
            endpoint = "ec2.eu-west-1.amazonaws.com";
        } else if (region.equals("sa-east") || region.equals("sa-east-1")) {
            endpoint = "ec2.sa-east-1.amazonaws.com";
        } else {
            throw new ElasticsearchIllegalArgumentException(
                    "No automatic endpoint could be derived from region [" + region + "]");
        }
        if (endpoint != null) {
            logger.debug("using ec2 region [{}], with endpoint [{}]", region, endpoint);
            client.setEndpoint(endpoint);
        }
    }

    return this.client;

}

From source file:org.elasticsearch.cloud.aws.AwsS3Service.java

License:Apache License

private synchronized AmazonS3 getClient(String endpoint, String account, String key) {
    Tuple<String, String> clientDescriptor = new Tuple<String, String>(endpoint, account);
    AmazonS3Client client = clients.get(clientDescriptor);
    if (client != null) {
        return client;
    }//from  w  ww . j  ava2s  .c o  m

    ClientConfiguration clientConfiguration = new ClientConfiguration();
    String protocol = componentSettings.get("protocol", "http").toLowerCase();
    if ("http".equals(protocol)) {
        clientConfiguration.setProtocol(Protocol.HTTP);
    } else if ("https".equals(protocol)) {
        clientConfiguration.setProtocol(Protocol.HTTPS);
    } else {
        throw new ElasticsearchIllegalArgumentException(
                "No protocol supported [" + protocol + "], can either be [http] or [https]");
    }

    String proxyHost = componentSettings.get("proxy_host");
    if (proxyHost != null) {
        String portString = componentSettings.get("proxy_port", "80");
        Integer proxyPort;
        try {
            proxyPort = Integer.parseInt(portString, 10);
        } catch (NumberFormatException ex) {
            throw new ElasticsearchIllegalArgumentException(
                    "The configured proxy port value [" + portString + "] is invalid", ex);
        }
        clientConfiguration.withProxyHost(proxyHost).setProxyPort(proxyPort);
    }

    AWSCredentialsProvider credentials;

    if (account == null && key == null) {
        credentials = new AWSCredentialsProviderChain(new EnvironmentVariableCredentialsProvider(),
                new SystemPropertiesCredentialsProvider(), new InstanceProfileCredentialsProvider());
    } else {
        credentials = new AWSCredentialsProviderChain(
                new StaticCredentialsProvider(new BasicAWSCredentials(account, key)));
    }
    client = new AmazonS3Client(credentials, clientConfiguration);

    if (endpoint != null) {
        client.setEndpoint(endpoint);
    }
    clients.put(clientDescriptor, client);
    return client;
}

From source file:org.elasticsearch.cloud.aws.InternalAwsS3Service.java

License:Apache License

private synchronized AmazonS3 getClient(String endpoint, String protocol, String account, String key,
        Integer maxRetries) {//from   w w  w.java 2  s  .  c o  m
    Tuple<String, String> clientDescriptor = new Tuple<String, String>(endpoint, account);
    AmazonS3Client client = clients.get(clientDescriptor);
    if (client != null) {
        return client;
    }

    ClientConfiguration clientConfiguration = new ClientConfiguration();
    // the response metadata cache is only there for diagnostics purposes,
    // but can force objects from every response to the old generation.
    clientConfiguration.setResponseMetadataCacheSize(0);
    if (protocol == null) {
        protocol = settings.get("cloud.aws.protocol", "https").toLowerCase();
        protocol = settings.get("cloud.aws.s3.protocol", protocol).toLowerCase();
    }

    if ("http".equals(protocol)) {
        clientConfiguration.setProtocol(Protocol.HTTP);
    } else if ("https".equals(protocol)) {
        clientConfiguration.setProtocol(Protocol.HTTPS);
    } else {
        throw new IllegalArgumentException(
                "No protocol supported [" + protocol + "], can either be [http] or [https]");
    }

    String proxyHost = settings.get("cloud.aws.proxy_host");
    proxyHost = settings.get("cloud.aws.s3.proxy_host", proxyHost);
    if (proxyHost != null) {
        String portString = settings.get("cloud.aws.proxy_port", "80");
        portString = settings.get("cloud.aws.s3.proxy_port", portString);
        Integer proxyPort;
        try {
            proxyPort = Integer.parseInt(portString, 10);
        } catch (NumberFormatException ex) {
            throw new IllegalArgumentException(
                    "The configured proxy port value [" + portString + "] is invalid", ex);
        }
        clientConfiguration.withProxyHost(proxyHost).setProxyPort(proxyPort);
    }

    if (maxRetries != null) {
        // If not explicitly set, default to 3 with exponential backoff policy
        clientConfiguration.setMaxErrorRetry(maxRetries);
    }

    // #155: we might have 3rd party users using older S3 API version
    String awsSigner = settings.get("cloud.aws.s3.signer", settings.get("cloud.aws.signer"));
    if (awsSigner != null) {
        logger.debug("using AWS API signer [{}]", awsSigner);
        try {
            AwsSigner.configureSigner(awsSigner, clientConfiguration);
        } catch (IllegalArgumentException e) {
            logger.warn("wrong signer set for [cloud.aws.s3.signer] or [cloud.aws.signer]: [{}]", awsSigner);
        }
    }

    AWSCredentialsProvider credentials;

    if (account == null && key == null) {
        credentials = new AWSCredentialsProviderChain(new EnvironmentVariableCredentialsProvider(),
                new SystemPropertiesCredentialsProvider(), new InstanceProfileCredentialsProvider());
    } else {
        credentials = new AWSCredentialsProviderChain(
                new StaticCredentialsProvider(new BasicAWSCredentials(account, key)));
    }
    client = new AmazonS3Client(credentials, clientConfiguration);

    if (endpoint != null) {
        client.setEndpoint(endpoint);
    }
    clients.put(clientDescriptor, client);
    return client;
}

From source file:org.geowebcache.s3.S3BlobStoreConfig.java

License:Open Source License

/**
 * @return {@link AmazonS3Client} constructed from this {@link S3BlobStoreConfig}.
 *//*from  w w  w  .j  ava  2s.c  om*/
public AmazonS3Client buildClient() {
    AWSCredentials awsCredentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
    ClientConfiguration clientConfig = new ClientConfiguration();
    if (null != useHTTPS) {
        clientConfig.setProtocol(useHTTPS ? Protocol.HTTPS : Protocol.HTTP);
    }
    if (null != maxConnections && maxConnections > 0) {
        clientConfig.setMaxConnections(maxConnections);
    }
    clientConfig.setProxyDomain(proxyDomain);
    clientConfig.setProxyWorkstation(proxyWorkstation);
    clientConfig.setProxyHost(proxyHost);
    if (null != proxyPort) {
        clientConfig.setProxyPort(proxyPort);
    }
    clientConfig.setProxyUsername(proxyUsername);
    clientConfig.setProxyPassword(proxyPassword);
    if (null != useGzip) {
        clientConfig.setUseGzip(useGzip);
    }
    log.debug("Initializing AWS S3 connection");
    return new AmazonS3Client(awsCredentials, clientConfig);
}