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

The following examples show how to use com.amazonaws.services.s3.AmazonS3ClientBuilder#withCredentials() . 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: 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 4
Source File: AmazonS3Configuration.java    From mojito with Apache License 2.0 5 votes vote down vote up
@Bean
public AmazonS3 amazonS3Client(AmazonS3ConfigurationProperties amazonS3ConfigurationProperties) {
     AmazonS3ClientBuilder amazonS3ClientBuilder = AmazonS3ClientBuilder
            .standard()
            .withRegion(Regions.fromName(amazonS3ConfigurationProperties.getRegion()));

    if (amazonS3ConfigurationProperties.getAccessKeyId() != null) {
        AWSCredentials credentials = new BasicAWSCredentials(amazonS3ConfigurationProperties.getAccessKeyId(), amazonS3ConfigurationProperties.getAccessKeySecret());
        amazonS3ClientBuilder.withCredentials(new AWSStaticCredentialsProvider(credentials));
    }

    AmazonS3 amazonS3 = amazonS3ClientBuilder.build();
    return amazonS3;
}
 
Example 5
Source File: AmazonS3ClientWrapper.java    From fredbet with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
public AmazonS3ClientWrapper(String accessKey, String secretKey, String region, String bucketName) {
	Assert.notNull(region, "region must not be null");
	Assert.notNull(bucketName, "bucketName must not be null");
	this.bucketName = bucketName;

	AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard().withRegion(region);
	if (StringUtils.isNotBlank(accessKey) && StringUtils.isNotBlank(secretKey)) {
		AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
		AWSCredentialsProvider provider = new AWSStaticCredentialsProvider(credentials);
		builder.withCredentials(provider);
	}

	this.amazonS3 = builder.build();
}
 
Example 6
Source File: S3Accessor.java    From datacollector with Apache License 2.0 5 votes vote down vote up
AmazonS3Client createS3Client() throws StageException {

    AmazonS3ClientBuilder builder = createAmazonS3ClientBuilder().withClientConfiguration(createClientConfiguration())
                                                                 .withChunkedEncodingDisabled(connectionConfigs
                                                                     .isChunkedEncodingEnabled())
                                                                 .withPathStyleAccessEnabled(true);
    AWSCredentialsProvider awsCredentialsProvider = getCredentialsProvider();
    // If we don't call build.withCredentials(...) then we will not overwrite the default credentials provider
    // already set in the builder when doing AmazonS3ClientBuilder.standard() so only calling build.withCredentials(...)
    // if our own provider exists
    if (awsCredentialsProvider != null) {
      builder.withCredentials(awsCredentialsProvider);
    }

    String region = (connectionConfigs.getRegion() == null || connectionConfigs.getRegion().isEmpty())
                    ? null
                    : connectionConfigs.getRegion();

    if (connectionConfigs.isUseEndpoint()) {
      builder.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(connectionConfigs.getEndpoint(),
          region
      ));
    } else if (region != null) {
      builder.withRegion(connectionConfigs.getRegion());
    } else {
      builder.withRegion(AwsRegion.US_WEST_1.getId());
      builder.withForceGlobalBucketAccessEnabled(true);
    }
    return (AmazonS3Client) builder.build();
  }
 
Example 7
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 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: 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 11
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);
        }
    }
}