Skip to content

Commit 2f8348a

Browse files
pomadchinecheipesh
andauthored
Add rasters S3 write methods (#3333)
Co-authored-by: Eugene Cheipesh <echeipesh@gmail.com>
1 parent 16c7475 commit 2f8348a

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased]
88

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

1213
### Changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2021 Azavea
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 geotrellis.store.s3
18+
19+
import geotrellis.raster.CellGrid
20+
import geotrellis.raster.io.geotiff.GeoTiff
21+
import geotrellis.raster.render.{Jpg, Png}
22+
import geotrellis.util.MethodExtensions
23+
import software.amazon.awssdk.core.sync.RequestBody
24+
import software.amazon.awssdk.services.s3.S3Client
25+
import software.amazon.awssdk.services.s3.model.PutObjectRequest
26+
27+
trait S3RasterMethods[T] extends MethodExtensions[T] {
28+
def write(uri: AmazonS3URI, client: S3Client = S3ClientProducer.get()): Unit
29+
30+
protected def writeArray(uri: AmazonS3URI, client: S3Client, bytes: Array[Byte]): Unit = {
31+
val objectRequest = PutObjectRequest.builder
32+
.bucket(uri.getBucket)
33+
.key(uri.getKey)
34+
.build
35+
36+
client.putObject(objectRequest, RequestBody.fromBytes(bytes))
37+
}
38+
}
39+
40+
abstract class JpgS3WriteMethods(self: Jpg) extends S3RasterMethods[Jpg] {
41+
def write(uri: AmazonS3URI, client: S3Client = S3ClientProducer.get()): Unit =
42+
writeArray(uri, client, self.bytes)
43+
}
44+
45+
abstract class PngS3WriteMethods(self: Png) extends S3RasterMethods[Png] {
46+
def write(uri: AmazonS3URI, client: S3Client = S3ClientProducer.get()): Unit =
47+
writeArray(uri, client, self.bytes)
48+
}
49+
50+
abstract class GeoTiffS3WriteMethods[T <: CellGrid[Int]](self: GeoTiff[T]) extends S3RasterMethods[GeoTiff[T]] {
51+
def write(uri: AmazonS3URI, client: S3Client = S3ClientProducer.get()): Unit =
52+
writeArray(uri, client, self.toCloudOptimizedByteArray)
53+
}

s3/src/main/scala/geotrellis/store/s3/package.scala

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,21 @@
1616

1717
package geotrellis.store
1818

19+
import geotrellis.raster.CellGrid
20+
import geotrellis.raster.io.geotiff.GeoTiff
21+
import geotrellis.raster.render.{Jpg, Png}
1922
import software.amazon.awssdk.services.s3.S3Client
2023
import software.amazon.awssdk.services.s3.model.{NoSuchKeyException, HeadObjectRequest}
2124

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

2427
package object s3 {
25-
private[geotrellis] def makePath(chunks: String*) =
28+
private[geotrellis] def makePath(chunks: String*): String =
2629
chunks
2730
.collect { case str if str.nonEmpty => if(str.endsWith("/")) str.dropRight(1) else str }
2831
.mkString("/")
2932

30-
implicit class S3ClientExtension(client: S3Client) {
33+
implicit class S3ClientExtension(val client: S3Client) extends AnyVal {
3134
def objectExists(bucket: String, key: String): Boolean = {
3235
val request = HeadObjectRequest.builder()
3336
.bucket(bucket)
@@ -50,4 +53,10 @@ package object s3 {
5053
client.objectExists(bucket, key)
5154
}
5255
}
56+
57+
implicit class withJpgS3WriteMethods(val self: Jpg) extends JpgS3WriteMethods(self)
58+
59+
implicit class withPngS3WriteMethods(val self: Png) extends PngS3WriteMethods(self)
60+
61+
implicit class withGeoTiffS3WriteMethods[T <: CellGrid[Int]](val self: GeoTiff[T]) extends GeoTiffS3WriteMethods[T](self)
5362
}

0 commit comments

Comments
 (0)