Java Code Examples for com.amazonaws.services.s3.AmazonS3ClientBuilder#standard()

The following examples show how to use com.amazonaws.services.s3.AmazonS3ClientBuilder#standard() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: S3.java    From rdf-delta with Apache License 2.0 6 votes vote down vote up
public static AmazonS3 buildS3(LocalServerConfig configuration) {
    String region = configuration.getProperty(pRegion);
    String endpoint = configuration.getProperty(pEndpoint);
    String credentialsFile =  configuration.getProperty(pCredentialFile);
    String credentialsProfile =  configuration.getProperty(pCredentialProfile);

    AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard();
    if ( endpoint == null )
        builder.withRegion(region);
    else  {
        // Needed for S3mock
        builder.withPathStyleAccessEnabled(true);
        builder.withEndpointConfiguration(new EndpointConfiguration(endpoint, region));
        builder.withCredentials(new AWSStaticCredentialsProvider(new AnonymousAWSCredentials()));
    }
    if ( credentialsFile != null )
        builder.withCredentials(new ProfileCredentialsProvider(credentialsFile, credentialsProfile));
    return builder.build();
}
 
Example 2
Source File: S3Service.java    From crate with Apache License 2.0 6 votes vote down vote up
private AmazonS3 buildClient(final S3ClientSettings clientSettings) {
    final AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard();
    builder.withCredentials(buildCredentials(LOGGER, clientSettings));
    builder.withClientConfiguration(buildConfiguration(clientSettings));

    final String endpoint = Strings.hasLength(clientSettings.endpoint)
        ? clientSettings.endpoint
        : Constants.S3_HOSTNAME;
    LOGGER.debug("using endpoint [{}]", endpoint);

    // If the endpoint configuration isn't set on the builder then the default behaviour is to try
    // and work out what region we are in and use an appropriate endpoint - see AwsClientBuilder#setRegion.
    // In contrast, directly-constructed clients use s3.amazonaws.com unless otherwise instructed. We currently
    // use a directly-constructed client, and need to keep the existing behaviour to avoid a breaking change,
    // so to move to using the builder we must set it explicitly to keep the existing behaviour.
    //
    // We do this because directly constructing the client is deprecated (was already deprecated in 1.1.223 too)
    // so this change removes that usage of a deprecated API.
    builder.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, null));

    return builder.build();
}
 
Example 3
Source File: AWSClientUtils.java    From camel-kafka-connector with Apache License 2.0 5 votes vote down vote up
public static AmazonS3 newS3Client() {
    LOG.debug("Creating a new S3 client");
    AmazonS3ClientBuilder clientBuilder = AmazonS3ClientBuilder.standard();

    String awsInstanceType = System.getProperty("aws-service.instance.type");
    String region = getRegion();

    if (awsInstanceType == null || awsInstanceType.equals("local-aws-container")) {
        String amazonHost = System.getProperty(AWSConfigs.AMAZON_AWS_HOST);
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setProtocol(Protocol.HTTP);

        clientBuilder
                .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(amazonHost, region))
                .withClientConfiguration(clientConfiguration)
                .withCredentials(new TestAWSCredentialsProvider("accesskey", "secretkey"));
    } else {
        clientBuilder
                .withRegion(region)
                .withCredentials(new TestAWSCredentialsProvider());
    }

    clientBuilder
            .withPathStyleAccessEnabled(true);

    return clientBuilder.build();
}
 
Example 4
Source File: AwsS3BuildCacheServiceFactory.java    From gradle-s3-build-cache with Apache License 2.0 5 votes vote down vote up
private AmazonS3 createS3Client(AwsS3BuildCache config) {
  AmazonS3 s3;
  try {
    AmazonS3ClientBuilder s3Builder = AmazonS3ClientBuilder.standard();
    if (!isNullOrEmpty(config.getAwsAccessKeyId()) && !isNullOrEmpty(config.getAwsSecretKey()) &&
              !isNullOrEmpty(config.getSessionToken())) {
          s3Builder.withCredentials(new AWSStaticCredentialsProvider(
                  new BasicSessionCredentials(config.getAwsAccessKeyId(), config.getAwsSecretKey(),
                          config.getSessionToken())));
    } else if (!isNullOrEmpty(config.getAwsAccessKeyId()) && !isNullOrEmpty(config.getAwsSecretKey())) {
      s3Builder.withCredentials(new AWSStaticCredentialsProvider(
          new BasicAWSCredentials(config.getAwsAccessKeyId(), config.getAwsSecretKey())));
    }

    addHttpHeaders(s3Builder, config);

    if (isNullOrEmpty(config.getEndpoint())) {
      s3Builder.withRegion(config.getRegion());
    } else {
      s3Builder.withEndpointConfiguration(
          new AwsClientBuilder.EndpointConfiguration(config.getEndpoint(), config.getRegion()));
    }
    s3 = s3Builder.build();
  } catch (SdkClientException e) {
    logger.debug("Error while building AWS S3 client: {}", e.getMessage());
    throw new GradleException("Creation of S3 build cache failed; cannot create S3 client", e);
  }
  return s3;
}
 
Example 5
Source File: S3Service.java    From fullstop with Apache License 2.0 5 votes vote down vote up
public S3Service(@Value("${fullstop.processor.properties.s3Region:#{null}}") String s3Region) {
    final AmazonS3ClientBuilder s3Builder = AmazonS3ClientBuilder.standard();
    if (StringUtils.hasText(s3Region)) {
        s3Builder.setRegion(s3Region);
    }
    s3client = s3Builder.build();
}
 
Example 6
Source File: S3ArtifactStore.java    From gocd-s3-artifacts with Apache License 2.0 5 votes vote down vote up
public static AmazonS3 getS3client(GoEnvironment env) {
    AmazonS3ClientBuilder amazonS3ClientBuilder = AmazonS3ClientBuilder.standard();

    if (env.has(AWS_REGION)) {
        amazonS3ClientBuilder.withRegion(env.get(AWS_REGION));
    }
    if (env.hasAWSUseIamRole()) {
        amazonS3ClientBuilder.withCredentials(new InstanceProfileCredentialsProvider(false));
    } else if (env.has(AWS_ACCESS_KEY_ID) && env.has(AWS_SECRET_ACCESS_KEY)) {
        BasicAWSCredentials basicCreds = new BasicAWSCredentials(env.get(AWS_ACCESS_KEY_ID), env.get(AWS_SECRET_ACCESS_KEY));
        amazonS3ClientBuilder.withCredentials(new AWSStaticCredentialsProvider(basicCreds));
    }

    return amazonS3ClientBuilder.build();
}
 
Example 7
Source File: AwsS3EnvironmentRepositoryFactory.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Override
public AwsS3EnvironmentRepository build(
		AwsS3EnvironmentProperties environmentProperties) {
	final AmazonS3ClientBuilder clientBuilder = AmazonS3ClientBuilder.standard();
	if (environmentProperties.getRegion() != null) {
		clientBuilder.withRegion(environmentProperties.getRegion());
	}
	final AmazonS3 client = clientBuilder.build();
	if (environmentProperties.getEndpoint() != null) {
		client.setEndpoint(environmentProperties.getEndpoint());
	}
	AwsS3EnvironmentRepository repository = new AwsS3EnvironmentRepository(client,
			environmentProperties.getBucket(), server);
	return repository;
}
 
Example 8
Source File: AmazonS3ClientFactory.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
private AmazonS3ClientBuilder buildAmazonS3ForRegion(AmazonS3 prototype,
		String region) {
	AmazonS3ClientBuilder clientBuilder = AmazonS3ClientBuilder.standard();

	AmazonS3Client target = getAmazonS3ClientFromProxy(prototype);
	if (target != null) {
		AWSCredentialsProvider awsCredentialsProvider = (AWSCredentialsProvider) ReflectionUtils
				.getField(this.credentialsProviderField, target);
		clientBuilder.withCredentials(awsCredentialsProvider);
	}

	return clientBuilder.withRegion(region);
}
 
Example 9
Source File: S3ArtifactStore.java    From gocd-s3-artifacts with Apache License 2.0 5 votes vote down vote up
public static AmazonS3 getS3client(GoEnvironment env) {
    AmazonS3ClientBuilder amazonS3ClientBuilder = AmazonS3ClientBuilder.standard();

    if (env.has(AWS_REGION)) {
        amazonS3ClientBuilder.withRegion(env.get(AWS_REGION));
    }
    if (env.hasAWSUseIamRole()) {
        amazonS3ClientBuilder.withCredentials(new InstanceProfileCredentialsProvider(false));
    } else if (env.has(AWS_ACCESS_KEY_ID) && env.has(AWS_SECRET_ACCESS_KEY)) {
        BasicAWSCredentials basicCreds = new BasicAWSCredentials(env.get(AWS_ACCESS_KEY_ID), env.get(AWS_SECRET_ACCESS_KEY));
        amazonS3ClientBuilder.withCredentials(new AWSStaticCredentialsProvider(basicCreds));
    }

    return amazonS3ClientBuilder.build();
}
 
Example 10
Source File: S3Module.java    From cassandra-backup with Apache License 2.0 4 votes vote down vote up
private AmazonS3 provideAmazonS3(final Provider<CoreV1Api> coreV1ApiProvider, final AbstractOperationRequest operationRequest) {

            final S3Configuration s3Conf = resolveS3Configuration(coreV1ApiProvider, operationRequest);

            final AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard();

            if (s3Conf.awsEndpoint != null) {
                // AWS_REGION must be set if AWS_ENDPOINT is set
                if (s3Conf.awsRegion == null) {
                    throw new IllegalArgumentException("AWS_REGION must be set if AWS_ENDPOINT is set.");
                }

                builder.withEndpointConfiguration(new EndpointConfiguration(s3Conf.awsEndpoint, s3Conf.awsRegion.toLowerCase()));
            } else if (s3Conf.awsRegion != null) {
                builder.withRegion(Regions.fromName(s3Conf.awsRegion.toLowerCase()));
            }

            // if we are not running against Kubernetes, credentials should be fetched from ~/.aws/...
            if (isRunningInKubernetes()) {
                // it is possible that we have not set any secrets for s3 so the last
                // resort is to fallback to AWS instance credentials.
                if (s3Conf.awsAccessKeyId != null && s3Conf.awsSecretKey != null) {
                    builder.setCredentials(new AWSCredentialsProvider() {
                        @Override
                        public AWSCredentials getCredentials() {
                            return new AWSCredentials() {
                                @Override
                                public String getAWSAccessKeyId() {
                                    return s3Conf.awsAccessKeyId;
                                }

                                @Override
                                public String getAWSSecretKey() {
                                    return s3Conf.awsSecretKey;
                                }
                            };
                        }

                        @Override
                        public void refresh() {
                        }
                    });
                }
            }

            return builder.build();
        }
 
Example 11
Source File: AwsConfiguration.java    From kayenta with Apache License 2.0 4 votes vote down vote up
@Bean
boolean registerAwsCredentials(
    AwsConfigurationProperties awsConfigurationProperties,
    AccountCredentialsRepository accountCredentialsRepository)
    throws IOException {
  for (AwsManagedAccount awsManagedAccount : awsConfigurationProperties.getAccounts()) {
    String name = awsManagedAccount.getName();
    List<AccountCredentials.Type> supportedTypes = awsManagedAccount.getSupportedTypes();

    log.info("Registering AWS account {} with supported types {}.", name, supportedTypes);

    ClientConfiguration clientConfiguration = new ClientConfiguration();

    if (awsManagedAccount.getProxyProtocol() != null) {
      if (awsManagedAccount.getProxyProtocol().equalsIgnoreCase("HTTPS")) {
        clientConfiguration.setProtocol(Protocol.HTTPS);
      } else {
        clientConfiguration.setProtocol(Protocol.HTTP);
      }
      Optional.ofNullable(awsManagedAccount.getProxyHost())
          .ifPresent(clientConfiguration::setProxyHost);
      Optional.ofNullable(awsManagedAccount.getProxyPort())
          .map(Integer::parseInt)
          .ifPresent(clientConfiguration::setProxyPort);
    }

    AmazonS3ClientBuilder amazonS3ClientBuilder = AmazonS3ClientBuilder.standard();
    String profileName = awsManagedAccount.getProfileName();

    if (!StringUtils.isEmpty(profileName)) {
      amazonS3ClientBuilder.withCredentials(new ProfileCredentialsProvider(profileName));
    }

    AwsManagedAccount.ExplicitAwsCredentials explicitCredentials =
        awsManagedAccount.getExplicitCredentials();
    if (explicitCredentials != null) {
      String sessionToken = explicitCredentials.getSessionToken();
      AWSCredentials awsCreds =
          (sessionToken == null)
              ? new BasicAWSCredentials(
                  explicitCredentials.getAccessKey(), explicitCredentials.getSecretKey())
              : new BasicSessionCredentials(
                  explicitCredentials.getAccessKey(),
                  explicitCredentials.getSecretKey(),
                  sessionToken);
      amazonS3ClientBuilder.withCredentials(new AWSStaticCredentialsProvider(awsCreds));
    }

    String endpoint = awsManagedAccount.getEndpoint();

    if (!StringUtils.isEmpty(endpoint)) {
      amazonS3ClientBuilder.setEndpointConfiguration(
          new AwsClientBuilder.EndpointConfiguration(endpoint, null));
      amazonS3ClientBuilder.setPathStyleAccessEnabled(true);
    } else {
      Optional.ofNullable(awsManagedAccount.getRegion())
          .ifPresent(amazonS3ClientBuilder::setRegion);
    }

    AmazonS3 amazonS3 = amazonS3ClientBuilder.build();

    try {
      AwsCredentials awsCredentials = new AwsCredentials();
      AwsNamedAccountCredentials.AwsNamedAccountCredentialsBuilder
          awsNamedAccountCredentialsBuilder =
              AwsNamedAccountCredentials.builder().name(name).credentials(awsCredentials);

      if (!CollectionUtils.isEmpty(supportedTypes)) {
        if (supportedTypes.contains(AccountCredentials.Type.OBJECT_STORE)) {
          String bucket = awsManagedAccount.getBucket();
          String rootFolder = awsManagedAccount.getRootFolder();

          if (StringUtils.isEmpty(bucket)) {
            throw new IllegalArgumentException(
                "AWS/S3 account " + name + " is required to specify a bucket.");
          }

          if (StringUtils.isEmpty(rootFolder)) {
            throw new IllegalArgumentException(
                "AWS/S3 account " + name + " is required to specify a rootFolder.");
          }

          awsNamedAccountCredentialsBuilder.bucket(bucket);
          awsNamedAccountCredentialsBuilder.region(awsManagedAccount.getRegion());
          awsNamedAccountCredentialsBuilder.rootFolder(rootFolder);
          awsNamedAccountCredentialsBuilder.amazonS3(amazonS3);
        }

        awsNamedAccountCredentialsBuilder.supportedTypes(supportedTypes);
      }

      AwsNamedAccountCredentials awsNamedAccountCredentials =
          awsNamedAccountCredentialsBuilder.build();
      accountCredentialsRepository.save(name, awsNamedAccountCredentials);
    } catch (Throwable t) {
      log.error("Could not load AWS account " + name + ".", t);
    }
  }

  return true;
}
 
Example 12
Source File: S3Accessor.java    From datacollector with Apache License 2.0 4 votes vote down vote up
AmazonS3ClientBuilder createAmazonS3ClientBuilder() {
  return AmazonS3ClientBuilder.standard();
}
 
Example 13
Source File: EndToEndTest.java    From ecs-sync with Apache License 2.0 4 votes vote down vote up
@Test
public void testS3() throws Exception {
    Properties syncProperties = com.emc.ecs.sync.test.TestConfig.getProperties();
    final String bucket = "ecs-sync-s3-test-bucket";
    final String endpoint = syncProperties.getProperty(com.emc.ecs.sync.test.TestConfig.PROP_S3_ENDPOINT);
    final String accessKey = syncProperties.getProperty(com.emc.ecs.sync.test.TestConfig.PROP_S3_ACCESS_KEY_ID);
    final String secretKey = syncProperties.getProperty(com.emc.ecs.sync.test.TestConfig.PROP_S3_SECRET_KEY);
    final String region = syncProperties.getProperty(com.emc.ecs.sync.test.TestConfig.PROP_S3_REGION);
    Assume.assumeNotNull(endpoint, accessKey, secretKey);
    URI endpointUri = new URI(endpoint);

    ClientConfiguration config = new ClientConfiguration().withSignerOverride("S3SignerType");
    AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard();
    builder.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)));
    builder.withClientConfiguration(config);
    builder.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, region));

    AmazonS3 s3 = builder.build();
    try {
        s3.createBucket(bucket);
    } catch (AmazonServiceException e) {
        if (!e.getErrorCode().equals("BucketAlreadyExists")) throw e;
    }

    // for testing ACLs
    String authUsers = "http://acs.amazonaws.com/groups/global/AuthenticatedUsers";
    String everyone = "http://acs.amazonaws.com/groups/global/AllUsers";
    String[] validGroups = {authUsers, everyone};
    String[] validPermissions = {"READ", "WRITE", "FULL_CONTROL"};

    AwsS3Config awsS3Config = new AwsS3Config();
    if (endpointUri.getScheme() != null)
        awsS3Config.setProtocol(Protocol.valueOf(endpointUri.getScheme().toLowerCase()));
    awsS3Config.setHost(endpointUri.getHost());
    awsS3Config.setPort(endpointUri.getPort());
    awsS3Config.setAccessKey(accessKey);
    awsS3Config.setSecretKey(secretKey);
    awsS3Config.setRegion(region);
    awsS3Config.setLegacySignatures(true);
    awsS3Config.setDisableVHosts(true);
    awsS3Config.setBucketName(bucket);
    awsS3Config.setPreserveDirectories(true);

    TestConfig testConfig = new TestConfig();
    testConfig.setObjectOwner(accessKey);
    testConfig.setValidGroups(validGroups);
    testConfig.setValidPermissions(validPermissions);

    try {
        multiEndToEndTest(awsS3Config, testConfig, true);
    } finally {
        try {
            s3.deleteBucket(bucket);
        } catch (Throwable t) {
            log.warn("could not delete bucket", t);
        }
    }
}