Example usage for com.amazonaws.services.s3.model GetObjectTaggingResult getTagSet

List of usage examples for com.amazonaws.services.s3.model GetObjectTaggingResult getTagSet

Introduction

In this page you can find the example usage for com.amazonaws.services.s3.model GetObjectTaggingResult getTagSet.

Prototype

public List<Tag> getTagSet() 

Source Link

Usage

From source file:org.apache.nifi.processors.aws.s3.TagS3Object.java

License:Apache License

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
    FlowFile flowFile = session.get();/*from   w ww  . j  av  a  2  s.  c  om*/
    if (flowFile == null) {
        return;
    }

    final long startNanos = System.nanoTime();

    final String bucket = context.getProperty(BUCKET).evaluateAttributeExpressions(flowFile).getValue();
    final String key = context.getProperty(KEY).evaluateAttributeExpressions(flowFile).getValue();
    final String newTagKey = context.getProperty(TAG_KEY).evaluateAttributeExpressions(flowFile).getValue();
    final String newTagVal = context.getProperty(TAG_VALUE).evaluateAttributeExpressions(flowFile).getValue();

    if (StringUtils.isBlank(bucket)) {
        failFlowWithBlankEvaluatedProperty(session, flowFile, BUCKET);
        return;
    }

    if (StringUtils.isBlank(key)) {
        failFlowWithBlankEvaluatedProperty(session, flowFile, KEY);
        return;
    }

    if (StringUtils.isBlank(newTagKey)) {
        failFlowWithBlankEvaluatedProperty(session, flowFile, TAG_KEY);
        return;
    }

    if (StringUtils.isBlank(newTagVal)) {
        failFlowWithBlankEvaluatedProperty(session, flowFile, TAG_VALUE);
        return;
    }

    final String version = context.getProperty(VERSION_ID).evaluateAttributeExpressions(flowFile).getValue();

    final AmazonS3 s3 = getClient();

    SetObjectTaggingRequest r;
    List<Tag> tags = new ArrayList<>();

    try {
        if (context.getProperty(APPEND_TAG).asBoolean()) {
            final GetObjectTaggingRequest gr = new GetObjectTaggingRequest(bucket, key);
            GetObjectTaggingResult res = s3.getObjectTagging(gr);

            // preserve tags on S3 object, but filter out existing tag keys that match the one we're setting
            tags = res.getTagSet().stream().filter(t -> !t.getKey().equals(newTagKey))
                    .collect(Collectors.toList());
        }

        tags.add(new Tag(newTagKey, newTagVal));

        if (StringUtils.isBlank(version)) {
            r = new SetObjectTaggingRequest(bucket, key, new ObjectTagging(tags));
        } else {
            r = new SetObjectTaggingRequest(bucket, key, version, new ObjectTagging(tags));
        }
        s3.setObjectTagging(r);
    } catch (final AmazonServiceException ase) {
        getLogger().error("Failed to tag S3 Object for {}; routing to failure", new Object[] { flowFile, ase });
        flowFile = session.penalize(flowFile);
        session.transfer(flowFile, REL_FAILURE);
        return;
    }

    flowFile = setTagAttributes(session, flowFile, tags);

    session.transfer(flowFile, REL_SUCCESS);
    final long transferMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos);
    getLogger().info("Successfully tagged S3 Object for {} in {} millis; routing to success",
            new Object[] { flowFile, transferMillis });
}

From source file:org.finra.herd.dao.impl.S3DaoImpl.java

License:Apache License

private void tagVersionsHelper(final S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto,
        final S3FileTransferRequestParamsDto s3ObjectTaggerParamsDto,
        final List<S3VersionSummary> s3VersionSummaries, final Tag tag) {
    // Initialize an S3 version for the error message in the catch block.
    S3VersionSummary currentS3VersionSummary = s3VersionSummaries.get(0);

    // Amazon S3 client to access S3 objects.
    AmazonS3Client s3Client = null;//from   w  ww.  jav  a 2  s  . c om

    // Amazon S3 client for S3 object tagging.
    AmazonS3Client s3ObjectTaggerClient = null;

    try {
        // Create an S3 client to access S3 objects.
        s3Client = getAmazonS3(s3FileTransferRequestParamsDto);

        // Create an S3 client for S3 object tagging.
        s3ObjectTaggerClient = getAmazonS3(s3ObjectTaggerParamsDto);

        // Create a get object tagging request.
        GetObjectTaggingRequest getObjectTaggingRequest = new GetObjectTaggingRequest(
                s3FileTransferRequestParamsDto.getS3BucketName(), null, null);

        // Create a set object tagging request.
        SetObjectTaggingRequest setObjectTaggingRequest = new SetObjectTaggingRequest(
                s3FileTransferRequestParamsDto.getS3BucketName(), null, null, null);

        for (S3VersionSummary s3VersionSummary : s3VersionSummaries) {
            // Set the current S3 version summary.
            currentS3VersionSummary = s3VersionSummary;

            // Retrieve the current tagging information for the S3 version.
            getObjectTaggingRequest.setKey(s3VersionSummary.getKey());
            getObjectTaggingRequest.setVersionId(s3VersionSummary.getVersionId());
            GetObjectTaggingResult getObjectTaggingResult = s3Operations
                    .getObjectTagging(getObjectTaggingRequest, s3Client);

            // Update the list of tags to include the specified S3 object tag.
            List<Tag> updatedTags = new ArrayList<>();
            updatedTags.add(tag);
            if (CollectionUtils.isNotEmpty(getObjectTaggingResult.getTagSet())) {
                for (Tag currentTag : getObjectTaggingResult.getTagSet()) {
                    if (!StringUtils.equals(tag.getKey(), currentTag.getKey())) {
                        updatedTags.add(currentTag);
                    }
                }
            }

            // Update tagging information for the S3 version.
            setObjectTaggingRequest.setKey(s3VersionSummary.getKey());
            setObjectTaggingRequest.setVersionId(s3VersionSummary.getVersionId());
            setObjectTaggingRequest.setTagging(new ObjectTagging(updatedTags));
            s3Operations.setObjectTagging(setObjectTaggingRequest, s3ObjectTaggerClient);
        }
    } catch (Exception e) {
        throw new IllegalStateException(String.format(
                "Failed to tag S3 object with \"%s\" key and \"%s\" version id in \"%s\" bucket. Reason: %s",
                currentS3VersionSummary.getKey(), currentS3VersionSummary.getVersionId(),
                s3FileTransferRequestParamsDto.getS3BucketName(), e.getMessage()), e);
    } finally {
        if (s3Client != null) {
            s3Client.shutdown();
        }

        if (s3ObjectTaggerClient != null) {
            s3ObjectTaggerClient.shutdown();
        }
    }
}