Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rasters S3 write methods #3333

Merged
merged 2 commits into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added
- Add rasters S3 write methods [#3333](https://github.com/locationtech/geotrellis/pull/3333)
- Add initial GDAL Transform rotation support [#3331](https://github.com/locationtech/geotrellis/pull/3331)

### Changed
Expand Down
53 changes: 53 additions & 0 deletions s3/src/main/scala/geotrellis/store/s3/S3RasterMethods.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2021 Azavea
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package geotrellis.store.s3

import geotrellis.raster.CellGrid
import geotrellis.raster.io.geotiff.GeoTiff
import geotrellis.raster.render.{Jpg, Png}
import geotrellis.util.MethodExtensions
import software.amazon.awssdk.core.sync.RequestBody
import software.amazon.awssdk.services.s3.S3Client
import software.amazon.awssdk.services.s3.model.PutObjectRequest

trait S3RasterMethods[T] extends MethodExtensions[T] {
def write(uri: AmazonS3URI, client: S3Client = S3ClientProducer.get()): Unit

protected def writeArray(uri: AmazonS3URI, client: S3Client, bytes: Array[Byte]): Unit = {
val objectRequest = PutObjectRequest.builder
.bucket(uri.getBucket)
.key(uri.getKey)
.build

client.putObject(objectRequest, RequestBody.fromBytes(bytes))
}
}

abstract class JpgS3WriteMethods(self: Jpg) extends S3RasterMethods[Jpg] {
def write(uri: AmazonS3URI, client: S3Client = S3ClientProducer.get()): Unit =
writeArray(uri, client, self.bytes)
}

abstract class PngS3WriteMethods(self: Png) extends S3RasterMethods[Png] {
def write(uri: AmazonS3URI, client: S3Client = S3ClientProducer.get()): Unit =
writeArray(uri, client, self.bytes)
}

abstract class GeoTiffS3WriteMethods[T <: CellGrid[Int]](self: GeoTiff[T]) extends S3RasterMethods[GeoTiff[T]] {
def write(uri: AmazonS3URI, client: S3Client = S3ClientProducer.get()): Unit =
writeArray(uri, client, self.toCloudOptimizedByteArray)
}
13 changes: 11 additions & 2 deletions s3/src/main/scala/geotrellis/store/s3/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@

package geotrellis.store

import geotrellis.raster.CellGrid
import geotrellis.raster.io.geotiff.GeoTiff
import geotrellis.raster.render.{Jpg, Png}
import software.amazon.awssdk.services.s3.S3Client
import software.amazon.awssdk.services.s3.model.{NoSuchKeyException, HeadObjectRequest}

import scala.util.{Try, Success, Failure}

package object s3 {
private[geotrellis] def makePath(chunks: String*) =
private[geotrellis] def makePath(chunks: String*): String =
chunks
.collect { case str if str.nonEmpty => if(str.endsWith("/")) str.dropRight(1) else str }
.mkString("/")

implicit class S3ClientExtension(client: S3Client) {
implicit class S3ClientExtension(val client: S3Client) extends AnyVal {
def objectExists(bucket: String, key: String): Boolean = {
val request = HeadObjectRequest.builder()
.bucket(bucket)
Expand All @@ -50,4 +53,10 @@ package object s3 {
client.objectExists(bucket, key)
}
}

implicit class withJpgS3WriteMethods(val self: Jpg) extends JpgS3WriteMethods(self)

implicit class withPngS3WriteMethods(val self: Png) extends PngS3WriteMethods(self)

implicit class withGeoTiffS3WriteMethods[T <: CellGrid[Int]](val self: GeoTiff[T]) extends GeoTiffS3WriteMethods[T](self)
}