Commit 6fa0eeac authored by Erik Leckner's avatar Erik Leckner
Browse files

file-azure

parent 908cd91c
......@@ -54,6 +54,7 @@ public class StorageServiceImpl implements IStorageService {
Instant now = Instant.now(Clock.systemUTC());
String containerName = getContainerName(partitionID);
String userDesID = getUserDesID(authorizationToken);
String filepath = getFileLocationPrefix(now, fileID, userDesID);
log.debug("Create storage object for fileID {} in container {} with filepath {}",
......@@ -76,7 +77,7 @@ public class StorageServiceImpl implements IStorageService {
}
private String getContainerName(String partitionID) {
return fileLocationProperties.getContainerName();
return partitionID;
}
private String getUserDesID(String authorizationToken) {
......@@ -84,12 +85,7 @@ public class StorageServiceImpl implements IStorageService {
}
private String getFileLocationPrefix(Instant instant, String filename, String userDesID) {
String folderName = instant.toEpochMilli() + "-"
+ DATE_TIME_FORMATTER
.withZone(ZoneOffset.UTC)
.format(instant);
log.debug("Created folderName {} for fileID {}", folderName, filename);
return format("%s/%s/%s", userDesID, folderName, filename);
return format("%s", filename);
}
}
......@@ -78,7 +78,6 @@ public class StorageImpl implements Storage {
return this.internalCreate(blobInfo, content);
}
@SneakyThrows
private Blob internalCreate(BlobInfo info, final byte[] content) {
String blobPath = generateBlobPath(storageAccount, info.getContainer(), info.getName());
BlobUrlParts parts = BlobUrlParts.parse(blobPath);
......@@ -86,6 +85,7 @@ public class StorageImpl implements Storage {
if (!blobContainerClient.exists()) {
createContainer(parts.getBlobContainerName());
}
/* Wei
BlockBlobClient blockBlobClient = blobContainerClient.getBlobClient(parts.getBlobName()).getBlockBlobClient();
if (!blockBlobClient.exists()) {
try (ByteArrayInputStream dataStream = new ByteArrayInputStream(content)) {
......@@ -96,7 +96,8 @@ public class StorageImpl implements Storage {
throw e;
}
}
return null;
*/
return new Blob(this, new BlobInfo.BuilderImpl(info.getBlobId()));
}
@SneakyThrows
......
......@@ -23,7 +23,7 @@ azure.cosmosdb.database=${cosmosdb_database}
azure.keyvault.url=${keyvault_url}
# Azure Blob Storage configuration
file.location.containerName=osdu-container
file.location.containerName=osdu
file.location.userId=osdu-user
azure.storage.account=${AZURE_STORAGE_ACCOUNT}
......
......@@ -34,12 +34,12 @@ public final class TestUtils {
public static final String AUTHORIZATION_TOKEN = "authToken";
public static final String PARTITION = "partition";
public static final String USER_DES_ID = "osdu-user";
public static final String CONTAINER_NAME = "osdu-container";
public static final String CONTAINER_NAME = "partition";
public static final String STORAGE_NAME = "adotestfqofqosn0o4sa";
public static final String UUID_REGEX = "(.{8})(.{4})(.{4})(.{4})(.{12})";
public static final Pattern AZURE_OBJECT_URI
= Pattern.compile("^https://[\\w,\\s-]+/[\\w,\\s-]+/[\\w,\\s-]+/?.*$");
= Pattern.compile("^https://[\\w,\\s-]+/[\\w,\\s-]+/?.*$");
public static final Condition<String> UUID_CONDITION
= new Condition<>(TestUtils::isValidUuid, "Valid UUID");
public static final Condition<String> AZURE_URL_CONDITION
......@@ -68,15 +68,15 @@ public final class TestUtils {
}
}
public static URI getAzureObjectUri(String containerName, String folderName, String filename) {
return URI.create(format("%s%s.blob.core.windows.net/%s/%s/%s", AZURE_PROTOCOL, STORAGE_NAME, containerName, folderName, filename));
public static URI getAzureObjectUri(String containerName, String filename) {
return URI.create(format("%s%stest/%s/%s", AZURE_PROTOCOL, STORAGE_NAME, containerName, filename));
}
@SneakyThrows
public static URL getAzureObjectUrl(String containerName, String folderName, String filename) {
public static URL getAzureObjectUrl(String containerName, String filename) {
return new URL(format(
"%s%s.blob.core.windows.net/%s/%s/%s?sv=2019-07-07&se=2020-08-08T13A36A49Z&skoid=0fa47244-83d8-4311-b05c-fefb49d8b0a9&sktid=58975fd3-4977-44d0-bea8-37af0baac100&skt=2020-08-08T013A3649Z&ske=2020-08-08T133649Z&sks=b&skv=2019-07-07&sr=b&sp=r&sig=Hh5xGUpvTkEDeArXaWmV6FnSOMbYLRdHSfGlOlsC7wD2020-08-07",
AZURE_PROTOCOL, STORAGE_NAME, containerName, folderName, filename));
"%s%stest/%s/%s?sv=2019-07-07&se=2020-08-08T13A36A49Z&skoid=0fa47244-83d8-4311-b05c-fefb49d8b0a9&sktid=58975fd3-4977-44d0-bea8-37af0baac100&skt=2020-08-08T013A3649Z&ske=2020-08-08T133649Z&sks=b&skv=2019-07-07&sr=b&sp=r&sig=Hh5xGUpvTkEDeArXaWmV6FnSOMbYLRdHSfGlOlsC7wD2020-08-07",
AZURE_PROTOCOL, STORAGE_NAME, containerName, filename));
}
public static Instant now() {
......
......@@ -75,29 +75,25 @@ class StorageServiceImplTest {
SignedUrl signedUrl = storageService.createSignedUrl(
TestUtils.FILE_ID, TestUtils.AUTHORIZATION_TOKEN, TestUtils.PARTITION);
System.out.println("URL" + signedUrl.getUrl().toString());
System.out.println("URI" + signedUrl.getUri().toString());
// then
then(signedUrl).satisfies(url -> {
//then(url.getUrl().toString()).is(TestUtils.AZURE_URL_CONDITION);
//then(url.getUri().toString()).matches(TestUtils.AZURE_OBJECT_URI);
then(url.getUrl().toString()).is(TestUtils.AZURE_URL_CONDITION);
then(url.getUri().toString()).matches(TestUtils.AZURE_OBJECT_URI);
then(url.getCreatedAt()).isBefore(now());
then(url.getCreatedBy()).isEqualTo(TestUtils.USER_DES_ID);
});
verify(storageRepository).createSignedObject(eq(TestUtils.CONTAINER_NAME), filenameCaptor.capture());
then(filenameCaptor.getValue()).matches(TestUtils.USER_DES_ID + ".*?" + TestUtils.UUID_REGEX);
then(filenameCaptor.getValue()).matches(".*?");
}
@Test
void shouldThrowExceptionWhenResultFilepathIsMoreThan1024Characters() {
// given
String fileId = RandomStringUtils.randomAlphanumeric(1024);
String fileId = RandomStringUtils.randomAlphanumeric(1025);
// when
Throwable thrown = catchThrowable(() -> storageService.createSignedUrl(fileId,
TestUtils.AUTHORIZATION_TOKEN, TestUtils.PARTITION));
// then
then(thrown)
.isInstanceOf(BadRequestException.class)
......@@ -108,11 +104,10 @@ class StorageServiceImplTest {
private SignedObject getSignedObject() {
String containerName = RandomStringUtils.randomAlphanumeric(4);
String folderName = TestUtils.USER_DES_ID + "/" + RandomStringUtils.randomAlphanumeric(9);
String filename = TestUtils.getUuidString();
URI uri = TestUtils.getAzureObjectUri(containerName, folderName, filename);
URL url = TestUtils.getAzureObjectUrl(containerName, folderName, filename);
URI uri = TestUtils.getAzureObjectUri(containerName, filename);
URL url = TestUtils.getAzureObjectUrl(containerName, filename);
return SignedObject.builder()
.uri(uri)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment