Skip to content

Commit

Permalink
EPMRPP-85017 || Postfix for bucket names in binary storage (#904)
Browse files Browse the repository at this point in the history
* EPMRPP-85017 || Postfix for bucket names in binary storage

---------

Co-authored-by: Andrei Piankouski <andrei_piankouski@epam.com>
  • Loading branch information
APiankouski and APiankouski authored Jul 24, 2023
1 parent 015e23e commit 2fbd566
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,10 @@ public BlobStore minioBlobStore(@Value("${datastore.accessKey}") String accessKe
@ConditionalOnProperty(name = "datastore.type", havingValue = "minio")
public DataStore minioDataStore(@Autowired BlobStore blobStore,
@Value("${datastore.bucketPrefix}") String bucketPrefix,
@Value("${datastore.bucketPostfix}") String bucketPostfix,
@Value("${datastore.defaultBucketName}") String defaultBucketName,
@Value("${datastore.region}") String region, FeatureFlagHandler featureFlagHandler) {
return new S3DataStore(blobStore, bucketPrefix, defaultBucketName, region, featureFlagHandler);
return new S3DataStore(blobStore, bucketPrefix, bucketPostfix, defaultBucketName, region, featureFlagHandler);
}

/**
Expand Down Expand Up @@ -208,9 +209,11 @@ public BlobStore s3BlobStore(@Value("${datastore.accessKey}") String accessKey,
@ConditionalOnProperty(name = "datastore.type", havingValue = "s3")
public DataStore s3DataStore(@Autowired BlobStore blobStore,
@Value("${datastore.bucketPrefix}") String bucketPrefix,
@Value("${datastore.bucketPostfix}") String bucketPostfix,
@Value("${datastore.defaultBucketName}") String defaultBucketName,
@Value("${datastore.region}") String region, FeatureFlagHandler featureFlagHandler) {
return new S3DataStore(blobStore, bucketPrefix, defaultBucketName, region, featureFlagHandler);
return new S3DataStore(blobStore, bucketPrefix, bucketPostfix, defaultBucketName, region,
featureFlagHandler);
}

@Bean("attachmentThumbnailator")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.jclouds.blobstore.BlobStore;
Expand All @@ -36,6 +37,8 @@
import org.slf4j.LoggerFactory;

/**
* Implimitation of basic operations with blob storages.
*
* @author <a href="mailto:ivan_budayeu@epam.com">Ivan Budayeu</a>
*/
public class S3DataStore implements DataStore {
Expand All @@ -45,6 +48,7 @@ public class S3DataStore implements DataStore {

private final BlobStore blobStore;
private final String bucketPrefix;
private final String bucketPostfix;
private final String defaultBucketName;
private final Location location;

Expand All @@ -55,14 +59,16 @@ public class S3DataStore implements DataStore {
*
* @param blobStore {@link BlobStore}
* @param bucketPrefix Prefix for bucket name
* @param bucketPostfix Postfix for bucket name
* @param defaultBucketName Name of default bucket to use
* @param region Region to use
* @param featureFlagHandler {@link FeatureFlagHandler}
*/
public S3DataStore(BlobStore blobStore, String bucketPrefix, String defaultBucketName,
String region, FeatureFlagHandler featureFlagHandler) {
public S3DataStore(BlobStore blobStore, String bucketPrefix, String bucketPostfix,
String defaultBucketName, String region, FeatureFlagHandler featureFlagHandler) {
this.blobStore = blobStore;
this.bucketPrefix = bucketPrefix;
this.bucketPostfix = Objects.requireNonNullElse(bucketPostfix, "");
this.defaultBucketName = defaultBucketName;
this.location = getLocationFromString(region);
this.featureFlagHandler = featureFlagHandler;
Expand Down Expand Up @@ -126,14 +132,14 @@ private S3File getS3File(String filePath) {
}
Path targetPath = Paths.get(filePath);
int nameCount = targetPath.getNameCount();
String bucketName;
if (nameCount > 1) {
return new S3File(bucketPrefix + retrievePath(targetPath, 0, 1),
retrievePath(targetPath, 1, nameCount)
);
bucketName = bucketPrefix + retrievePath(targetPath, 0, 1) + bucketPostfix;
return new S3File(bucketName, retrievePath(targetPath, 1, nameCount));
} else {
return new S3File(defaultBucketName, retrievePath(targetPath, 0, 1));
bucketName = bucketPrefix + defaultBucketName + bucketPostfix;
return new S3File(bucketName, retrievePath(targetPath, 0, 1));
}

}

private Location getLocationFromString(String locationString) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class S3DataStoreTest {

private static final String FILE_PATH = "someFile";
private static final String BUCKET_PREFIX = "prj-";
private static final String BUCKET_POSTFIX = "-postfix";
private static final String DEFAULT_BUCKET_NAME = "rp-bucket";
private static final String REGION = "us-east-1";
private static final int ZERO = 0;
Expand All @@ -49,7 +50,8 @@ class S3DataStoreTest {
private final FeatureFlagHandler featureFlagHandler = mock(FeatureFlagHandler.class);

private final S3DataStore s3DataStore =
new S3DataStore(blobStore, BUCKET_PREFIX, DEFAULT_BUCKET_NAME, REGION, featureFlagHandler);
new S3DataStore(blobStore, BUCKET_PREFIX, BUCKET_POSTFIX, DEFAULT_BUCKET_NAME, REGION,
featureFlagHandler);

@Test
void save() throws Exception {
Expand All @@ -72,7 +74,8 @@ void save() throws Exception {

s3DataStore.save(FILE_PATH, inputStream);

verify(blobStore, times(1)).putBlob(DEFAULT_BUCKET_NAME, blobMock);
verify(blobStore, times(1))
.putBlob(BUCKET_PREFIX + DEFAULT_BUCKET_NAME + BUCKET_POSTFIX, blobMock);
}

@Test
Expand All @@ -84,7 +87,8 @@ void load() throws Exception {
when(mockPayload.openStream()).thenReturn(inputStream);
when(mockBlob.getPayload()).thenReturn(mockPayload);

when(blobStore.getBlob(DEFAULT_BUCKET_NAME, FILE_PATH)).thenReturn(mockBlob);
when(blobStore.getBlob(BUCKET_PREFIX + DEFAULT_BUCKET_NAME + BUCKET_POSTFIX,
FILE_PATH)).thenReturn(mockBlob);
InputStream loaded = s3DataStore.load(FILE_PATH);

Assertions.assertEquals(inputStream, loaded);
Expand All @@ -95,6 +99,7 @@ void delete() throws Exception {

s3DataStore.delete(FILE_PATH);

verify(blobStore, times(1)).removeBlob(DEFAULT_BUCKET_NAME, FILE_PATH);
verify(blobStore, times(1))
.removeBlob(BUCKET_PREFIX + DEFAULT_BUCKET_NAME + BUCKET_POSTFIX, FILE_PATH);
}
}

0 comments on commit 2fbd566

Please sign in to comment.