S3BinaryStore extends AbstractBinaryStore { @Override public BinaryValue storeValue(InputStream stream, boolean markAsUnused) throws BinaryStoreException { BinaryValue cachedFile = fileSystemCache.storeValue(stream, false); try { BinaryKey key = new BinaryKey(cachedFile.getKey().toString()); if (!s3Client.doesObjectExist(bucketName, key.toString())) { ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(cachedFile.getSize()); metadata.setContentType(fileSystemCache.getMimeType(cachedFile, key.toString())); s3Client.putObject(bucketName, key.toString(), fileSystemCache.getInputStream(key), metadata); } if (markAsUnused) { markAsUnused(Collections.singleton(key)); } else { markAsUsed(Collections.singleton(key)); } return new StoredBinaryValue(this, key, cachedFile.getSize()); } catch (AmazonClientException|RepositoryException |IOException e) { throw new BinaryStoreException(e); } finally { fileSystemCache.markAsUnused(Collections.singleton(cachedFile.getKey())); fileSystemCache.removeValuesUnusedLongerThan(1, TimeUnit.MICROSECONDS); } } S3BinaryStore(String accessKey, String secretKey, String bucketName);  S3BinaryStore(String accessKey, String secretKey, String bucketName, String endPoint);  S3BinaryStore( String accessKey,
                          String secretKey,
                          String bucketName,
                          String endPoint,
                          Boolean deleteUnusedNatively ); protected  S3BinaryStore(String bucketName, AmazonS3 s3Client, boolean deleteUnusedNatively); @Override void storeExtractedText(BinaryValue binaryValue, String extractedText); @Override String getExtractedText(BinaryValue binaryValue); @Override BinaryValue storeValue(InputStream stream, boolean markAsUnused); @Override InputStream getInputStream(BinaryKey key); @Override void markAsUsed(Iterable<BinaryKey> keys); @Override void markAsUnused(Iterable<BinaryKey> keys); @Override void removeValuesUnusedLongerThan(long minimumAge, TimeUnit timeUnit); @Override Iterable<BinaryKey> getAllBinaryKeys(); void migrateUnusedMetadataToTags();  }
@Test public void testStoreValueWhenAbsent() throws BinaryStoreException, IOException { String valueToStore = "value-to-store"; expect(s3Client.doesObjectExist(eq(BUCKET), isA(String.class))).andReturn(false); expect(s3Client.putObject(eq(BUCKET), isA(String.class), isA(InputStream.class), isA(ObjectMetadata.class))) .andReturn(null); Capture<GetObjectTaggingRequest> getTaggingRequestCapture = Capture.newInstance(); GetObjectTaggingResult getTaggingResult = new GetObjectTaggingResult(new ArrayList<>()); expect(s3Client.getObjectTagging(capture(getTaggingRequestCapture))) .andReturn(getTaggingResult); Capture<SetObjectTaggingRequest> setTaggingRequestCapture = Capture.newInstance(); expect(s3Client.setObjectTagging(capture(setTaggingRequestCapture))) .andReturn(new SetObjectTaggingResult()); replayAll(); s3BinaryStore.storeValue(new StringInputStream(valueToStore), false); SetObjectTaggingRequest setTaggingRequest = setTaggingRequestCapture.getValue(); assertUnusedTagInSetTaggingRequest(setTaggingRequest, String.valueOf(false)); }
@Test public void testStoreValueWhenExisting() throws BinaryStoreException, IOException { String valueToStore = "value-to-store"; expect(s3Client.doesObjectExist(eq(BUCKET), isA(String.class))).andReturn(true); Capture<GetObjectTaggingRequest> getTaggingRequestCapture = Capture.newInstance(); GetObjectTaggingResult getTaggingResult = new GetObjectTaggingResult(new ArrayList<>()); expect(s3Client.getObjectTagging(capture(getTaggingRequestCapture))) .andReturn(getTaggingResult); Capture<SetObjectTaggingRequest> setTaggingRequestCapture = Capture.newInstance(); expect(s3Client.setObjectTagging(capture(setTaggingRequestCapture))) .andReturn(new SetObjectTaggingResult()); replayAll(); s3BinaryStore.storeValue(new StringInputStream(valueToStore), false); SetObjectTaggingRequest setTaggingRequest = setTaggingRequestCapture.getValue(); assertUnusedTagInSetTaggingRequest(setTaggingRequest, String.valueOf(false)); }
@Test public void testStoreValueWhenExistingUnused() throws BinaryStoreException, IOException { String valueToStore = "value-to-store"; expect(s3Client.doesObjectExist(eq(BUCKET), isA(String.class))).andReturn(true); Capture<GetObjectTaggingRequest> getTaggingRequestCapture = Capture.newInstance(); GetObjectTaggingResult getTaggingResult = new GetObjectTaggingResult(new ArrayList<>()); getTaggingResult.getTagSet().add(new Tag(S3BinaryStore.UNUSED_TAG_KEY, String.valueOf(true))); expect(s3Client.getObjectTagging(capture(getTaggingRequestCapture))) .andReturn(getTaggingResult); Capture<SetObjectTaggingRequest> setTaggingRequestCapture = Capture.newInstance(); expect(s3Client.setObjectTagging(capture(setTaggingRequestCapture))) .andReturn(new SetObjectTaggingResult()); replayAll(); s3BinaryStore.storeValue(new StringInputStream(valueToStore), false); SetObjectTaggingRequest setTaggingRequest = setTaggingRequestCapture.getValue(); assertUnusedTagInSetTaggingRequest(setTaggingRequest, String.valueOf(false)); }
@Test public void testStoreUnusedValueWhenAbsent() throws BinaryStoreException, IOException { String valueToStore = "value-to-store"; expect(s3Client.doesObjectExist(eq(BUCKET), isA(String.class))).andReturn(false); expect(s3Client.putObject(eq(BUCKET), isA(String.class), isA(InputStream.class), isA(ObjectMetadata.class))) .andReturn(null); Capture<GetObjectTaggingRequest> getTaggingRequestCapture = Capture.newInstance(); GetObjectTaggingResult getTaggingResult = new GetObjectTaggingResult(new ArrayList<>()); expect(s3Client.getObjectTagging(capture(getTaggingRequestCapture))) .andReturn(getTaggingResult); Capture<SetObjectTaggingRequest> setTaggingRequestCapture = Capture.newInstance(); expect(s3Client.setObjectTagging(capture(setTaggingRequestCapture))) .andReturn(new SetObjectTaggingResult()); replayAll(); s3BinaryStore.storeValue(new StringInputStream(valueToStore), true); SetObjectTaggingRequest setTaggingRequest = setTaggingRequestCapture.getValue(); assertUnusedTagInSetTaggingRequest(setTaggingRequest, String.valueOf(true)); }
@Test public void testStoreUnusedValueWhenExisting() throws BinaryStoreException, IOException { String valueToStore = "value-to-store"; expect(s3Client.doesObjectExist(eq(BUCKET), isA(String.class))).andReturn(true); Capture<GetObjectTaggingRequest> getTaggingRequestCapture = Capture.newInstance(); GetObjectTaggingResult getTaggingResult = new GetObjectTaggingResult(new ArrayList<>()); getTaggingResult.getTagSet().add(new Tag(S3BinaryStore.UNUSED_TAG_KEY, String.valueOf(false))); expect(s3Client.getObjectTagging(capture(getTaggingRequestCapture))) .andReturn(getTaggingResult); Capture<SetObjectTaggingRequest> setTaggingRequestCapture = Capture.newInstance(); expect(s3Client.setObjectTagging(capture(setTaggingRequestCapture))) .andReturn(new SetObjectTaggingResult()); replayAll(); s3BinaryStore.storeValue(new StringInputStream(valueToStore), true); SetObjectTaggingRequest setTaggingRequest = setTaggingRequestCapture.getValue(); assertUnusedTagInSetTaggingRequest(setTaggingRequest, String.valueOf(true)); }
@Test public void testStoreUnusedValueWhenExistingUnused() throws BinaryStoreException, IOException { String valueToStore = "value-to-store"; expect(s3Client.doesObjectExist(eq(BUCKET), isA(String.class))).andReturn(true); Capture<GetObjectTaggingRequest> getTaggingRequestCapture = Capture.newInstance(); GetObjectTaggingResult getTaggingResult = new GetObjectTaggingResult(new ArrayList<>()); getTaggingResult.getTagSet().add(new Tag(S3BinaryStore.UNUSED_TAG_KEY, String.valueOf(true))); expect(s3Client.getObjectTagging(capture(getTaggingRequestCapture))) .andReturn(getTaggingResult); replayAll(); s3BinaryStore.storeValue(new StringInputStream(valueToStore), true); }