diff --git a/.gitignore b/.gitignore index f06baf2..21511df 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ build/ !gradle/wrapper/gradle-wrapper.jar kotlin-js-store -*.iml \ No newline at end of file +*.iml +*.json diff --git a/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/Config.kt b/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/Config.kt new file mode 100644 index 0000000..452b12a --- /dev/null +++ b/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/Config.kt @@ -0,0 +1,40 @@ +package space.kscience.snark.storage + +import aws.sdk.kotlin.services.s3.S3Client +import space.kscience.snark.storage.local.localStorage +import space.kscience.snark.storage.s3.s3Bucket +import space.kscience.snark.storage.s3.s3Storage +import java.nio.file.Path + +private const val DEFAULT_REGION = "arctic-vault" + +public sealed interface Config { + public fun build(): Directory +} + +public data class LocalConfig(val path: Path) : Config { + override fun build(): Directory { + return localStorage(path) + } +} + +/* + * ~/.aws/credentials.json file is required + */ +internal fun buildS3Client(regionSpec: String): S3Client { + return S3Client { + region = regionSpec + } +} + +public data class S3BucketConfig(val bucketName: String, val region: String = DEFAULT_REGION) : Config { + override fun build(): Directory { + return s3Bucket(buildS3Client(region), bucketName) + } +} + +public data class S3ServiceConfig(val region: String = DEFAULT_REGION) : Config { + override fun build(): Directory { + return s3Storage(buildS3Client(region)) + } +} diff --git a/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/Driver.kt b/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/Driver.kt index 902aa23..d0e2b6a 100644 --- a/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/Driver.kt +++ b/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/Driver.kt @@ -20,6 +20,12 @@ public interface Directory : AutoCloseable { @Deprecated("Not a good idea") public val path: Path + + public companion object { + public fun fromConfig(config: Config): Directory { + return config.build() + } + } } diff --git a/snark-storage-driver/src/test/kotlin/space/kscience/snark/storage/JustCreates.kt b/snark-storage-driver/src/test/kotlin/space/kscience/snark/storage/JustCreates.kt new file mode 100644 index 0000000..43e8144 --- /dev/null +++ b/snark-storage-driver/src/test/kotlin/space/kscience/snark/storage/JustCreates.kt @@ -0,0 +1,25 @@ +package space.kscience.snark.storage + +import org.junit.jupiter.api.Test +import kotlin.io.path.createTempDirectory + +class JustCreates { + + @Test + fun s3Created() { + val dir = Directory.fromConfig(S3ServiceConfig()) + dir.close() + } + + @Test + fun s3BucketCreated() { + val dir = Directory.fromConfig(S3BucketConfig("snark-test")) + dir.close() + } + + @Test + fun localCreated() { + val dir = Directory.fromConfig(LocalConfig(createTempDirectory("snark-test"))) + dir.close() + } +} \ No newline at end of file