@BeforeClass public static void setupS3() { final ProfileCredentialsProvider credentialsProvider = new ProfileCredentialsProvider(System.getenv("AWS_PROFILE")); s3Client = AmazonS3ClientBuilder.standard().withCredentials(credentialsProvider).build(); bucket = System.getenv("AWS_S3_BUCKET"); if (! s3Client.doesBucketExist(bucket)) { if (System.getenv("AWS_S3_CREATE_BUCKET") == null || !Boolean.parseBoolean(System.getenv("AWS_S3_CREATE_BUCKET"))) { throw new IllegalStateException("Bucket does not exist and not allowed to create."); } s3Client.createBucket(bucket); } S3SecretHandler.setS3CredentialsProvider(credentialsProvider); secretHandler = new S3SecretHandler(); }
protected BaseAmazonGlacierClientAware(String region, String account, String vault) { this.region = region; this.account = account; this.vault = vault; //@TODO Need to move away from deprecated fashion of acquiring a client //@TODO Also, consider supporting other ways of passing credentials (other than relying of local .aws profile) // load the credentials from the .aws profile this.credentialsProvider = new ProfileCredentialsProvider(); this.awsClient = new AmazonGlacierClient(this.credentialsProvider); // Set Glacier end-point this.awsClient.setEndpoint("https://glacier." + this.region + ".amazonaws.com/"); }
public AbstractS3Action( FileSystemIO delegate, Map<String, ?> env ) { this.delegate = delegate; AWSCredentials credentials; try { credentials = new ProfileCredentialsProvider().getCredentials(); } catch( Exception ex ) { throw new AmazonClientException( "Cannot load the credentials from the credential profiles file. " + "Please make sure that your credentials file is at the correct " + "location (~/.aws/credentials), and is in valid format.", ex ); } s3 = new AmazonS3Client( credentials ); Region region = Region.getRegion( Regions.EU_WEST_1 ); s3.setRegion( region ); String n = FileSystemUtils.get( env, BUCKET_READ ); if( n == null || n.trim().isEmpty() ) { n = FileSystemUtils.get( env, BUCKET ); } bucketName = Objects.requireNonNull( n, BUCKET + " or " + BUCKET_READ + " is not defined" ); }
public Processor(ProcessorConfig config) { ProfileCredentialsProvider creds = new ProfileCredentialsProvider(config.profile()); creds.getCredentials(); // credible credential criteria if (config.disableCerts()) System.setProperty("com.amazonaws.sdk.disableCertChecking", "true"); // Rekognition init rek = new AmazonRekognitionClient(creds); if (config.endpointOverride()) rek.setEndpoint(config.endpoint()); minConfidence = Integer.parseInt(config.confidence()); // The SQS queue to find jobs on sqs = new AmazonSQSClient(creds); queueUrl = sqs.createQueue(config.queue()).getQueueUrl(); // Processors if (config.wantCloudSearch()) processors.add(new CloudSearchIndexer(creds, config.cloudSearch())); if (config.wantDynamo()) processors.add(new DynamoWriter(creds, config.dynamo())); if (config.wantTags3()) processors.add(new S3ObjectTagger(creds, config.tagPrefix())); // Executor Service int maxWorkers = Integer.parseInt(config.concurrency()); executor = new ThreadPoolExecutor( 1, maxWorkers, 30, TimeUnit.SECONDS, new ArrayBlockingQueue<>(maxWorkers * 2, false), new CallerRunsPolicy() // prevents backing up too many jobs ); maxImagesToProcess = Long.parseLong(config.max()); }
@Override @SuppressWarnings("unused") public List<AwsFileMiniModel> list(String prefix) { AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider()); List<AwsFileMiniModel> files = new ArrayList(); ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucketName).withPrefix(prefix); ObjectListing objectListing; do { objectListing = s3client.listObjects(listObjectsRequest); for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { System.out.println(" - " + objectSummary.getKey() + " " + "(size = " + objectSummary.getSize() + ")" + " (date = " + objectSummary.getLastModified() + ")"); files.add(new AwsFileMiniModel(objectSummary.getKey(), objectSummary.getLastModified())); } listObjectsRequest.setMarker(objectListing.getNextMarker()); } while (objectListing.isTruncated()); return files; }
public AmazonS3 configureS3(){ /* * Create your credentials file at ~/.aws/credentials (C:\Users\USER_NAME\.aws\credentials for Windows users) * and save the following lines after replacing the underlined values with your own. * * [default] * aws_access_key_id = YOUR_ACCESS_KEY_ID * aws_secret_access_key = YOUR_SECRET_ACCESS_KEY */ AWSCredentials credentials = null; try { credentials = new ProfileCredentialsProvider().getCredentials(); } catch (Exception e) { throw new AmazonClientException( "Cannot load the credentials from the credential profiles file. " + "Please make sure that your credentials file is at the correct " + "location (~/.aws/credentials), and is in valid format.", e); } AmazonS3 s3 = new AmazonS3Client(credentials); Region usEast1 = Region.getRegion(Regions.US_EAST_1); s3.setRegion(usEast1); return s3; }
public static AWSCredentialsProviderChain getAWSCredentialsProviderChain() { String cerberusRoleToAssume = System.getenv(CERBERUS_ASSUME_ROLE_ARN) != null ? System.getenv(CERBERUS_ASSUME_ROLE_ARN) : ""; String cerberusRoleToAssumeExternalId = System.getenv(CERBERUS_ASSUME_ROLE_EXTERNAL_ID) != null ? System.getenv(CERBERUS_ASSUME_ROLE_EXTERNAL_ID) : ""; STSAssumeRoleSessionCredentialsProvider sTSAssumeRoleSessionCredentialsProvider = new STSAssumeRoleSessionCredentialsProvider .Builder(cerberusRoleToAssume, UUID.randomUUID().toString()) .withExternalId(cerberusRoleToAssumeExternalId) .build(); AWSCredentialsProviderChain chain = new AWSCredentialsProviderChain( new EnvironmentVariableCredentialsProvider(), new SystemPropertiesCredentialsProvider(), new ProfileCredentialsProvider(), sTSAssumeRoleSessionCredentialsProvider, new InstanceProfileCredentialsProvider()); return chain; }
@Test public void testFindOrCreateWeatherPipeJobBucket() { String jobID = null; String bucketName = "fdgfhfdx2"; AWSInterface awsInterface = new AWSInterface(jobID, null); jobID = "job1"; awsInterface = new AWSInterface(jobID, bucketName); String output; output = awsInterface.FindOrCreateWeatherPipeJobDirectory(); String answer = "s3n://fdgfhfdx2/"; assertEquals(answer, output); AWSCredentials credentials = new ProfileCredentialsProvider("default").getCredentials(); // TODO: add better credential searching later Region region = Region.getRegion(Regions.US_EAST_1); AmazonS3Client s3client = new AmazonS3Client(credentials); s3client.setRegion(region); s3client.deleteBucket(bucketName); System.out.println("FindOrCreateWeatherPipeJobBucket() is ok"); }
AWSCredentials getAwsCredentials() { // The ProfileCredentialsProvider will return your [default] // credential profile by reading from the credentials file located at // (~/.aws/credentials). AWSCredentials credentials; try { credentials = new ProfileCredentialsProvider().getCredentials(); } catch (Exception e) { throw new AmazonClientException( "Cannot load the credentials from the credential profiles file. " + "Please make sure that your credentials file is at the correct " + "location (~/.aws/credentials), and is in valid format.", e); } return credentials; }
private AmazonEC2Client getClientForAccount(final String accountId, final Region region) { final AWSSecurityTokenServiceClient stsClient = new AWSSecurityTokenServiceClient(new ProfileCredentialsProvider()); final AssumeRoleRequest assumeRequest = new AssumeRoleRequest().withRoleArn( "arn:aws:iam::ACCOUNT_ID:role/fullstop-role") .withDurationSeconds(3600).withRoleSessionName( "fullstop-role"); final AssumeRoleResult assumeResult = stsClient.assumeRole(assumeRequest); final BasicSessionCredentials temporaryCredentials = new BasicSessionCredentials( assumeResult.getCredentials() .getAccessKeyId(), assumeResult.getCredentials().getSecretAccessKey(), assumeResult.getCredentials().getSessionToken()); final AmazonEC2Client amazonEC2Client = new AmazonEC2Client(temporaryCredentials); amazonEC2Client.setRegion(region); return amazonEC2Client; }
@Override public AWSCredentials getCredentials() { // if profile is specified, use that final String profile = configuration.get(AWSConstants.PROFILE); if(!StringUtils.isNullOrEmpty(profile)) { return new ProfileCredentialsProvider(profile).getCredentials(); } // then try access key and secret final String accessKeyId = configuration.get(AWSConstants.ACCESS_KEY_ID); final String secretAccessKey = configuration.get(AWSConstants.SECRET_ACCESS_KEY); if(!StringUtils.isNullOrEmpty(accessKeyId) && !StringUtils.isNullOrEmpty(secretAccessKey)) { return new BasicAWSCredentials(accessKeyId, secretAccessKey); } // fall back to default return new DefaultAWSCredentialsProviderChain().getCredentials(); }
/** * Upload file to S3. * * @param local * - local file to upload. * @return boolean */ boolean uploadS3(File local) { boolean retval = false; AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider()); try { LOG.debug("Uploading a new object to S3 from local, file name " + local.getName()); s3client.putObject(new PutObjectRequest(bucketName, keyName, local)); retval = true; } catch (AmazonServiceException ase) { logAWSServiceException(ase); } catch (AmazonClientException ace) { logAWSClientException(ace); } return retval; }
/** * The only information needed to create a client are security credentials - * your AWS Access Key ID and Secret Access Key. All other * configuration, such as the service endpoints have defaults provided. * * Additional client parameters, such as proxy configuration, can be specified * in an optional ClientConfiguration object when constructing a client. * * @see com.amazonaws.auth.BasicAWSCredentials * @see com.amazonaws.auth.PropertiesCredentials * @see com.amazonaws.ClientConfiguration */ private static void init() throws Exception { /* * ProfileCredentialsProvider loads AWS security credentials from a * .aws/config file in your home directory. * * These same credentials are used when working with other AWS SDKs and the AWS CLI. * * You can find more information on the AWS profiles config file here: * http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html */ File configFile = new File(System.getProperty("user.home"), ".aws/credentials"); AWSCredentialsProvider credentialsProvider = new ProfileCredentialsProvider( new ProfilesConfigFile(configFile), "default"); if (credentialsProvider.getCredentials() == null) { throw new RuntimeException("No AWS security credentials found:\n" + "Make sure you've configured your credentials in: " + configFile.getAbsolutePath() + "\n" + "For more information on configuring your credentials, see " + "http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html"); } ec2 = new AmazonEC2Client(credentialsProvider); s3 = new AmazonS3Client(credentialsProvider); }
@Override public void open(Map map, TopologyContext topologyContext, SpoutOutputCollector spoutOutputCollector) { super.open(map, topologyContext, spoutOutputCollector); try { if (map.containsKey(Constants.S3_BUCKET_NAME)) { bucketName = (String) map.get(Constants.S3_BUCKET_NAME); } if (map.containsKey(Constants.S3_KEY)) { key = (String) map.get(Constants.S3_KEY); } credentials = new ProfileCredentialsProvider().getCredentials(); s3Client = new AmazonS3Client(credentials); object = s3Client.getObject( new GetObjectRequest(bucketName, key)); objectData = object.getObjectContent(); reader = new BufferedReader(new InputStreamReader(objectData)); } catch (Exception e) { System.err.println("Error connecting to bucket." + e.getMessage()); } }
@Test public void credentialsProvider_noExplicitCredentialsProviderConfigured_configuresDefaultAwsCredentialsProviderChainWithInstanceProfile() throws Exception { // Arrange this.context = new AnnotationConfigApplicationContext(); this.context.register(ContextCredentialsAutoConfiguration.class); // Act this.context.refresh(); // Assert AWSCredentialsProvider awsCredentialsProvider = this.context.getBean(AmazonWebserviceClientConfigurationUtils.CREDENTIALS_PROVIDER_BEAN_NAME, AWSCredentialsProvider.class); assertNotNull(awsCredentialsProvider); @SuppressWarnings("unchecked") List<CredentialsProvider> credentialsProviders = (List<CredentialsProvider>) ReflectionTestUtils.getField(awsCredentialsProvider, "credentialsProviders"); assertEquals(2, credentialsProviders.size()); assertTrue(EC2ContainerCredentialsProviderWrapper.class.isInstance(credentialsProviders.get(0))); assertTrue(ProfileCredentialsProvider.class.isInstance(credentialsProviders.get(1))); }
@Test public void credentialsProvider_accessKeyAndSecretKeyConfigured_configuresStaticCredentialsProviderWithAccessAndSecretKey() { this.context = new AnnotationConfigApplicationContext(); this.context.register(ContextCredentialsAutoConfiguration.class); TestPropertyValues.of( "cloud.aws.credentials.accessKey:foo", "cloud.aws.credentials.secretKey:bar").applyTo(this.context); this.context.refresh(); AWSCredentialsProvider awsCredentialsProvider = this.context.getBean(AmazonWebserviceClientConfigurationUtils.CREDENTIALS_PROVIDER_BEAN_NAME, AWSCredentialsProviderChain.class); assertNotNull(awsCredentialsProvider); @SuppressWarnings("unchecked") List<CredentialsProvider> credentialsProviders = (List<CredentialsProvider>) ReflectionTestUtils.getField(awsCredentialsProvider, "credentialsProviders"); assertEquals(2, credentialsProviders.size()); assertTrue(AWSStaticCredentialsProvider.class.isInstance(credentialsProviders.get(0))); assertTrue(ProfileCredentialsProvider.class.isInstance(credentialsProviders.get(1))); assertEquals("foo", awsCredentialsProvider.getCredentials().getAWSAccessKeyId()); assertEquals("bar", awsCredentialsProvider.getCredentials().getAWSSecretKey()); }
@Test public void credentialsProvider_instanceProfileConfigured_configuresInstanceProfileCredentialsProvider() { this.context = new AnnotationConfigApplicationContext(); this.context.register(ContextCredentialsAutoConfiguration.class); TestPropertyValues.of( "cloud.aws.credentials.instanceProfile").applyTo(this.context); this.context.refresh(); AWSCredentialsProvider awsCredentialsProvider = this.context.getBean(AmazonWebserviceClientConfigurationUtils.CREDENTIALS_PROVIDER_BEAN_NAME, AWSCredentialsProvider.class); assertNotNull(awsCredentialsProvider); @SuppressWarnings("unchecked") List<CredentialsProvider> credentialsProviders = (List<CredentialsProvider>) ReflectionTestUtils.getField(awsCredentialsProvider, "credentialsProviders"); assertEquals(2, credentialsProviders.size()); assertTrue(EC2ContainerCredentialsProviderWrapper.class.isInstance(credentialsProviders.get(0))); assertTrue(ProfileCredentialsProvider.class.isInstance(credentialsProviders.get(1))); }
@Test public void credentialsProvider_profileNameConfigured_configuresProfileCredentialsProvider() { this.context = new AnnotationConfigApplicationContext(); this.context.register(ContextCredentialsAutoConfiguration.class); TestPropertyValues.of( "cloud.aws.credentials.profileName:test").applyTo(this.context); this.context.refresh(); AWSCredentialsProvider awsCredentialsProvider = this.context.getBean(AmazonWebserviceClientConfigurationUtils.CREDENTIALS_PROVIDER_BEAN_NAME, AWSCredentialsProvider.class); assertNotNull(awsCredentialsProvider); @SuppressWarnings("unchecked") List<CredentialsProvider> credentialsProviders = (List<CredentialsProvider>) ReflectionTestUtils.getField(awsCredentialsProvider, "credentialsProviders"); assertEquals(2, credentialsProviders.size()); assertTrue(EC2ContainerCredentialsProviderWrapper.class.isInstance(credentialsProviders.get(0))); assertTrue(ProfileCredentialsProvider.class.isInstance(credentialsProviders.get(1))); assertEquals("test", ReflectionTestUtils.getField(credentialsProviders.get(1), "profileName")); }
@Test public void credentialsProvider_profileNameAndPathConfigured_configuresProfileCredentialsProvider() throws IOException { this.context = new AnnotationConfigApplicationContext(); this.context.register(ContextCredentialsAutoConfiguration.class); TestPropertyValues.of( "cloud.aws.credentials.profileName:customProfile", "cloud.aws.credentials.profilePath:" + new ClassPathResource(getClass().getSimpleName() + "-profile", getClass()).getFile().getAbsolutePath()).applyTo(this.context); this.context.refresh(); AWSCredentialsProvider awsCredentialsProvider = this.context.getBean(AmazonWebserviceClientConfigurationUtils.CREDENTIALS_PROVIDER_BEAN_NAME, AWSCredentialsProvider.class); assertNotNull(awsCredentialsProvider); @SuppressWarnings("unchecked") List<CredentialsProvider> credentialsProviders = (List<CredentialsProvider>) ReflectionTestUtils.getField(awsCredentialsProvider, "credentialsProviders"); assertEquals(2, credentialsProviders.size()); assertTrue(EC2ContainerCredentialsProviderWrapper.class.isInstance(credentialsProviders.get(0))); assertTrue(ProfileCredentialsProvider.class.isInstance(credentialsProviders.get(1))); ProfileCredentialsProvider provider = (ProfileCredentialsProvider) credentialsProviders.get(1); assertEquals("testAccessKey", provider.getCredentials().getAWSAccessKeyId()); assertEquals("testSecretKey", provider.getCredentials().getAWSSecretKey()); }
@Test public void credentialsProvider_configWithProfileNameAndNoProfilePath_profileCredentialsProviderConfigured() throws Exception { //Arrange this.context = new AnnotationConfigApplicationContext(ApplicationConfigurationWithProfileAndDefaultProfilePath.class); //Act AWSCredentialsProvider awsCredentialsProvider = this.context.getBean(AWSCredentialsProvider.class); //Assert assertNotNull(awsCredentialsProvider); @SuppressWarnings("unchecked") List<CredentialsProvider> credentialsProviders = (List<CredentialsProvider>) ReflectionTestUtils.getField(awsCredentialsProvider, "credentialsProviders"); assertEquals(1, credentialsProviders.size()); assertTrue(ProfileCredentialsProvider.class.isInstance(credentialsProviders.get(0))); ProfileCredentialsProvider provider = (ProfileCredentialsProvider) credentialsProviders.get(0); assertEquals("test", ReflectionTestUtils.getField(provider, "profileName")); }
@Test public void credentialsProvider_configWithAllProviders_allCredentialsProvidersConfigured() throws Exception { //Arrange this.context = new AnnotationConfigApplicationContext(ApplicationConfigurationWithAllProviders.class); //Act AWSCredentialsProvider awsCredentialsProvider = this.context.getBean(AWSCredentialsProvider.class); //Assert assertNotNull(awsCredentialsProvider); @SuppressWarnings("unchecked") List<CredentialsProvider> credentialsProviders = (List<CredentialsProvider>) ReflectionTestUtils.getField(awsCredentialsProvider, "credentialsProviders"); assertEquals(3, credentialsProviders.size()); assertTrue(AWSStaticCredentialsProvider.class.isInstance(credentialsProviders.get(0))); assertTrue(EC2ContainerCredentialsProviderWrapper.class.isInstance(credentialsProviders.get(1))); assertTrue(ProfileCredentialsProvider.class.isInstance(credentialsProviders.get(2))); }
public void remove(Object[] params) { AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider()); try { s3client.deleteObject(new DeleteObjectRequest(bucketName, params[0].toString())); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which " + "means your request made it " + "to Amazon S3, but was rejected with an error response" + " for some reason."); System.out.println("Error Message:" + ase.getMessage()); System.out.println("HTTP Status Code:" + ase.getStatusCode()); System.out.println("AWS Error Code:" + ase.getErrorCode()); System.out.println("Error Type:" + ase.getErrorType()); System.out.println("Request ID:" + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which " + "means the client encountered " + "an internal error while trying to " + "communicate with S3, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
public Object get(Object[] params) { AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider()); try { S3Object object = s3Client.getObject(new GetObjectRequest(bucketName, params[0].toString())); InputStream objectData = object.getObjectContent(); byte[] bytes = IOUtils.toByteArray(is); ByteBuffer b = ByteBuffer.wrap(bytes); return b; } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which " + "means your request made it " + "to Amazon S3, but was rejected with an error response" + " for some reason."); System.out.println("Error Message:" + ase.getMessage()); System.out.println("HTTP Status Code:" + ase.getStatusCode()); System.out.println("AWS Error Code:" + ase.getErrorCode()); System.out.println("Error Type:" + ase.getErrorType()); System.out.println("Request ID:" + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which " + "means the client encountered " + "an internal error while trying to " + "communicate with S3, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
public void put(Object[] params) { String key = params[0].toString(); File file = new File(params[0].toString()); AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider()); try { s3client.putObject(new PutObjectRequest(bucketName, key, file)); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which " + "means your request made it " + "to Amazon S3, but was rejected with an error response" + " for some reason."); System.out.println("Error Message:" + ase.getMessage()); System.out.println("HTTP Status Code:" + ase.getStatusCode()); System.out.println("AWS Error Code:" + ase.getErrorCode()); System.out.println("Error Type:" + ase.getErrorType()); System.out.println("Request ID:" + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which " + "means the client encountered " + "an internal error while trying to " + "communicate with S3, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
@Bean public AmazonDynamoDBAsync getDynamoDBClient() { return AmazonDynamoDBAsyncClientBuilder.standard() .withCredentials(new ProfileCredentialsProvider(CREDENTIALS_PROVIDER)) .withRegion(Regions.AP_SOUTHEAST_2) .build(); }
@Bean public AmazonSQSAsync getSQSClient() { return AmazonSQSAsyncClientBuilder.standard() .withCredentials(new ProfileCredentialsProvider(CREDENTIALS_PROVIDER)) .withRegion(Regions.AP_SOUTHEAST_2) .build(); }
@Bean public SQSConnectionFactory sqsConnectionFactory() throws JMSException { return new SQSConnectionFactory( new ProviderConfiguration(), AmazonSQSClientBuilder.standard() .withRegion(Regions.AP_SOUTHEAST_2) .withCredentials(new ProfileCredentialsProvider(CREDENTIALS_PROVIDER)) ); }
@Override public void run() { super.run(); String awsCredentialsProfile = this.readStringArgument("awsProfile", "default"); String bucket = this.readStringArgument("bucket"); String objectKey = this.readStringArgument("objectKey"); String sourceFilePath = this.readStringArgument("sourceFile"); File sourceFile = new File(sourceFilePath); if (sourceFile.exists()) { try { AmazonS3 s3Client = new AmazonS3Client( new ProfileCredentialsProvider(awsCredentialsProfile)); s3Client.putObject(new PutObjectRequest(bucket, objectKey, sourceFile)); } catch (Exception ex) { throw new RuntimeException(String.format( "Failed to upload file \"%s\" to S3 bucket \"%s\"", sourceFilePath, bucket), ex); } } else { throw new RuntimeException(String.format( "Source file \"%s\" doesn't exist", sourceFilePath)); } }
@Override public void run() { super.run(); String awsCredentialsProfile = this.readStringArgument("awsProfile", "default"); String bucket = this.readStringArgument("bucket"); String objectKey = this.readStringArgument("objectKey"); AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider(awsCredentialsProfile)); ObjectMetadata metadata = s3Client.getObjectMetadata( new GetObjectMetadataRequest(bucket, objectKey)); try { Date expirationTime = metadata.getExpirationTime(); if (expirationTime != null) { this.writeOutput("expirationTime", metadata.getExpirationTime().getTime()); } else { this.writeOutput("expirationTime", null); } this.writeOutput("lastModified", metadata.getLastModified().getTime()); this.writeOutput("userMetadata", metadata.getUserMetadata()); this.writeOutput("size", metadata.getContentLength()); this.writeOutput("storageClass", metadata.getStorageClass()); this.writeOutput("versionId", metadata.getVersionId()); } catch (Exception ex) { throw new RuntimeException(String.format( "Failed to get object metadata for object key %s in bucket %s", objectKey, bucket), ex); } }
@Override public void run() { super.run(); String awsCredentialsProfile = this.readStringArgument("awsProfile", "default"); String bucket = this.readStringArgument("bucket"); String objectKey = this.readStringArgument("objectKey"); String targetFilePath = this.readStringArgument("targetFile"); Boolean overwrite = this.readBooleanArgument("overwrite", false); AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider(awsCredentialsProfile)); S3Object object = s3Client.getObject( new GetObjectRequest(bucket, objectKey)); InputStream objectDataStream = object.getObjectContent(); if (targetFilePath != null) { File targetFile = new File(targetFilePath); if (!targetFile.isAbsolute()) { targetFile = Paths.get(this.getActor().getTempDir().getAbsolutePath(), targetFilePath).toFile(); } targetFile.getParentFile().mkdirs(); try { if (overwrite) { Files.copy(objectDataStream, targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING); } else { Files.copy(objectDataStream, targetFile.toPath()); } } catch (Exception ex) { throw new RuntimeException(String.format( "Failed to transfer data from the input stream into file %s", targetFilePath), ex); } this.writeArgument("targetFile", targetFile.getAbsolutePath()); } else { // TODO: Make targetFile arg optional so this branch can execute. // Read data in memory and write it to an output value } }
public static String getOwnIP() { AWSCredentials credentials = null; try { credentials = new ProfileCredentialsProvider().getCredentials(); } catch(Exception e){} AmazonEC2 ec2 = new AmazonEC2Client(credentials); ec2.setEndpoint("ec2.eu-west-1.amazonaws.com"); DescribeInstancesResult describeInstancesResult = ec2.describeInstances(); List<Reservation> reservations = describeInstancesResult.getReservations(); ArrayList<Instance> listOfInstances = new ArrayList<Instance>(); for(Reservation reservation : reservations) listOfInstances.addAll(reservation.getInstances()); String ownIP = null; String ownInstanceID = checkInstanceId(); for(Instance instance: listOfInstances) { if(instance.getInstanceId().equals(ownInstanceID)) ownIP = instance.getPublicIpAddress(); } return ownIP; }
public static DynamoEntry getFromMSS(String parameter) { dynamoDB = new AmazonDynamoDBClient(new ProfileCredentialsProvider().getCredentials()); dynamoDB.setRegion(Region.getRegion(Regions.EU_WEST_1)); HashMap<String, Condition> keyConditions = new HashMap<String, Condition>(); keyConditions.put("Parameter", new Condition().withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue(parameter))); QueryRequest queryRequest = new QueryRequest(); queryRequest.setKeyConditions(keyConditions); queryRequest.withTableName("MSS"); QueryResult result = dynamoDB.query(queryRequest); //se houver um pedido ja tratado no MSS getCount > 0, caso contrario nao existe if(result.getCount() == 0) return null; else { //vamos buscar o rank Map<String, AttributeValue> queryResult = result.getItems().get(0); String rank = ""; boolean runningState = false; for (Map.Entry<String, AttributeValue> entry : queryResult.entrySet()) { if(entry.getKey().equals("Rank")) rank = entry.getValue().getN(); else if(entry.getKey().equals("Running")) runningState = entry.getValue().getBOOL(); } DynamoEntry rankResult = new DynamoEntry(new BigInteger(rank), runningState); return rankResult; } }
@Override public void run() { try { FileInputStream logFile = new FileInputStream("logIntermedio.txt"); BufferedReader bufReader = new BufferedReader(new InputStreamReader(logFile)); String line; while ((line = bufReader.readLine()) != null) { String[] parts = line.split(":"); if (parts[0].equals(threadID)) { AmazonDynamoDBClient dynamoDB = new AmazonDynamoDBClient(new ProfileCredentialsProvider()); dynamoDB.setRegion(Region.getRegion(Regions.EU_WEST_1)); String[] parameterParts = parts[1].split("="); String parameter = parameterParts[1]; QueryResult result = getEntryDynamo(parameter, dynamoDB); if (result.getCount() == 0) // se a query nao retornar nada significa que nao temos nada no dynamo sobre este parametro { putDynamo(parameter, metricas.i_count, metricas.b_count, metricas.fieldLoadCount, metricas.loadCount, dynamoDB, true, threadID, null); } else { //caso contrario vamos buscar as metricas que temos para atualiza las Map<String, AttributeValue> queryResult = result.getItems().get(0); // runningValue fica a true se Running estiver a true, se ja estiver a false entao ja temos as metricas finais para este parametro portanto nao atualizamos o dynamo boolean runningValue = queryResult.values().contains(new AttributeValue().withBOOL(true)); if (runningValue) { putDynamo(parameter, metricas.i_count, metricas.b_count, metricas.fieldLoadCount, metricas.loadCount, dynamoDB, true, threadID, result); } } break; } } } catch (Exception e) { e.printStackTrace(); } }
@Ignore @Test public void testExample() throws InterruptedException, IOException { KinesisVideoExample example = KinesisVideoExample.builder().region(Regions.US_WEST_2) .streamName("myTestStream") .credentialsProvider(new ProfileCredentialsProvider()) .inputVideoStream(TestResourceUtil.getTestInputStream("clusters.mkv")) .build(); example.execute(); Assert.assertEquals(5, example.getFragmentsPersisted()); Assert.assertEquals(5, example.getFragmentsRead()); }
public ConnectorModel(Regions region, String profileName) { ProfileCredentialsProvider profileCredentialsProvider = new ProfileCredentialsProvider(profileName); awsLambdaClient = AWSLambdaClientBuilder.standard() .withRegion(region) .withCredentials(profileCredentialsProvider) .build(); }
public Scanner(ScanConfig config) { opt = config; ProfileCredentialsProvider creds = new ProfileCredentialsProvider(opt.profile()); creds.getCredentials(); // credible credential criteria s3 = new AmazonS3Client(creds); sqs = new AmazonSQSClient(creds); CreateQueueResult queueResult = sqs.createQueue(opt.queue()); queueUrl = queueResult.getQueueUrl(); filter = Pattern.compile(opt.filter(), Pattern.CASE_INSENSITIVE); max = Long.parseLong(opt.max()); }
@SuppressWarnings("deprecation") public ApiImporter(String profile, String accessKey, String secret) { if (accessKey != null && secret != null) { credentialsProvider=new AWSCredentialsProvider() { @Override public void refresh() { } @Override public AWSCredentials getCredentials() { return new AWSCredentials() { @Override public String getAWSSecretKey() { return secret; } @Override public String getAWSAccessKeyId() { return accessKey; } }; } }; } else { credentialsProvider = new AWSCredentialsProviderChain(new EnvironmentVariableCredentialsProvider(), new SystemPropertiesCredentialsProvider(), new ProfileCredentialsProvider(profile), new InstanceProfileCredentialsProvider()); } amazonApiGatewayClient = new AmazonApiGatewayClient(credentialsProvider); }
AuthenticationInfoAWSCredentialsProviderChain(AuthenticationInfo authenticationInfo) { super( new InstanceProfileCredentialsProvider(), new ProfileCredentialsProvider(), new EnvironmentVariableCredentialsProvider(), new SystemPropertiesCredentialsProvider(), new InstanceProfileCredentialsProvider()); }
@TaskAction protected void deploy() { String versionLabel = getVersionLabel(); AWSCredentialsProviderChain credentialsProvider = new AWSCredentialsProviderChain(new EnvironmentVariableCredentialsProvider(), new SystemPropertiesCredentialsProvider(), new ProfileCredentialsProvider(beanstalk.getProfile())); BeanstalkDeployer deployer = new BeanstalkDeployer(beanstalk.getS3Endpoint(), beanstalk.getBeanstalkEndpoint(), credentialsProvider); File warFile = getProject().files(war).getSingleFile(); deployer.deploy(warFile, deployment.getApplication(), deployment.getEnvironment(), deployment.getTemplate(), versionLabel); }