|
| 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 | +} |
0 commit comments