|
| 1 | +/* |
| 2 | + * Copyright 2018 The Hyve |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.radarbase.hdfs.storage |
| 18 | + |
| 19 | +import org.slf4j.LoggerFactory |
| 20 | +import software.amazon.awssdk.services.s3.S3Client |
| 21 | +import software.amazon.awssdk.services.s3.model.NoSuchKeyException |
| 22 | +import java.io.IOException |
| 23 | +import java.io.InputStream |
| 24 | +import java.net.URI |
| 25 | +import java.nio.file.Files |
| 26 | +import java.nio.file.Path |
| 27 | + |
| 28 | +class S3StorageDriver : StorageDriver { |
| 29 | + private lateinit var bucket: String |
| 30 | + private lateinit var awsClient: S3Client |
| 31 | + |
| 32 | + override fun init(properties: Map<String, String>) { |
| 33 | + awsClient = S3Client.builder().also { s3Builder -> |
| 34 | + properties["s3EndpointUrl"]?.let { |
| 35 | + val endpoint = try { |
| 36 | + URI.create(it) |
| 37 | + } catch (ex: IllegalArgumentException) { |
| 38 | + logger.warn("Invalid S3 URL", ex) |
| 39 | + throw ex |
| 40 | + } |
| 41 | + |
| 42 | + s3Builder.endpointOverride(endpoint) |
| 43 | + } |
| 44 | + }.build() |
| 45 | + |
| 46 | + bucket = requireNotNull(properties["s3Bucket"]) { "No AWS bucket provided" } |
| 47 | + } |
| 48 | + |
| 49 | + override fun status(path: Path): StorageDriver.PathStatus? { |
| 50 | + return try { |
| 51 | + awsClient.headObject { it.bucket(bucket).key(path.toString()) } |
| 52 | + .let { StorageDriver.PathStatus(it.contentLength()) } |
| 53 | + } catch (ex: NoSuchKeyException) { |
| 54 | + null |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Throws(IOException::class) |
| 59 | + override fun newInputStream(path: Path): InputStream = awsClient.getObject { |
| 60 | + it.bucket(bucket).key(path.toString()) |
| 61 | + } |
| 62 | + |
| 63 | + @Throws(IOException::class) |
| 64 | + override fun move(oldPath: Path, newPath: Path) { |
| 65 | + awsClient.copyObject { |
| 66 | + it.bucket(bucket).key(newPath.toString()) |
| 67 | + .copySource("$bucket/$oldPath") |
| 68 | + } |
| 69 | + delete(oldPath) |
| 70 | + } |
| 71 | + |
| 72 | + @Throws(IOException::class) |
| 73 | + override fun store(localPath: Path, newPath: Path) { |
| 74 | + awsClient.putObject({ it.bucket(bucket).key(newPath.toString()) }, localPath) |
| 75 | + Files.delete(localPath) |
| 76 | + } |
| 77 | + |
| 78 | + @Throws(IOException::class) |
| 79 | + override fun delete(path: Path) { |
| 80 | + awsClient.deleteObject { it.bucket(bucket).key(path.toString()) } |
| 81 | + } |
| 82 | + |
| 83 | + companion object { |
| 84 | + private val logger = LoggerFactory.getLogger(S3StorageDriver::class.java) |
| 85 | + } |
| 86 | +} |
0 commit comments