Java Code Examples for com.amazonaws.auth.AWSCredentials

The following examples show how to use com.amazonaws.auth.AWSCredentials. These examples are extracted from open source projects. 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
@Override
public void refresh() {
    if (!iamRoleArn.isEmpty()) {
        if (!haveCredentialsExpired()) {
            return;
        }

        AWSCredentialsProvider credentialsProvider = getBasicCredentialsOrDefaultChain(accessKey, secretKey);
        AWSCredentials credentials = credentialsProvider.getCredentials();

        AssumeRoleRequest assumeRequest = new AssumeRoleRequest()
                .withRoleArn(iamRoleArn)
                .withExternalId(externalId)
                .withDurationSeconds(3600)
                .withRoleSessionName(ROLE_SESSION_NAME);

        AssumeRoleResult assumeResult = new AWSSecurityTokenServiceClient(credentials).assumeRole(assumeRequest);

        roleCredentials = assumeResult.getCredentials();
    }
}
 
Example 2
Source Project: pentaho-kettle   Source File: S3ObjectsProviderTest.java    License: Apache License 2.0 6 votes vote down vote up
private AmazonS3 getS3ClientMock( AWSCredentials credentials ) throws Exception {
  AmazonS3 s3Client = mock( AmazonS3.class );
  when( s3Client.listBuckets() ).thenReturn( generateTestBuckets( TEST_USER_BUCKETS_NAMES ) );
  // BUCKET2 - not empty bucket
  when( s3Client.listObjects( BUCKET2.getName() ) ).thenReturn( bucket2Objects );
  // BUCKET3 - empty bucket
  when( s3Client.listObjects( BUCKET3.getName() ) ).thenReturn( bucket3Objects );

  when( s3Client.getObject( any( String.class ), any( String.class ) ) ).thenReturn( testObject );
  ObjectMetadata mockMetaData = mock( ObjectMetadata.class );
  when( s3Client.getObjectMetadata( BUCKET1.getName(), "test3Object" ) ).thenReturn( mockMetaData );

  doReturn( false ).when( s3Client ).doesBucketExistV2( UNKNOWN_BUCKET );
  doReturn( true ).when( s3Client ).doesBucketExistV2( BUCKET2_NAME );
  doReturn( true ).when( s3Client ).doesBucketExistV2( BUCKET3_NAME );

  return s3Client;
}
 
Example 3
Source Project: java-slack-sdk   Source File: AmazonS3InstallationServiceTest.java    License: MIT License 6 votes vote down vote up
@Test
public void operations() throws Exception {
    AWSCredentials credentials = mock(AWSCredentials.class);
    when(credentials.getAWSAccessKeyId()).thenReturn("valid key");
    AmazonS3 s3 = mock(AmazonS3.class);
    when(s3.doesBucketExistV2(anyString())).thenReturn(true);

    AmazonS3InstallationService service = new AmazonS3InstallationService("test-bucket") {
        @Override
        protected AWSCredentials getCredentials() {
            return credentials;
        }

        @Override
        protected AmazonS3 createS3Client() {
            return s3;
        }
    };
    service.initializer().accept(null);

    service.saveInstallerAndBot(new DefaultInstaller());
    service.deleteBot(new DefaultBot());
    service.deleteInstaller(new DefaultInstaller());
    service.findBot("E123", "T123");
    service.findInstaller("E123", "T123", "U123");
}
 
Example 4
@Test
public void testCredentialsCreatedBySessionCredentialsProviderFactory() throws Exception {
  hiveConf.setStrings(SessionCredentialsProviderFactory.AWS_ACCESS_KEY_CONF_VAR, FAKE_ACCESS_KEY);
  hiveConf.setStrings(SessionCredentialsProviderFactory.AWS_SECRET_KEY_CONF_VAR, FAKE_SECRET_KEY);
  hiveConf.setStrings(SessionCredentialsProviderFactory.AWS_SESSION_TOKEN_CONF_VAR, FAKE_SESSION_TOKEN);

  SessionCredentialsProviderFactory factory = new SessionCredentialsProviderFactory();
  AWSCredentialsProvider provider = factory.buildAWSCredentialsProvider(hiveConf);
  AWSCredentials credentials = provider.getCredentials();

  assertThat(credentials, instanceOf(BasicSessionCredentials.class));

  BasicSessionCredentials sessionCredentials = (BasicSessionCredentials) credentials;

  assertEquals(FAKE_ACCESS_KEY, sessionCredentials.getAWSAccessKeyId());
  assertEquals(FAKE_SECRET_KEY, sessionCredentials.getAWSSecretKey());
  assertEquals(FAKE_SESSION_TOKEN, sessionCredentials.getSessionToken());
}
 
Example 5
@Test
@Override
public void test() throws Exception {
	for (int i = 1; i <= 2; i++) {
		Message<?> received = this.messageCollector.forChannel(this.channels.output())
				.poll(10, TimeUnit.SECONDS);
		assertNotNull(received);
		assertThat(received, hasPayload(new File(this.config.getLocalDir(), i + ".test")));
	}

	assertEquals(2, this.config.getLocalDir().list().length);

	AWSCredentialsProvider awsCredentialsProvider =
			TestUtils.getPropertyValue(this.amazonS3, "awsCredentialsProvider", AWSCredentialsProvider.class);

	AWSCredentials credentials = awsCredentialsProvider.getCredentials();
	assertEquals(AWS_ACCESS_KEY, credentials.getAWSAccessKeyId());
	assertEquals(AWS_SECRET_KEY, credentials.getAWSSecretKey());

	assertEquals(Region.US_GovCloud, this.amazonS3.getRegion());
	assertEquals(new URI("https://s3-us-gov-west-1.amazonaws.com"),
			TestUtils.getPropertyValue(this.amazonS3, "endpoint"));
}
 
Example 6
Source Project: presto   Source File: TestPrestoS3FileSystem.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testAssumeRoleStaticCredentials()
        throws Exception
{
    Configuration config = new Configuration(false);
    config.set(S3_ACCESS_KEY, "test_access_key");
    config.set(S3_SECRET_KEY, "test_secret_key");
    config.set(S3_IAM_ROLE, "test_role");

    try (PrestoS3FileSystem fs = new PrestoS3FileSystem()) {
        fs.initialize(new URI("s3n://test-bucket/"), config);
        AWSCredentialsProvider tokenService = getStsCredentialsProvider(fs, "test_role");
        assertInstanceOf(tokenService, AWSStaticCredentialsProvider.class);

        AWSCredentials credentials = tokenService.getCredentials();
        assertEquals(credentials.getAWSAccessKeyId(), "test_access_key");
        assertEquals(credentials.getAWSSecretKey(), "test_secret_key");
    }
}
 
Example 7
Source Project: suro   Source File: PropertyAWSCredentialsProvider.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public AWSCredentials getCredentials() {
    if (accessKey != null && secretKey != null) {
        return new AWSCredentials() {
            @Override
            public String getAWSAccessKeyId() {
                return accessKey;
            }

            @Override
            public String getAWSSecretKey() {
                return secretKey;
            }
        };
    } 
    else {
        return null;
    }
}
 
Example 8
Source Project: digdag   Source File: S3StorageTest.java    License: Apache License 2.0 6 votes vote down vote up
@Before
public void setUp()
        throws Exception
{
    assumeThat(TEST_S3_ENDPOINT, not(isEmptyOrNullString()));

    AWSCredentials credentials = new BasicAWSCredentials(TEST_S3_ACCESS_KEY_ID, TEST_S3_SECRET_ACCESS_KEY);
    AmazonS3Client s3 = new AmazonS3Client(credentials);
    s3.setEndpoint(TEST_S3_ENDPOINT);

    String bucket = UUID.randomUUID().toString();
    s3.createBucket(bucket);

    ConfigFactory cf = new ConfigFactory(objectMapper());
    Config config = cf.create()
        .set("endpoint", TEST_S3_ENDPOINT)
        .set("bucket", bucket)  // use unique bucket name
        .set("credentials.access-key-id", TEST_S3_ACCESS_KEY_ID)
        .set("credentials.secret-access-key", TEST_S3_SECRET_ACCESS_KEY)
        ;
    storage = new S3StorageFactory().newStorage(config);
}
 
Example 9
Source Project: airpal   Source File: AirpalModule.java    License: Apache License 2.0 6 votes vote down vote up
@Singleton
@Provides
@Nullable
public AmazonS3 provideAmazonS3Client(@Nullable AWSCredentials awsCredentials, @Nullable EncryptionMaterialsProvider encryptionMaterialsProvider)
{
    if (awsCredentials == null) {
        if (encryptionMaterialsProvider == null) {
            return new AmazonS3Client(new InstanceProfileCredentialsProvider());
        }
        else {
            return new AmazonS3EncryptionClient(new InstanceProfileCredentialsProvider(), encryptionMaterialsProvider);
        }
    }

    if (encryptionMaterialsProvider == null) {
        return new AmazonS3Client(awsCredentials);
    }
    else {
        return new AmazonS3EncryptionClient(awsCredentials, encryptionMaterialsProvider);
    }
}
 
Example 10
Source Project: localization_nifi   Source File: AbstractAWSProcessor.java    License: Apache License 2.0 6 votes vote down vote up
protected AWSCredentials getCredentials(final ProcessContext context) {
    final String accessKey = context.getProperty(ACCESS_KEY).evaluateAttributeExpressions().getValue();
    final String secretKey = context.getProperty(SECRET_KEY).evaluateAttributeExpressions().getValue();

    final String credentialsFile = context.getProperty(CREDENTIALS_FILE).getValue();

    if (credentialsFile != null) {
        try {
            return new PropertiesCredentials(new File(credentialsFile));
        } catch (final IOException ioe) {
            throw new ProcessException("Could not read Credentials File", ioe);
        }
    }

    if (accessKey != null && secretKey != null) {
        return new BasicAWSCredentials(accessKey, secretKey);
    }

    return new AnonymousAWSCredentials();

}
 
Example 11
Source Project: micro-server   Source File: S3UploadSystemTest.java    License: Apache License 2.0 6 votes vote down vote up
private static TransferManager createManager() {
    AWSCredentials credentials = new AWSCredentials() {

        @Override
        public String getAWSAccessKeyId() {
            return System.getProperty("s3.accessKey");
        }

        @Override
        public String getAWSSecretKey() {
            return System.getProperty("s3.secretKey");
        }

    };
    return new TransferManager(
                                  credentials);
}
 
Example 12
private <Y> Request<Y> prepareRequest(Request<Y> request, ExecutionContext executionContext, boolean signRequest) {
    request.setEndpoint(endpoint);
    request.setTimeOffset(timeOffset);

    AWSCredentials credentials = awsCredentialsProvider.getCredentials();

    AmazonWebServiceRequest originalRequest = request.getOriginalRequest();
    if (originalRequest != null && originalRequest.getRequestCredentials() != null) {
        credentials = originalRequest.getRequestCredentials();
    }
    if (signRequest) {
        // expiration date is not currently supported on service side, but presignRequest method requires
        // this argument so one with default value is provided.
        Date expirationDate = DateTime.now(DateTimeZone.UTC)
                .plusMinutes(DEFAULT_GET_REQUEST_EXPIRATION_MINUTES).toDate();
        signer.presignRequest(request, credentials, expirationDate);
    } else {
        executionContext.setSigner(signer);
        executionContext.setCredentials(credentials);
    }
    return request;
}
 
Example 13
Source Project: amazon-kinesis-client   Source File: V2CredentialWrapper.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public AwsCredentials resolveCredentials() {
    AWSCredentials current = oldCredentialsProvider.getCredentials();
    if (current instanceof AWSSessionCredentials) {
        return AwsSessionCredentials.create(current.getAWSAccessKeyId(), current.getAWSSecretKey(), ((AWSSessionCredentials) current).getSessionToken());
    }
    return new AwsCredentials() {
        @Override
        public String accessKeyId() {
            return current.getAWSAccessKeyId();
        }

        @Override
        public String secretAccessKey() {
            return current.getAWSSecretKey();
        }
    };
}
 
Example 14
protected AWSSecurityTokenService getTokenService(final Host host, final String region, final String accessKey, final String secretKey, final String sessionToken) {
    final ClientConfiguration configuration = new CustomClientConfiguration(host,
        new ThreadLocalHostnameDelegatingTrustManager(trust, host.getHostname()), key);
    return AWSSecurityTokenServiceClientBuilder.standard()
        .withCredentials(new AWSStaticCredentialsProvider(StringUtils.isBlank(sessionToken) ? new AWSCredentials() {
            @Override
            public String getAWSAccessKeyId() {
                return accessKey;
            }

            @Override
            public String getAWSSecretKey() {
                return secretKey;
            }
        } : new AWSSessionCredentials() {
            @Override
            public String getAWSAccessKeyId() {
                return accessKey;
            }

            @Override
            public String getAWSSecretKey() {
                return secretKey;
            }

            @Override
            public String getSessionToken() {
                return sessionToken;
            }
        }))
        .withClientConfiguration(configuration)
        .withRegion(StringUtils.isNotBlank(region) ? Regions.fromName(region) : Regions.DEFAULT_REGION).build();
}
 
Example 15
@Override
public Credentials configure(final Host host) {
    final Credentials credentials = new Credentials(host.getCredentials());
    if(!credentials.validate(host.getProtocol(), new LoginOptions(host.getProtocol()).password(false))) {
        // Lookup from default profile if no access key is set in bookmark
        for(AWSCredentialsProvider provider : providers) {
            try {
                final AWSCredentials c = provider.getCredentials();
                credentials.setUsername(c.getAWSAccessKeyId());
                credentials.setPassword(c.getAWSSecretKey());
                if(c instanceof AWSSessionCredentials) {
                    credentials.setToken(((AWSSessionCredentials) c).getSessionToken());
                }
                break;
            }
            catch(SdkClientException e) {
                log.debug(String.format("Ignore failure loading credentials from provider %s", provider));
                // Continue searching with next provider
            }
        }
    }
    return credentials;
}
 
Example 16
Source Project: pulsar   Source File: AwsCredentialProviderPlugin.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns a V2 credential provider for use with the v2 SDK.
 *
 * Defaults to an implementation that pulls credentials from a v1 provider
 */
default software.amazon.awssdk.auth.credentials.AwsCredentialsProvider getV2CredentialsProvider() {
    // make a small wrapper to forward requests to v1, this allows
    // for this interface to not "break" for implementers
    AWSCredentialsProvider v1Provider = getCredentialProvider();
    return () -> {
        AWSCredentials creds = v1Provider.getCredentials();
        if (creds instanceof AWSSessionCredentials) {
            return software.amazon.awssdk.auth.credentials.AwsSessionCredentials.create(
                    creds.getAWSAccessKeyId(),
                    creds.getAWSSecretKey(),
                    ((AWSSessionCredentials) creds).getSessionToken());
        } else {
            return software.amazon.awssdk.auth.credentials.AwsBasicCredentials.create(
                    creds.getAWSAccessKeyId(),
                    creds.getAWSSecretKey());
        }
    };
}
 
Example 17
Source Project: aws-codecommit-trigger-plugin   Source File: MockAwsSqs.java    License: Apache License 2.0 6 votes vote down vote up
private void start() throws IOException {
    System.out.println("starting mock sqs server");
    //this.port = findFreeLocalPort(); @see https://github.com/findify/sqsmock/pull/7
    this.api = new SQSService(this.port, 1);
    this.api.start();

    AWSCredentials credentials = new AnonymousAWSCredentials();
    this.sqsClient = new MockSQSClient(credentials);

    this.endpoint = String.format("http://localhost:%s", this.port);
    this.sqsClient.setEndpoint(endpoint);

    this.sqsUrl = this.sqsClient.createQueue(this.getClass().getSimpleName()).getQueueUrl();
    ((MockSQSClient)this.sqsClient).setQueueUrl(this.sqsUrl);

    this.started = true;
}
 
Example 18
Source Project: cloudExplorer   Source File: Acl.java    License: GNU General Public License v3.0 5 votes vote down vote up
void setBUCKETwebsite(String object, String access_key, String secret_key, String endpoint, String bucket) {
    try {
        AWSCredentials credentials = new BasicAWSCredentials(access_key, secret_key);
        AmazonS3 s3Client = new AmazonS3Client(credentials,
                new ClientConfiguration());
        if (endpoint.contains("amazonaws.com")) {
            String aws_endpoint = s3Client.getBucketLocation(new GetBucketLocationRequest(bucket));
            if (aws_endpoint.contains("US")) {
                s3Client.setEndpoint("https://s3.amazonaws.com");
            } else if (aws_endpoint.contains("us-west")) {
                s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
            } else if (aws_endpoint.contains("eu-west")) {
                s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
            } else if (aws_endpoint.contains("ap-")) {
                s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
            } else if (aws_endpoint.contains("sa-east-1")) {
                s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
            } else {
                s3Client.setEndpoint("https://s3." + aws_endpoint + ".amazonaws.com");
            }
        } else {
            s3Client.setS3ClientOptions(S3ClientOptions.builder().setPathStyleAccess(true).build());
            s3Client.setEndpoint(endpoint);
        }
        BucketWebsiteConfiguration bucketWebsiteConfiguration = s3Client.getBucketWebsiteConfiguration(bucket);
        s3Client.setBucketAcl(bucket, CannedAccessControlList.PublicRead);
        s3Client.setBucketWebsiteConfiguration(bucket, new BucketWebsiteConfiguration("index.html", "error.html"));
    } catch (Exception setACLpublic) {
        mainFrame.jTextArea1.append("\nException occurred in ACL");
    }
}
 
Example 19
@Override
public AWSCredentials getCredentials() {
  AWSSecurityTokenServiceClientBuilder clientBuilder = AWSSecurityTokenServiceClientBuilder.standard();
  AWSCredentialsProvider provider = new STSAssumeRoleSessionCredentialsProvider.Builder(roleArn, sessionName)
      .withStsClient(clientBuilder.defaultClient())
      .withExternalId(externalId)
      .build();

  return provider.getCredentials();
}
 
Example 20
Source Project: judgels   Source File: AwsFileSystem.java    License: GNU General Public License v2.0 5 votes vote down vote up
public AwsFileSystem(AwsConfiguration config, AwsFsConfiguration fsConfig) {
    AWSCredentials creds = new BasicAWSCredentials(config.getAccessKey(), config.getSecretKey());
    this.s3 = AmazonS3ClientBuilder
            .standard()
            .withRegion(config.getS3BucketRegionId())
            .withCredentials(new AWSStaticCredentialsProvider(creds))
            .build();

    this.cloudFrontBaseUrl = config.getCloudFrontBaseUrl();

    this.bucketName = fsConfig.getS3BucketName();
    if (!s3.doesBucketExistV2(bucketName)) {
        s3.createBucket(new CreateBucketRequest(bucketName, Region.fromValue(config.getS3BucketRegionId())));
    }
}
 
Example 21
Source Project: terrapin   Source File: TerrapinUtil.java    License: Apache License 2.0 5 votes vote down vote up
static public List<Pair<Path, Long>> getS3FileList(AWSCredentials credentials,
    String s3Bucket, String s3KeyPrefix) {
  List<Pair<Path, Long>> fileSizePairList = Lists.newArrayListWithCapacity(
      Constants.MAX_ALLOWED_SHARDS);
  AmazonS3Client s3Client = new AmazonS3Client(credentials);
  // List files and build the path using the s3n: prefix.
  // Note that keys > marker are retrieved where the > is by lexicographic order.
  String prefix = s3KeyPrefix;
  String marker = prefix;
  while (true) {
    boolean reachedEnd = false;
    ObjectListing listing = s3Client.listObjects(new ListObjectsRequest().
        withBucketName(s3Bucket).
        withMarker(marker));
    List<S3ObjectSummary> summaries = listing.getObjectSummaries();

    if (summaries.isEmpty()) {
      break;
    }

    for (S3ObjectSummary summary: summaries) {
      if (summary.getKey().startsWith(prefix)) {
        fileSizePairList.add(new ImmutablePair(new Path("s3n", s3Bucket, "/" + summary.getKey()),
            summary.getSize()));
        if (fileSizePairList.size() > Constants.MAX_ALLOWED_SHARDS) {
          throw new RuntimeException("Too many files " + fileSizePairList.size());
        }
      } else {
        // We found a key which does not match the prefix, stop.
        reachedEnd = true;
        break;
      }
    }
    if (reachedEnd) {
      break;
    }
    marker = summaries.get(summaries.size() - 1).getKey();
  }
  return fileSizePairList;
}
 
Example 22
@Test
public void testSessionCredentialSupplier() throws Exception {
    PowerMockito.mockStatic(CredentialsUtil.class);
    PowerMockito.when(CredentialsUtil.getAWSCredentialProvider(any())).thenReturn(new AWSCredentialsProvider() {
        @Override
        public AWSCredentials getCredentials() {
            return new AWSSessionCredentials() {
                @Override
                public String getSessionToken() {
                    return "token";
                }

                @Override
                public String getAWSAccessKeyId() {
                    return "access";
                }

                @Override
                public String getAWSSecretKey() {
                    return "secret";
                }
            };
        }

        @Override
        public void refresh() {

        }
    });

    Supplier<Credentials> creds = BlobStoreManagedLedgerOffloader.getCredentials("aws-s3", any());

    Assert.assertTrue(creds.get() instanceof SessionCredentials);
    SessionCredentials sessCreds = (SessionCredentials) creds.get();
    Assert.assertEquals(sessCreds.getAccessKeyId(), "access");
    Assert.assertEquals(sessCreds.getSecretAccessKey(), "secret");
    Assert.assertEquals(sessCreds.getSessionToken(), "token");
}
 
Example 23
@Override
public AWSCredentials getCredentials() {
  try {
    return super.getCredentials();
  } catch (final SdkClientException exception) {

  }
  // fall back to anonymous credentials
  return new AnonymousAWSCredentials();
}
 
Example 24
/**
 * Create client using AWSCredentials
 *
 * @deprecated use {@link #createClient(ProcessContext, AWSCredentialsProvider, ClientConfiguration)} instead
 */
@Override
protected AmazonDynamoDBClient createClient(final ProcessContext context, final AWSCredentials credentials, final ClientConfiguration config) {
    getLogger().debug("Creating client with aws credentials");

    final AmazonDynamoDBClient client = new AmazonDynamoDBClient(credentials, config);

    return client;
}
 
Example 25
Source Project: nifi   Source File: AbstractAWSGatewayApiProcessor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@Deprecated
protected GenericApiGatewayClient createClient(final ProcessContext context,
                                               final AWSCredentials credentials,
                                               final ClientConfiguration clientConfiguration) {
    return createClient(context, new AWSStaticCredentialsProvider(credentials),
                        clientConfiguration);
}
 
Example 26
Source Project: nifi   Source File: AbstractSQSProcessor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Create client using AWSCredentials
 *
 * @deprecated use {@link #createClient(ProcessContext, AWSCredentialsProvider, ClientConfiguration)} instead
 */
@Override
protected AmazonSQSClient createClient(final ProcessContext context, final AWSCredentials credentials, final ClientConfiguration config) {
    getLogger().info("Creating client using aws credentials ");

    return new AmazonSQSClient(credentials, config);
}
 
Example 27
@Override
public AWSCredentials getCredentials() {
          try {
              return super.getCredentials();
          } catch (AmazonClientException ace) {
              return null; // fall back to anonymous
          }
}
 
Example 28
Source Project: sequenceiq-samples   Source File: AwsCredentialsFactory.java    License: Apache License 2.0 5 votes vote down vote up
public AWSCredentials createSimpleAWSCredentials(String accessKey, String secretAccessKey) {
	String aKey = accessKey;
	String sAccessKey = secretAccessKey;
	try {
		aKey = URLDecoder.decode(aKey, ("UTF-8"));
		sAccessKey = URLDecoder.decode(sAccessKey, ("UTF-8"));
	} catch (UnsupportedEncodingException e) {
		System.out.println(aKey + ":" + sAccessKey + e.getMessage());
	}
	return new SimpleAWSCredentials(aKey, sAccessKey);
}
 
Example 29
Source Project: cloudExplorer   Source File: BucketClass.java    License: GNU General Public License v3.0 5 votes vote down vote up
Boolean VersioningStatus(String access_key, String secret_key, String bucket, String endpoint, Boolean enable) {
    String message = null;
    boolean result = false;
    AWSCredentials credentials = new BasicAWSCredentials(access_key, secret_key);
    AmazonS3 s3Client = new AmazonS3Client(credentials,
            new ClientConfiguration());

    if (endpoint.contains("amazonaws.com")) {
        String aws_endpoint = s3Client.getBucketLocation(new GetBucketLocationRequest(bucket));
        if (aws_endpoint.contains("US")) {
            s3Client.setEndpoint("https://s3.amazonaws.com");
        } else if (aws_endpoint.contains("us-west")) {
            s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
        } else if (aws_endpoint.contains("eu-west")) {
            s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
        } else if (aws_endpoint.contains("ap-")) {
            s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
        } else if (aws_endpoint.contains("sa-east-1")) {
            s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
        } else {
            s3Client.setEndpoint("https://s3." + aws_endpoint + ".amazonaws.com");
        }
    } else {
        s3Client.setS3ClientOptions(S3ClientOptions.builder().setPathStyleAccess(true).build());
        s3Client.setEndpoint(endpoint);
    }
    try {
        message = s3Client.getBucketVersioningConfiguration(bucket).getStatus().toString();
        if (message.contains("Enabled") || message.contains("Suspended")) {
            result = true;
        } else {
            result = false;
        }
    } catch (Exception versioning) {
    }

    return result;
}
 
Example 30
Source Project: nifi   Source File: AbstractDynamoDBProcessor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Create client using AWSCredentials
 *
 * @deprecated use {@link #createClient(ProcessContext, AWSCredentialsProvider, ClientConfiguration)} instead
 */
@Override
protected AmazonDynamoDBClient createClient(final ProcessContext context, final AWSCredentials credentials, final ClientConfiguration config) {
    getLogger().debug("Creating client with aws credentials");

    final AmazonDynamoDBClient client = new AmazonDynamoDBClient(credentials, config);

    return client;
}