@Override public SetObjectTaggingResult setObjectTagging(SetObjectTaggingRequest setObjectTaggingRequest, AmazonS3 s3Client) { MockS3Object mockS3Object = getMockS3Object(setObjectTaggingRequest.getBucketName(), setObjectTaggingRequest.getKey()); if (setObjectTaggingRequest.getTagging() != null) { mockS3Object.setTags(setObjectTaggingRequest.getTagging().getTagSet()); } else { mockS3Object.setTags(null); } return new SetObjectTaggingResult(); }
private void changeExistingObject( Record record, ELVars variables, String bucket, String objectPath ) throws OnRecordErrorException { // Tag application if(!config.taskConfig.tags.isEmpty()) { List<Tag> newTags = new ArrayList<>(); // Evaluate each tag separately for (Map.Entry<String, String> entry : config.taskConfig.tags.entrySet()) { newTags.add(new Tag( evaluate(record, "tags", variables, entry.getKey()), evaluate(record, "tags", variables, entry.getValue()) )); } // Apply all tags at once config.s3Config.getS3Client().setObjectTagging(new SetObjectTaggingRequest( bucket, objectPath, new ObjectTagging(newTags) )); } }
@Override public void process(List<Label> labels, String path) { Processor.PathSplit components = new Processor.PathSplit(path); String bucket = components.bucket; String key = components.key; // fetch the current set GetObjectTaggingResult tagging = s3.getObjectTagging(new GetObjectTaggingRequest(bucket, key)); List<Tag> origTags = tagging.getTagSet(); List<Tag> updateTags = new ArrayList<>(); // copy the existing tags, but drop the ones matched by prefix (∴ leaves non-Rekognition label tags alone) for (Tag tag : origTags) { if (!tag.getKey().startsWith(tagPrefix)) updateTags.add(tag); } // add the new ones for (Label label : labels) { if (updateTags.size() < maxTags) updateTags.add(new Tag(tagPrefix + label.getName(), label.getConfidence().toString())); else break; } // save it back s3.setObjectTagging(new SetObjectTaggingRequest(bucket, key, new ObjectTagging(updateTags))); }
SetObjectTaggingRequest withDeletedTag(final String key) { return new SetObjectTaggingRequest( getConfiguredBucket(), key, new ObjectTagging(singletonList(DELETED_TAG)) ); }
@Override @Guarded(by = STARTED) public boolean delete(final BlobId blobId, String reason) { checkNotNull(blobId); final S3Blob blob = liveBlobs.getUnchecked(blobId); Lock lock = blob.lock(); try { log.debug("Soft deleting blob {}", blobId); S3BlobAttributes blobAttributes = new S3BlobAttributes(s3, getConfiguredBucket(), attributePath(blobId).toString()); boolean loaded = blobAttributes.load(); if (!loaded) { // This could happen under some concurrent situations (two threads try to delete the same blob) // but it can also occur if the deleted index refers to a manually-deleted blob. log.warn("Attempt to mark-for-delete non-existent blob {}", blobId); return false; } else if (blobAttributes.isDeleted()) { log.debug("Attempt to delete already-deleted blob {}", blobId); return false; } blobAttributes.setDeleted(true); blobAttributes.setDeletedReason(reason); blobAttributes.store(); // set "deleted=true" tag on the object, let S3 take care of deleting the blob after it expires s3.setObjectTagging( new SetObjectTaggingRequest( getConfiguredBucket(), contentPath(blobId), new ObjectTagging(Arrays.asList(DELETED_TAG)) ) ); blob.markStale(); return true; } catch (Exception e) { throw new BlobStoreException(e, blobId); } finally { lock.unlock(); } }
@Override public SetObjectTaggingResult setObjectTagging(SetObjectTaggingRequest setObjectTaggingRequest) { return call(() -> getDelegate().setObjectTagging(setObjectTaggingRequest)); }
@Override public SetObjectTaggingResult setObjectTagging(SetObjectTaggingRequest setObjectTaggingRequest, AmazonS3 s3Client) { return s3Client.setObjectTagging(setObjectTaggingRequest); }
@Override public void tagObjects(final S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto, final S3FileTransferRequestParamsDto s3ObjectTaggerParamsDto, final Tag tag) { LOGGER.info("Tagging objects in S3... s3BucketName=\"{}\" s3KeyCount={} s3ObjectTagKey=\"{}\" s3ObjectTagValue=\"{}\"", s3FileTransferRequestParamsDto.getS3BucketName(), s3FileTransferRequestParamsDto.getFiles().size(), tag.getKey(), tag.getValue()); if (!CollectionUtils.isEmpty(s3FileTransferRequestParamsDto.getFiles())) { // Initialize a key value pair for the error message in the catch block. String s3Key = s3FileTransferRequestParamsDto.getFiles().get(0).getPath().replaceAll("\\\\", "/"); // 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); // Create a restore object request. SetObjectTaggingRequest setObjectTaggingRequest = new SetObjectTaggingRequest(s3FileTransferRequestParamsDto.getS3BucketName(), null, null); for (File file : s3FileTransferRequestParamsDto.getFiles()) { // Prepare an S3 key. s3Key = file.getPath().replaceAll("\\\\", "/"); // Retrieve the current tagging information for the S3 key. getObjectTaggingRequest.setKey(s3Key); 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 the tagging information. setObjectTaggingRequest.setKey(s3Key); 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 in \"%s\" bucket. Reason: %s", s3Key, s3FileTransferRequestParamsDto.getS3BucketName(), e.getMessage()), e); } finally { if (s3Client != null) { s3Client.shutdown(); } if (s3ObjectTaggerClient != null) { s3ObjectTaggerClient.shutdown(); } } } }
@Test public void testTagObjects() { // Create an S3 file transfer request parameters DTO to access S3 objects. S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = new S3FileTransferRequestParamsDto(); s3FileTransferRequestParamsDto.setS3BucketName(S3_BUCKET_NAME); s3FileTransferRequestParamsDto.setFiles(Arrays.asList(new File(S3_KEY_PREFIX + "/" + LOCAL_FILE))); // Create an S3 file transfer request parameters DTO to tag S3 objects. S3FileTransferRequestParamsDto s3ObjectTaggerParamsDto = new S3FileTransferRequestParamsDto(); s3ObjectTaggerParamsDto.setAwsAccessKeyId(AWS_ASSUMED_ROLE_ACCESS_KEY); s3ObjectTaggerParamsDto.setAwsSecretKey(AWS_ASSUMED_ROLE_SECRET_KEY); s3ObjectTaggerParamsDto.setSessionToken(AWS_ASSUMED_ROLE_SESSION_TOKEN); // Create an S3 object tag. Tag tag = new Tag(S3_OBJECT_TAG_KEY, S3_OBJECT_TAG_VALUE); // Create a retry policy. RetryPolicy retryPolicy = new RetryPolicy(PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION, PredefinedRetryPolicies.DEFAULT_BACKOFF_STRATEGY, INTEGER_VALUE, true); // Create a get object tagging result. GetObjectTaggingResult getObjectTaggingResult = new GetObjectTaggingResult(null); // Create a set object tagging result. SetObjectTaggingResult setObjectTaggingResult = new SetObjectTaggingResult(); // Mock the external calls. when(retryPolicyFactory.getRetryPolicy()).thenReturn(retryPolicy); when(s3Operations.getObjectTagging(any(GetObjectTaggingRequest.class), any(AmazonS3Client.class))).thenReturn(getObjectTaggingResult); when(s3Operations.setObjectTagging(any(SetObjectTaggingRequest.class), any(AmazonS3Client.class))).thenReturn(setObjectTaggingResult); // Call the method under test. s3DaoImpl.tagObjects(s3FileTransferRequestParamsDto, s3ObjectTaggerParamsDto, tag); // Verify the external calls. verify(retryPolicyFactory, times(2)).getRetryPolicy(); verify(s3Operations).getObjectTagging(any(GetObjectTaggingRequest.class), any(AmazonS3Client.class)); verify(s3Operations).setObjectTagging(any(SetObjectTaggingRequest.class), any(AmazonS3Client.class)); verifyNoMoreInteractionsHelper(); }
/** * Set the tags for the specified object. * * @param setObjectTaggingRequest the request object containing all the options for setting the tags for the specified object * @param s3Client the {@link AmazonS3} implementation to use * * @return the result of the set operation */ public SetObjectTaggingResult setObjectTagging(SetObjectTaggingRequest setObjectTaggingRequest, AmazonS3 s3Client);