Merge SNARK-MR-24: Storage fabric method

This commit is contained in:
Kirill Grachev 2023-05-18 10:51:53 +00:00 committed by Space Cloud
commit f8bcdcaeeb
No known key found for this signature in database
GPG Key ID: 2F4D45726235F749
4 changed files with 73 additions and 1 deletions

3
.gitignore vendored
View File

@ -6,4 +6,5 @@ build/
!gradle/wrapper/gradle-wrapper.jar !gradle/wrapper/gradle-wrapper.jar
kotlin-js-store kotlin-js-store
*.iml *.iml
*.json

View File

@ -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))
}
}

View File

@ -20,6 +20,12 @@ public interface Directory : AutoCloseable {
@Deprecated("Not a good idea") @Deprecated("Not a good idea")
public val path: Path public val path: Path
public companion object {
public fun fromConfig(config: Config): Directory {
return config.build()
}
}
} }

View File

@ -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()
}
}