Example usage for com.amazonaws.services.s3 AmazonS3Client builder

List of usage examples for com.amazonaws.services.s3 AmazonS3Client builder

Introduction

In this page you can find the example usage for com.amazonaws.services.s3 AmazonS3Client builder.

Prototype

public static AmazonS3ClientBuilder builder() 

Source Link

Usage

From source file:com.adeptj.modules.aws.s3.internal.AwsS3Service.java

License:Apache License

@Activate
protected void start(S3Config s3Config) {
    try {/*from  w  w w . j  av a2  s  . c o  m*/
        this.s3Client = AmazonS3Client.builder()
                .withEndpointConfiguration(
                        AwsUtil.getEndpointConfig(s3Config.serviceEndpoint(), s3Config.signingRegion()))
                .withCredentials(AwsUtil.getCredentialsProvider(s3Config.accessKey(), s3Config.secretKey()))
                .build();
    } catch (Throwable ex) { //NOSONAR
        LOGGER.error("Exception while creating S3 client!!", ex);
    }
}

From source file:com.facebook.presto.hive.s3.PrestoS3ClientFactory.java

License:Apache License

synchronized AmazonS3 getS3Client(Configuration config, HiveClientConfig clientConfig) {
    if (s3Client != null) {
        return s3Client;
    }//from w w w  . jav a 2 s. c  o  m

    HiveS3Config defaults = new HiveS3Config();
    String userAgentPrefix = config.get(S3_USER_AGENT_PREFIX, defaults.getS3UserAgentPrefix());
    int maxErrorRetries = config.getInt(S3_MAX_ERROR_RETRIES, defaults.getS3MaxErrorRetries());
    boolean sslEnabled = config.getBoolean(S3_SSL_ENABLED, defaults.isS3SslEnabled());
    Duration connectTimeout = Duration
            .valueOf(config.get(S3_CONNECT_TIMEOUT, defaults.getS3ConnectTimeout().toString()));
    Duration socketTimeout = Duration
            .valueOf(config.get(S3_SOCKET_TIMEOUT, defaults.getS3SocketTimeout().toString()));
    int maxConnections = config.getInt(S3_SELECT_PUSHDOWN_MAX_CONNECTIONS,
            clientConfig.getS3SelectPushdownMaxConnections());

    if (clientConfig.isS3SelectPushdownEnabled()) {
        s3UserAgentSuffix = "presto-select";
    }

    ClientConfiguration clientConfiguration = new ClientConfiguration().withMaxErrorRetry(maxErrorRetries)
            .withProtocol(sslEnabled ? Protocol.HTTPS : Protocol.HTTP)
            .withConnectionTimeout(toIntExact(connectTimeout.toMillis()))
            .withSocketTimeout(toIntExact(socketTimeout.toMillis())).withMaxConnections(maxConnections)
            .withUserAgentPrefix(userAgentPrefix).withUserAgentSuffix(s3UserAgentSuffix);

    PrestoS3FileSystemStats stats = new PrestoS3FileSystemStats();
    RequestMetricCollector metricCollector = new PrestoS3FileSystemMetricCollector(stats);
    AWSCredentialsProvider awsCredentialsProvider = getAwsCredentialsProvider(config, defaults);
    AmazonS3Builder<? extends AmazonS3Builder, ? extends AmazonS3> clientBuilder = AmazonS3Client.builder()
            .withCredentials(awsCredentialsProvider).withClientConfiguration(clientConfiguration)
            .withMetricsCollector(metricCollector).enablePathStyleAccess();

    boolean regionOrEndpointSet = false;

    String endpoint = config.get(S3_ENDPOINT);
    boolean pinS3ClientToCurrentRegion = config.getBoolean(S3_PIN_CLIENT_TO_CURRENT_REGION,
            defaults.isPinS3ClientToCurrentRegion());
    verify(!pinS3ClientToCurrentRegion || endpoint == null,
            "Invalid configuration: either endpoint can be set or S3 client can be pinned to the current region");

    // use local region when running inside of EC2
    if (pinS3ClientToCurrentRegion) {
        Region region = Regions.getCurrentRegion();
        if (region != null) {
            clientBuilder.withRegion(region.getName());
            regionOrEndpointSet = true;
        }
    }

    if (!isNullOrEmpty(endpoint)) {
        clientBuilder.withEndpointConfiguration(new EndpointConfiguration(endpoint, null));
        regionOrEndpointSet = true;
    }

    if (!regionOrEndpointSet) {
        clientBuilder.withRegion(US_EAST_1);
        clientBuilder.setForceGlobalBucketAccessEnabled(true);
    }

    s3Client = clientBuilder.build();
    return s3Client;
}

From source file:com.github.kaklakariada.aws.sam.task.S3UploadTask.java

License:Open Source License

private AmazonS3 getS3Client() {
    if (s3Client == null) {
        s3Client = config.getAwsClientFactory().create(AmazonS3Client.builder());
    }
    return s3Client;
}

From source file:com.vitembp.services.interfaces.AmazonSimpleStorageService.java

License:Open Source License

/**
 * Initializes a new instance of the AmazonSQS class.
 * @param bucketName The name of the queue to connect to.
 *///  w  w  w .j a v a  2  s.c o m
public AmazonSimpleStorageService(String bucketName) {
    // save parameters
    this.bucketName = bucketName;

    // builds a client with credentials
    AmazonS3 client = AmazonS3Client.builder().build();

    // builds a transfer manager from the client
    this.transferManager = TransferManagerBuilder.standard().withS3Client(client).build();
}

From source file:io.prestosql.plugin.hive.s3.PrestoS3FileSystem.java

License:Apache License

private AmazonS3 createAmazonS3Client(Configuration hadoopConfig, ClientConfiguration clientConfig) {
    Optional<EncryptionMaterialsProvider> encryptionMaterialsProvider = createEncryptionMaterialsProvider(
            hadoopConfig);/*from  www  .ja va  2s  .  c  o  m*/
    AmazonS3Builder<? extends AmazonS3Builder, ? extends AmazonS3> clientBuilder;

    String signerType = hadoopConfig.get(S3_SIGNER_TYPE);
    if (signerType != null) {
        clientConfig.withSignerOverride(signerType);
    }

    if (encryptionMaterialsProvider.isPresent()) {
        clientBuilder = AmazonS3EncryptionClient.encryptionBuilder().withCredentials(credentialsProvider)
                .withEncryptionMaterials(encryptionMaterialsProvider.get())
                .withClientConfiguration(clientConfig).withMetricsCollector(METRIC_COLLECTOR);
    } else {
        clientBuilder = AmazonS3Client.builder().withCredentials(credentialsProvider)
                .withClientConfiguration(clientConfig).withMetricsCollector(METRIC_COLLECTOR);
    }

    boolean regionOrEndpointSet = false;

    // use local region when running inside of EC2
    if (pinS3ClientToCurrentRegion) {
        Region region = Regions.getCurrentRegion();
        if (region != null) {
            clientBuilder = clientBuilder.withRegion(region.getName());
            regionOrEndpointSet = true;
        }
    }

    String endpoint = hadoopConfig.get(S3_ENDPOINT);
    if (endpoint != null) {
        clientBuilder = clientBuilder.withEndpointConfiguration(new EndpointConfiguration(endpoint, null));
        regionOrEndpointSet = true;
    }

    if (isPathStyleAccess) {
        clientBuilder = clientBuilder.enablePathStyleAccess();
    }

    if (!regionOrEndpointSet) {
        clientBuilder = clientBuilder.withRegion(US_EAST_1);
        clientBuilder.setForceGlobalBucketAccessEnabled(true);
    }

    return clientBuilder.build();
}

From source file:org.apache.druid.storage.s3.S3StorageDruidModule.java

License:Apache License

@Provides
@LazySingleton/*from w  ww . j  a  va 2  s. c o  m*/
public ServerSideEncryptingAmazonS3 getAmazonS3Client(AWSCredentialsProvider provider,
        AWSProxyConfig proxyConfig, AWSEndpointConfig endpointConfig, AWSClientConfig clientConfig,
        S3StorageConfig storageConfig) {
    final ClientConfiguration configuration = new ClientConfigurationFactory().getConfig();
    final Protocol protocol = determineProtocol(clientConfig, endpointConfig);
    final AmazonS3ClientBuilder builder = AmazonS3Client.builder().withCredentials(provider)
            .withClientConfiguration(setProxyConfig(configuration, proxyConfig).withProtocol(protocol))
            .withChunkedEncodingDisabled(clientConfig.isDisableChunkedEncoding())
            .withPathStyleAccessEnabled(clientConfig.isEnablePathStyleAccess())
            .withForceGlobalBucketAccessEnabled(clientConfig.isForceGlobalBucketAccessEnabled());

    if (StringUtils.isNotEmpty(endpointConfig.getUrl())) {
        builder.setEndpointConfiguration(
                new EndpointConfiguration(endpointConfig.getUrl(), endpointConfig.getSigningRegion()));
    }

    return new ServerSideEncryptingAmazonS3(builder.build(), storageConfig.getServerSideEncryption());
}

From source file:org.apache.nifi.minifi.c2.cache.s3.S3ConfigurationCache.java

License:Apache License

/**
 * Creates a new S3 configuration cache.
 * @param bucket The S3 bucket./*from   w w w  .  j a va  2s.co m*/
 * @param prefix The S3 object prefix.
 * @param pathPattern The path pattern.
 * @param accessKey The (optional) S3 access key.
 * @param secretKey The (optional) S3 secret key.
 * @param region The AWS region (e.g. us-east-1).
 * @throws IOException Thrown if the configuration cannot be read.
 */
public S3ConfigurationCache(String bucket, String prefix, String pathPattern, String accessKey,
        String secretKey, String region) throws IOException {

    this.bucket = bucket;
    this.prefix = prefix;
    this.pathPattern = pathPattern;

    if (!StringUtils.isEmpty(accessKey)) {

        s3 = AmazonS3Client.builder()
                .withCredentials(
                        new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
                .withRegion(Regions.fromName(region)).build();

    } else {

        s3 = AmazonS3Client.builder().withRegion(Regions.fromName(region)).build();
    }

}