Java Code Examples for com.amazonaws.services.s3.model.GetObjectTaggingResult#getTagSet()

The following examples show how to use com.amazonaws.services.s3.model.GetObjectTaggingResult#getTagSet() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ListS3.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void addToListing(final S3VersionSummary versionSummary, final GetObjectTaggingResult taggingResult, final ObjectMetadata objectMetadata) {
    // Create the attributes
    final Map<String, String> attributes = new HashMap<>();
    attributes.put(CoreAttributes.FILENAME.key(), versionSummary.getKey());
    attributes.put("s3.bucket", versionSummary.getBucketName());
    if (versionSummary.getOwner() != null) { // We may not have permission to read the owner
        attributes.put("s3.owner", versionSummary.getOwner().getId());
    }
    attributes.put("s3.etag", versionSummary.getETag());
    attributes.put("s3.lastModified", String.valueOf(versionSummary.getLastModified().getTime()));
    attributes.put("s3.length", String.valueOf(versionSummary.getSize()));
    attributes.put("s3.storeClass", versionSummary.getStorageClass());
    attributes.put("s3.isLatest", String.valueOf(versionSummary.isLatest()));
    if (versionSummary.getVersionId() != null) {
        attributes.put("s3.version", versionSummary.getVersionId());
    }

    if (taggingResult != null) {
        final List<Tag> tags = taggingResult.getTagSet();
        for (final Tag tag : tags) {
            attributes.put("s3.tag." + tag.getKey(), tag.getValue());
        }
    }

    if (objectMetadata != null) {
        for (Map.Entry<String, String> e : objectMetadata.getUserMetadata().entrySet()) {
            attributes.put("s3.user.metadata." + e.getKey(), e.getValue());
        }
    }

    // Create the flowfile
    FlowFile flowFile = session.create();
    flowFile = session.putAllAttributes(flowFile, attributes);
    session.transfer(flowFile, REL_SUCCESS);
}
 
Example 2
Source File: ITPutS3Object.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testObjectTags() throws IOException, InterruptedException {
    final TestRunner runner = TestRunners.newTestRunner(new PutS3Object());
    runner.setProperty(PutS3Object.CREDENTIALS_FILE, CREDENTIALS_FILE);
    runner.setProperty(PutS3Object.REGION, REGION);
    runner.setProperty(PutS3Object.BUCKET, BUCKET_NAME);
    runner.setProperty(PutS3Object.OBJECT_TAGS_PREFIX, "tagS3");
    runner.setProperty(PutS3Object.REMOVE_TAG_PREFIX, "true");

    final Map<String, String> attrs = new HashMap<>();
    attrs.put("filename", "tag-test.txt");
    attrs.put("tagS3PII", "true");
    runner.enqueue(getResourcePath(SAMPLE_FILE_RESOURCE_NAME), attrs);

    runner.run();
    runner.assertAllFlowFilesTransferred(PutS3Object.REL_SUCCESS, 1);

    GetObjectTaggingResult result = client.getObjectTagging(new GetObjectTaggingRequest(BUCKET_NAME, "tag-test.txt"));
    List<Tag> objectTags = result.getTagSet();

    for (Tag tag : objectTags) {
        System.out.println("Tag Key : " + tag.getKey() + ", Tag Value : " + tag.getValue());
    }

    Assert.assertTrue(objectTags.size() == 1);
    Assert.assertEquals("PII", objectTags.get(0).getKey());
    Assert.assertEquals("true", objectTags.get(0).getValue());
}
 
Example 3
Source File: Configuration.java    From bidder with Apache License 2.0 4 votes vote down vote up
public void processDirectory(AmazonS3 s3, ObjectListing listing, String bucket) throws Exception {

		double time = System.currentTimeMillis();
		ExecutorService executor = Executors.newFixedThreadPool(16);

		int count = 0;

		for (S3ObjectSummary objectSummary : listing.getObjectSummaries()) {
			if ("STANDARD".equalsIgnoreCase(objectSummary.getStorageClass())) {
				long size = objectSummary.getSize();
				logger.debug("*** Processing S3 {}, size: {}", objectSummary.getKey(), size);
				S3Object object = s3.getObject(new GetObjectRequest(bucket, objectSummary.getKey()));

				String bucketName = object.getBucketName();
				String keyName = object.getKey();

				GetObjectTaggingRequest request = new GetObjectTaggingRequest(bucketName, keyName);
				GetObjectTaggingResult result = s3.getObjectTagging(request);
				List<Tag> tags = result.getTagSet();
				String type = null;
				String name = null;

				if (tags.isEmpty()) {
					object.close();
					logger.warn("Error, S3 object: {} has no tags", keyName);
				} else {
					for (Tag tag : tags) {
						String key = tag.getKey();
						String value = tag.getValue();

						if (key.equals("type")) {
							type = value;
						}

						if (key.equals("name")) {
							name = value;
						}
					}

					if (name == null) {
						object.close();
						throw new Exception("Error: " + keyName + " is missing a name tag");
					}
					if (name.contains(" ")) {
						object.close();
						throw new Exception("Error: " + keyName + " has a name attribute with a space in it");
					}
					if (type == null) {
						object.close();
						throw new Exception("Error: " + keyName + " has no type tag");
					}

					if (!name.startsWith("$"))
						name = "$" + name;

					// The runnable will call object.close();
					Runnable w = new AwsWorker(type, name, object, size);
					executor.execute(w);

					count++;
				}
			}
		}
		executor.shutdown();
		executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);

		time = System.currentTimeMillis() - time;
		time = time / 60000;
		logger.info("Initialized all {} S3 objects in {} minutes", count, time);
	}
 
Example 4
Source File: S3DaoImpl.java    From herd with Apache License 2.0 4 votes vote down vote up
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;

    // 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();
        }
    }
}
 
Example 5
Source File: Configuration.java    From XRTB with Apache License 2.0 4 votes vote down vote up
public void processDirectory(AmazonS3Client s3, ObjectListing listing, String bucket) throws Exception {
	for (S3ObjectSummary objectSummary : listing.getObjectSummaries()) {
		long size = objectSummary.getSize();
		logger.info("*** Processing S3 {}, size: {}",objectSummary.getKey(),size);
		S3Object object = s3.getObject(new GetObjectRequest(bucket, objectSummary.getKey()));

		String bucketName = object.getBucketName();
		String keyName = object.getKey();

		GetObjectTaggingRequest request = new GetObjectTaggingRequest(bucketName, keyName);
		GetObjectTaggingResult result = s3.getObjectTagging(request);
		List<Tag> tags = result.getTagSet();
		String type = null;
		String name = null;

		if (tags.isEmpty()) {
			System.err.println("Error: " + keyName + " has no tags");
		} else {
			for (Tag tag : tags) {
				String key = tag.getKey();
				String value = tag.getValue();

				if (key.equals("type")) {
					type = value;
				}

				if (key.equals("name")) {
					name = value;
				}
			}

			if (name == null)
				throw new Exception("Error: " + keyName + " is missing a name tag");
			if (name.contains(" "))
				throw new Exception("Error: " + keyName + " has a name attribute with a space in it");
			if (type == null)
				throw new Exception("Error: " + keyName + " has no type tag");

			if (!name.startsWith("$"))
				name = "$" + name;

			readData(type, name, object, size);
		}
	}
}
 
Example 6
Source File: GetObjectTags2.java    From aws-doc-sdk-examples with Apache License 2.0 3 votes vote down vote up
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]
    }