use of com.amazonaws.services.s3.model.Tag in project aws-doc-sdk-examples by awsdocs.
the class LifecycleConfiguration method main.
public static void main(String[] args) throws IOException {
Regions clientRegion = Regions.DEFAULT_REGION;
String bucketName = "*** Bucket name ***";
// Create a rule to archive objects with the "glacierobjects/" prefix to Glacier immediately.
BucketLifecycleConfiguration.Rule rule1 = new BucketLifecycleConfiguration.Rule().withId("Archive immediately rule").withFilter(new LifecycleFilter(new LifecyclePrefixPredicate("glacierobjects/"))).addTransition(new Transition().withDays(0).withStorageClass(StorageClass.Glacier)).withStatus(BucketLifecycleConfiguration.ENABLED);
// Create a rule to transition objects to the Standard-Infrequent Access storage class
// after 30 days, then to Glacier after 365 days. Amazon S3 will delete the objects after 3650 days.
// The rule applies to all objects with the tag "archive" set to "true".
BucketLifecycleConfiguration.Rule rule2 = new BucketLifecycleConfiguration.Rule().withId("Archive and then delete rule").withFilter(new LifecycleFilter(new LifecycleTagPredicate(new Tag("archive", "true")))).addTransition(new Transition().withDays(30).withStorageClass(StorageClass.StandardInfrequentAccess)).addTransition(new Transition().withDays(365).withStorageClass(StorageClass.Glacier)).withExpirationInDays(3650).withStatus(BucketLifecycleConfiguration.ENABLED);
// Add the rules to a new BucketLifecycleConfiguration.
BucketLifecycleConfiguration configuration = new BucketLifecycleConfiguration().withRules(Arrays.asList(rule1, rule2));
try {
AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new ProfileCredentialsProvider()).withRegion(clientRegion).build();
// Save the configuration.
s3Client.setBucketLifecycleConfiguration(bucketName, configuration);
// Retrieve the configuration.
configuration = s3Client.getBucketLifecycleConfiguration(bucketName);
// Add a new rule with both a prefix predicate and a tag predicate.
configuration.getRules().add(new BucketLifecycleConfiguration.Rule().withId("NewRule").withFilter(new LifecycleFilter(new LifecycleAndOperator(Arrays.asList(new LifecyclePrefixPredicate("YearlyDocuments/"), new LifecycleTagPredicate(new Tag("expire_after", "ten_years")))))).withExpirationInDays(3650).withStatus(BucketLifecycleConfiguration.ENABLED));
// Save the configuration.
s3Client.setBucketLifecycleConfiguration(bucketName, configuration);
// Retrieve the configuration.
configuration = s3Client.getBucketLifecycleConfiguration(bucketName);
// Verify that the configuration now has three rules.
configuration = s3Client.getBucketLifecycleConfiguration(bucketName);
System.out.println("Expected # of rules = 3; found: " + configuration.getRules().size());
// Delete the configuration.
s3Client.deleteBucketLifecycleConfiguration(bucketName);
// Verify that the configuration has been deleted by attempting to retrieve it.
configuration = s3Client.getBucketLifecycleConfiguration(bucketName);
String s = (configuration == null) ? "No configuration found." : "Configuration found.";
System.out.println(s);
} catch (AmazonServiceException e) {
// The call was transmitted successfully, but Amazon S3 couldn't process
// it, so it returned an error response.
e.printStackTrace();
} catch (SdkClientException e) {
// Amazon S3 couldn't be contacted for a response, or the client
// couldn't parse the response from Amazon S3.
e.printStackTrace();
}
}
use of com.amazonaws.services.s3.model.Tag in project cloudbreak by hortonworks.
the class AwsMetadataCollector method getTag.
private String getTag(Instance instance) {
for (Tag tag : instance.getTags()) {
if (TAG_NAME.equals(tag.getKey())) {
String value = tag.getValue();
LOGGER.info("Instance: {} was already tagged: {}", instance.getInstanceId(), value);
return value;
}
}
return null;
}
use of com.amazonaws.services.s3.model.Tag in project cloudbreak by hortonworks.
the class AwsMetadataCollector method addTag.
private void addTag(AmazonEC2 amazonEC2Client, CloudInstance cloudInstance, Instance instance) {
String tagName = awsClient.getCbName(cloudInstance.getTemplate().getGroupName(), cloudInstance.getTemplate().getPrivateId());
Tag t = new Tag();
t.setKey(TAG_NAME);
t.setValue(tagName);
CreateTagsRequest ctr = new CreateTagsRequest();
ctr.setTags(Collections.singletonList(t));
ctr.withResources(instance.getInstanceId());
amazonEC2Client.createTags(ctr);
}
use of com.amazonaws.services.s3.model.Tag in project cloudbreak by hortonworks.
the class TagsUtil method checkTagsAws.
protected static void checkTagsAws(Regions region, Collection<String> instanceIdList, Map<String, String> tagsToCheckMap) {
Map<String, String> extractedTagsToCheck = new HashMap<>();
List<Tag> extractedTags;
AmazonEC2 ec2 = AmazonEC2ClientBuilder.standard().withRegion(region).build();
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();
describeInstancesRequest.withInstanceIds(instanceIdList);
DescribeInstancesResult describeInstancesResultAll = ec2.describeInstances(describeInstancesRequest);
List<Reservation> reservationsAll = describeInstancesResultAll.getReservations();
for (Reservation reservation : reservationsAll) {
for (Instance instance : reservation.getInstances()) {
extractedTags = instance.getTags();
Assert.assertNotNull(extractedTags);
for (Tag tag : extractedTags) {
extractedTagsToCheck.put(tag.getKey(), tag.getValue());
}
checkTags(tagsToCheckMap, extractedTagsToCheck);
extractedTags.clear();
}
}
}
Aggregations