Search in sources :

Example 61 with Tag

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

the class S3Service method tagExistingObject.

// This method tags an existing object.
private void tagExistingObject(S3Client s3, String bucketName, String key, String label, String LabelValue) {
    try {
        // First need to get existing tag set; otherwise the existing tags are overwritten.
        GetObjectTaggingRequest getObjectTaggingRequest = GetObjectTaggingRequest.builder().bucket(bucketName).key(key).build();
        GetObjectTaggingResponse response = s3.getObjectTagging(getObjectTaggingRequest);
        // Get the existing immutable list - cannot modify this list.
        List<Tag> existingList = response.tagSet();
        ArrayList<Tag> newTagList = new ArrayList(new ArrayList<>(existingList));
        // Create a new tag.
        Tag myTag = Tag.builder().key(label).value(LabelValue).build();
        // push new tag to list.
        newTagList.add(myTag);
        Tagging tagging = Tagging.builder().tagSet(newTagList).build();
        PutObjectTaggingRequest taggingRequest = PutObjectTaggingRequest.builder().key(key).bucket(bucketName).tagging(tagging).build();
        s3.putObjectTagging(taggingRequest);
        System.out.println(key + " was tagged with " + label);
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : GetObjectTaggingRequest(software.amazon.awssdk.services.s3.model.GetObjectTaggingRequest) PutObjectTaggingRequest(software.amazon.awssdk.services.s3.model.PutObjectTaggingRequest) GetObjectTaggingResponse(software.amazon.awssdk.services.s3.model.GetObjectTaggingResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ArrayList(java.util.ArrayList) Tagging(software.amazon.awssdk.services.s3.model.Tagging) Tag(software.amazon.awssdk.services.s3.model.Tag)

Example 62 with Tag

use of software.amazon.awssdk.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();
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) LifecycleAndOperator(com.amazonaws.services.s3.model.lifecycle.LifecycleAndOperator) LifecycleFilter(com.amazonaws.services.s3.model.lifecycle.LifecycleFilter) Regions(com.amazonaws.regions.Regions) BucketLifecycleConfiguration(com.amazonaws.services.s3.model.BucketLifecycleConfiguration) LifecyclePrefixPredicate(com.amazonaws.services.s3.model.lifecycle.LifecyclePrefixPredicate) SdkClientException(com.amazonaws.SdkClientException) LifecycleTagPredicate(com.amazonaws.services.s3.model.lifecycle.LifecycleTagPredicate) Transition(com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Transition) AmazonServiceException(com.amazonaws.AmazonServiceException) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) Tag(com.amazonaws.services.s3.model.Tag)

Example 63 with Tag

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

the class GetObjectTags2 method main.

public static void main(String[] args) {
    if (args.length < 2) {
        System.out.println("Please specify a bucket name and key name");
        System.exit(1);
    }
    // snippet-start:[s3.java.getobjecttags.main]
    String bucketName = args[0];
    String keyName = args[1];
    System.out.println("Retrieving Object Tags for  " + keyName);
    final AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRegion(Regions.DEFAULT_REGION).build();
    try {
        GetObjectTaggingRequest getTaggingRequest = new GetObjectTaggingRequest(bucketName, keyName);
        GetObjectTaggingResult tags = s3.getObjectTagging(getTaggingRequest);
        List<Tag> tagSet = tags.getTagSet();
        // Iterate through the list
        Iterator<Tag> tagIterator = tagSet.iterator();
        while (tagIterator.hasNext()) {
            Tag tag = (Tag) tagIterator.next();
            System.out.println(tag.getKey());
            System.out.println(tag.getValue());
        }
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
// snippet-end:[s3.java.getobjecttags.main]
}
Also used : GetObjectTaggingRequest(com.amazonaws.services.s3.model.GetObjectTaggingRequest) AmazonS3(com.amazonaws.services.s3.AmazonS3) AmazonServiceException(com.amazonaws.AmazonServiceException) Tag(com.amazonaws.services.s3.model.Tag) GetObjectTaggingResult(com.amazonaws.services.s3.model.GetObjectTaggingResult)

Example 64 with Tag

use of software.amazon.awssdk.services.s3.model.Tag in project SimianArmy by Netflix.

the class AWSClient method createTagsForResources.

@Override
public void createTagsForResources(Map<String, String> keyValueMap, String... resourceIds) {
    Validate.notNull(keyValueMap);
    Validate.notEmpty(keyValueMap);
    Validate.notNull(resourceIds);
    Validate.notEmpty(resourceIds);
    AmazonEC2 ec2Client = ec2Client();
    List<Tag> tags = new ArrayList<Tag>();
    for (Map.Entry<String, String> entry : keyValueMap.entrySet()) {
        tags.add(new Tag(entry.getKey(), entry.getValue()));
    }
    CreateTagsRequest req = new CreateTagsRequest(Arrays.asList(resourceIds), tags);
    ec2Client.createTags(req);
}
Also used : AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) Tag(com.amazonaws.services.ec2.model.Tag)

Example 65 with Tag

use of software.amazon.awssdk.services.s3.model.Tag in project SimianArmy by Netflix.

the class EBSVolumeJanitorCrawler method getVolumeResources.

private List<Resource> getVolumeResources(String... volumeIds) {
    List<Resource> resources = new LinkedList<Resource>();
    AWSClient awsClient = getAWSClient();
    for (Volume volume : awsClient.describeVolumes(volumeIds)) {
        Resource volumeResource = new AWSResource().withId(volume.getVolumeId()).withRegion(getAWSClient().region()).withResourceType(AWSResourceType.EBS_VOLUME).withLaunchTime(volume.getCreateTime());
        for (Tag tag : volume.getTags()) {
            LOGGER.info(String.format("Adding tag %s = %s to resource %s", tag.getKey(), tag.getValue(), volumeResource.getId()));
            volumeResource.setTag(tag.getKey(), tag.getValue());
        }
        volumeResource.setOwnerEmail(getOwnerEmailForResource(volumeResource));
        volumeResource.setDescription(getVolumeDescription(volume));
        ((AWSResource) volumeResource).setAWSResourceState(volume.getState());
        resources.add(volumeResource);
    }
    return resources;
}
Also used : Volume(com.amazonaws.services.ec2.model.Volume) AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) AWSClient(com.netflix.simianarmy.client.aws.AWSClient) Tag(com.amazonaws.services.ec2.model.Tag) LinkedList(java.util.LinkedList)

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