Search in sources :

Example 56 with Tag

use of software.amazon.awssdk.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);
}
Also used : CreateTagsRequest(com.amazonaws.services.ec2.model.CreateTagsRequest) Tag(com.amazonaws.services.ec2.model.Tag)

Example 57 with Tag

use of software.amazon.awssdk.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();
        }
    }
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) HashMap(java.util.HashMap) Instance(com.amazonaws.services.ec2.model.Instance) Tag(com.amazonaws.services.ec2.model.Tag) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

Example 58 with Tag

use of software.amazon.awssdk.services.s3.model.Tag in project aws-doc-sdk-examples by awsdocs.

the class LifecycleConfiguration method getLifecycleConfig.

// Retrieve the configuration and add a new rule.
public static void getLifecycleConfig(S3Client s3, String bucketName, String accountId) {
    try {
        GetBucketLifecycleConfigurationRequest getBucketLifecycleConfigurationRequest = GetBucketLifecycleConfigurationRequest.builder().bucket(bucketName).expectedBucketOwner(accountId).build();
        GetBucketLifecycleConfigurationResponse response = s3.getBucketLifecycleConfiguration(getBucketLifecycleConfigurationRequest);
        // Create a new List.
        List<LifecycleRule> newList = new ArrayList<>();
        List<LifecycleRule> rules = response.rules();
        for (LifecycleRule rule : rules) {
            newList.add(rule);
        }
        // Add a new rule with both a prefix predicate and a tag predicate.
        LifecycleRuleFilter ruleFilter = LifecycleRuleFilter.builder().prefix("YearlyDocuments/").build();
        Transition transition = Transition.builder().storageClass(TransitionStorageClass.GLACIER).days(3650).build();
        LifecycleRule rule1 = LifecycleRule.builder().id("NewRule").filter(ruleFilter).transitions(transition).status(ExpirationStatus.ENABLED).build();
        // Add the new rule to the list.
        newList.add(rule1);
        BucketLifecycleConfiguration lifecycleConfiguration = BucketLifecycleConfiguration.builder().rules(newList).build();
        PutBucketLifecycleConfigurationRequest putBucketLifecycleConfigurationRequest = PutBucketLifecycleConfigurationRequest.builder().bucket(bucketName).lifecycleConfiguration(lifecycleConfiguration).expectedBucketOwner(accountId).build();
        s3.putBucketLifecycleConfiguration(putBucketLifecycleConfigurationRequest);
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : GetBucketLifecycleConfigurationRequest(software.amazon.awssdk.services.s3.model.GetBucketLifecycleConfigurationRequest) BucketLifecycleConfiguration(software.amazon.awssdk.services.s3.model.BucketLifecycleConfiguration) LifecycleRuleFilter(software.amazon.awssdk.services.s3.model.LifecycleRuleFilter) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ArrayList(java.util.ArrayList) Transition(software.amazon.awssdk.services.s3.model.Transition) GetBucketLifecycleConfigurationResponse(software.amazon.awssdk.services.s3.model.GetBucketLifecycleConfigurationResponse) LifecycleRule(software.amazon.awssdk.services.s3.model.LifecycleRule) PutBucketLifecycleConfigurationRequest(software.amazon.awssdk.services.s3.model.PutBucketLifecycleConfigurationRequest)

Example 59 with Tag

use of software.amazon.awssdk.services.s3.model.Tag in project aws-doc-sdk-examples by awsdocs.

the class GetObjectTags method listTags.

// snippet-start:[s3.java2.getobjecttags.main]
public static void listTags(S3Client s3, String bucketName, String keyName) {
    try {
        GetObjectTaggingRequest getTaggingRequest = GetObjectTaggingRequest.builder().key(keyName).bucket(bucketName).build();
        GetObjectTaggingResponse tags = s3.getObjectTagging(getTaggingRequest);
        List<Tag> tagSet = tags.tagSet();
        // Write out the tags
        Iterator<Tag> tagIterator = tagSet.iterator();
        while (tagIterator.hasNext()) {
            Tag tag = (Tag) tagIterator.next();
            System.out.println(tag.key());
            System.out.println(tag.value());
        }
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : GetObjectTaggingRequest(software.amazon.awssdk.services.s3.model.GetObjectTaggingRequest) GetObjectTaggingResponse(software.amazon.awssdk.services.s3.model.GetObjectTaggingResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) Tag(software.amazon.awssdk.services.s3.model.Tag)

Example 60 with Tag

use of software.amazon.awssdk.services.s3.model.Tag in project aws-doc-sdk-examples by awsdocs.

the class ManagingObjectTags method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "  <bucketName> <objectKey> <objectPath> \n\n" + "Where:\n" + "  bucketName - the Amazon S3 bucket.\n" + "  objectKey - the object that a tag is applied (for example, book.pdf).\n" + "  objectPath - the path where the file is located (for example, C:/AWS/book2.pdf). \n\n";
    if (args.length != 3) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String bucketName = args[0];
    String objectKey = args[1];
    String objectPath = args[2];
    System.out.println("Putting object " + objectKey + " into bucket " + bucketName);
    System.out.println("  in bucket: " + bucketName);
    Region region = Region.US_EAST_1;
    S3Client s3 = S3Client.builder().region(region).build();
    putS3ObjectTags(s3, bucketName, objectKey, objectPath);
    updateObjectTags(s3, bucketName, objectKey);
    s3.close();
}
Also used : Region(software.amazon.awssdk.regions.Region) S3Client(software.amazon.awssdk.services.s3.S3Client)

Aggregations

Tag (com.amazonaws.services.ec2.model.Tag)38 ArrayList (java.util.ArrayList)27 Tag (com.amazonaws.services.s3.model.Tag)18 HashMap (java.util.HashMap)17 Test (org.junit.Test)16 Instance (com.amazonaws.services.ec2.model.Instance)15 List (java.util.List)15 S3FileTransferRequestParamsDto (org.finra.herd.model.dto.S3FileTransferRequestParamsDto)14 File (java.io.File)11 Map (java.util.Map)10 GetObjectTaggingRequest (com.amazonaws.services.s3.model.GetObjectTaggingRequest)9 GetObjectTaggingResult (com.amazonaws.services.s3.model.GetObjectTaggingResult)9 HashSet (java.util.HashSet)9 Utils (com.vmware.xenon.common.Utils)8 CreateTagsRequest (com.amazonaws.services.ec2.model.CreateTagsRequest)7 Reservation (com.amazonaws.services.ec2.model.Reservation)7 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)5 TagsUtil.newTagState (com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState)5 TagState (com.vmware.photon.controller.model.resources.TagService.TagState)5 DeferredResult (com.vmware.xenon.common.DeferredResult)5