Example usage for com.amazonaws.services.s3.model GetObjectTaggingRequest GetObjectTaggingRequest

List of usage examples for com.amazonaws.services.s3.model GetObjectTaggingRequest GetObjectTaggingRequest

Introduction

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

Prototype

public GetObjectTaggingRequest(String bucketName, String key) 

Source Link

Document

Construct an instance of this object.

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 w w.  j a va2  s. c o  m
    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 });
}