diff options
author | zachdriver <zach@driver.xyz> | 2017-05-18 16:12:32 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-18 16:12:32 -0700 |
commit | 6427b9dc1a60b670c70aca05f419e3fc3313cf6a (patch) | |
tree | 92ea3d3ab0b4b2f01531916e1ab7ae3fc5d1b24e /src/main/scala/xyz/driver/core/file/GcsStorage.scala | |
parent | 3d2e8eb57e074e51da53d8c239c63b4ce7e5a820 (diff) | |
parent | 06cecf3761c77efbbc35b3d36178b34dd7ea7e64 (diff) | |
download | driver-core-6427b9dc1a60b670c70aca05f419e3fc3313cf6a.tar.gz driver-core-6427b9dc1a60b670c70aca05f419e3fc3313cf6a.tar.bz2 driver-core-6427b9dc1a60b670c70aca05f419e3fc3313cf6a.zip |
Merge pull request #34 from drivergroup/zsmith/PDW-336-gcsv0.12.7
PDW-336 Add GCS file storage implementation
Diffstat (limited to 'src/main/scala/xyz/driver/core/file/GcsStorage.scala')
-rw-r--r-- | src/main/scala/xyz/driver/core/file/GcsStorage.scala | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/main/scala/xyz/driver/core/file/GcsStorage.scala b/src/main/scala/xyz/driver/core/file/GcsStorage.scala new file mode 100644 index 0000000..6c2746e --- /dev/null +++ b/src/main/scala/xyz/driver/core/file/GcsStorage.scala @@ -0,0 +1,82 @@ +package xyz.driver.core.file + +import java.io.{BufferedOutputStream, File, FileInputStream, FileOutputStream} +import java.net.URL +import java.nio.file.{Path, Paths} +import java.util.concurrent.TimeUnit + +import com.google.cloud.storage.Storage.BlobListOption +import com.google.cloud.storage._ +import xyz.driver.core.time.Time +import xyz.driver.core.{Name, Revision, generators} + +import scala.collection.JavaConverters._ +import scala.concurrent.duration.Duration +import scala.concurrent.{ExecutionContext, Future} +import scalaz.{ListT, OptionT} + +class GcsStorage(storageClient: Storage, bucketName: Name[Bucket], executionContext: ExecutionContext) + extends SignedFileStorage { + implicit private val execution: ExecutionContext = executionContext + + override def upload(localSource: File, destination: Path): Future[Unit] = Future { + checkSafeFileName(destination) { + val blobId = BlobId.of(bucketName.value, destination.toString) + def acl = Bucket.BlobWriteOption.predefinedAcl(Storage.PredefinedAcl.PUBLIC_READ) + + storageClient.get(bucketName.value).create(blobId.getName, new FileInputStream(localSource), acl) + } + } + + override def download(filePath: Path): OptionT[Future, File] = { + OptionT.optionT(Future { + Option(storageClient.get(bucketName.value, filePath.toString)).filterNot(_.getSize == 0).map { + blob => + val tempDir = System.getProperty("java.io.tmpdir") + val randomFolderName = generators.nextUuid().toString + val tempDestinationFile = new File(Paths.get(tempDir, randomFolderName, filePath.toString).toString) + + if (!tempDestinationFile.getParentFile.mkdirs()) { + throw new Exception(s"Failed to create temp directory to download file `$tempDestinationFile`") + } else { + val target = new BufferedOutputStream(new FileOutputStream(tempDestinationFile)) + try target.write(blob.getContent()) + finally target.close() + tempDestinationFile + } + } + }) + } + + override def delete(filePath: Path): Future[Unit] = Future { + storageClient.delete(BlobId.of(bucketName.value, filePath.toString)) + } + + override def list(path: Path): ListT[Future, FileLink] = + ListT.listT(Future { + val page = storageClient.list( + bucketName.value, + BlobListOption.currentDirectory(), + BlobListOption.prefix(path.toString) + ) + + page.iterateAll().asScala.map(blobToFileLink(path, _)).toList + }) + + protected def blobToFileLink(path: Path, blob: Blob): FileLink = { + FileLink( + Name(blob.getName), + Paths.get(path.toString, blob.getName), + Revision(blob.getGeneration.toString), + Time(blob.getUpdateTime), + blob.getSize + ) + } + + override def signedFileUrl(filePath: Path, duration: Duration): OptionT[Future, URL] = + OptionT.optionT(Future { + Option(storageClient.get(bucketName.value, filePath.toString)).filterNot(_.getSize == 0).map { blob => + blob.signUrl(duration.toSeconds, TimeUnit.SECONDS) + } + }) +} |