Example usage for com.amazonaws.services.s3 AmazonS3Builder setForceGlobalBucketAccessEnabled

List of usage examples for com.amazonaws.services.s3 AmazonS3Builder setForceGlobalBucketAccessEnabled

Introduction

In this page you can find the example usage for com.amazonaws.services.s3 AmazonS3Builder setForceGlobalBucketAccessEnabled.

Prototype

public void setForceGlobalBucketAccessEnabled(Boolean forceGlobalBucketAccessEnabled) 

Source Link

Document

Configure whether global bucket access is enabled for clients generated by this builder.

When global bucket access is enabled, the region to which a request is routed may differ from the region that is configured in #setRegion(String) in order to make the request succeed.

The following behavior is currently used when this mode is enabled:

  1. All requests that do not act on an existing bucket (for example, AmazonS3Client#createBucket(String) ) will be routed to the region configured by #setRegion(String) , unless the region is manually overridden with CreateBucketRequest#setRegion(String) , in which case the request will be routed to the region configured in the request.
  2. The first time a request is made that references an existing bucket (for example, AmazonS3Client#putObject(PutObjectRequest) ) a request will be made to the region configured by #setRegion(String) to determine the region in which the bucket was created.

    Usage

    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;
        }/* w  w  w.  j  a  v a2 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:io.prestosql.plugin.hive.s3.PrestoS3FileSystem.java

    License:Apache License

    private AmazonS3 createAmazonS3Client(Configuration hadoopConfig, ClientConfiguration clientConfig) {
        Optional<EncryptionMaterialsProvider> encryptionMaterialsProvider = createEncryptionMaterialsProvider(
                hadoopConfig);//w w w  .jav a  2  s.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();
    }