From ad3b1d6341a170d8b83b3c399b35abf26822dd8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20=C5=A0valjek?= <36041566+msvaljek@users.noreply.github.com> Date: Fri, 24 Jul 2020 03:04:42 +0200 Subject: [PATCH] Add support for defining metadata (#90) * Add support for defining metadata --- src/main/scala/zio/s3/Live.scala | 8 ++++++-- src/main/scala/zio/s3/Test.scala | 8 +++++--- src/main/scala/zio/s3/package.scala | 17 +++++++++++------ src/test/scala/zio/s3/S3Test.scala | 4 ++-- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/main/scala/zio/s3/Live.scala b/src/main/scala/zio/s3/Live.scala index 6c8f7220..1360fb96 100644 --- a/src/main/scala/zio/s3/Live.scala +++ b/src/main/scala/zio/s3/Live.scala @@ -95,7 +95,8 @@ final class Live(unsafeClient: S3AsyncClient) extends S3.Service { key: String, contentLength: Long, contentType: String, - content: ZStream[R, Throwable, Byte] + content: ZStream[R, Throwable, Byte], + metadata: Map[String, String] = Map.empty ): ZIO[R, S3Exception, Unit] = content .mapChunks(Chunk.single) @@ -110,6 +111,7 @@ final class Live(unsafeClient: S3AsyncClient) extends S3.Service { .contentLength(contentLength) .contentType(contentType) .key(key) + .metadata(metadata.asJava) .build(), AsyncRequestBody.fromPublisher(publisher) ) @@ -122,7 +124,8 @@ final class Live(unsafeClient: S3AsyncClient) extends S3.Service { bucketName: String, key: String, contentType: String, - content: ZStream[R, Throwable, Byte] + content: ZStream[R, Throwable, Byte], + metadata: Map[String, String] = Map.empty ): ZIO[R, S3Exception, Unit] = for { uploadId <- execute( @@ -132,6 +135,7 @@ final class Live(unsafeClient: S3AsyncClient) extends S3.Service { .bucket(bucketName) .key(key) .contentType(contentType) + .metadata(metadata.asJava) .build() ) ).map(_.uploadId()) diff --git a/src/main/scala/zio/s3/Test.scala b/src/main/scala/zio/s3/Test.scala index 8c3441cc..c9739382 100644 --- a/src/main/scala/zio/s3/Test.scala +++ b/src/main/scala/zio/s3/Test.scala @@ -102,7 +102,8 @@ object Test { key: String, contentLength: Long, contentType: String, - content: ZStream[R, Throwable, Byte] + content: ZStream[R, Throwable, Byte], + metadata: Map[String, String] ): ZIO[R, S3Exception, Unit] = ZManaged .fromAutoCloseable(Task(new FileOutputStream((path / bucketName / key).toFile))) @@ -121,9 +122,10 @@ object Test { bucketName: String, key: String, contentType: String, - content: ZStream[R, Throwable, Byte] + content: ZStream[R, Throwable, Byte], + metadata: Map[String, String] ): ZIO[R, S3Exception, Unit] = - putObject(bucketName, key, 0, contentType, content.chunkN(10)) + putObject(bucketName, key, 0, contentType, content.chunkN(10), metadata) } } } diff --git a/src/main/scala/zio/s3/package.scala b/src/main/scala/zio/s3/package.scala index 4ad3ff24..16251e3e 100644 --- a/src/main/scala/zio/s3/package.scala +++ b/src/main/scala/zio/s3/package.scala @@ -106,6 +106,7 @@ package object s3 { * @param contentLength length of the data in bytes * @param contentType content type of the object (json, csv, txt, binary, ...) * @param content object data + * @param metadata metadata * @return */ def putObject[R <: zio.Has[_]: Tag]( @@ -113,7 +114,8 @@ package object s3 { key: String, contentLength: Long, contentType: String, - content: ZStream[R, Throwable, Byte] + content: ZStream[R, Throwable, Byte], + metadata: Map[String, String] = Map.empty ): ZIO[R, S3Exception, Unit] /** @@ -130,7 +132,8 @@ package object s3 { bucketName: String, key: String, contentType: String, - content: ZStream[R, Throwable, Byte] + content: ZStream[R, Throwable, Byte], + metadata: Map[String, String] = Map.empty ): ZIO[R, S3Exception, Unit] /** @@ -235,17 +238,19 @@ package object s3 { key: String, contentLength: Long, contentType: String, - content: ZStream[R, Throwable, Byte] + content: ZStream[R, Throwable, Byte], + metadata: Map[String, String] ): ZIO[S3 with R, S3Exception, Unit] = - ZIO.accessM[S3 with R](_.get.putObject(bucketName, key, contentLength, contentType, content)) + ZIO.accessM[S3 with R](_.get.putObject(bucketName, key, contentLength, contentType, content, metadata)) def multipartUpload[R <: Has[_]: Tag]( bucketName: String, key: String, contentType: String, - content: ZStream[R, Throwable, Byte] + content: ZStream[R, Throwable, Byte], + metadata: Map[String, String] ): ZIO[S3 with R, S3Exception, Unit] = - ZIO.accessM[S3 with R](_.get.multipartUpload(bucketName, key, contentType, content)) + ZIO.accessM[S3 with R](_.get.multipartUpload(bucketName, key, contentType, content, metadata)) def execute[T](f: S3AsyncClient => CompletableFuture[T]): ZIO[S3, S3Exception, T] = ZIO.accessM(_.get.execute(f)) diff --git a/src/test/scala/zio/s3/S3Test.scala b/src/test/scala/zio/s3/S3Test.scala index e543c62e..96cb0991 100644 --- a/src/test/scala/zio/s3/S3Test.scala +++ b/src/test/scala/zio/s3/S3Test.scala @@ -169,7 +169,7 @@ object S3Suite { val tmpKey = Random.alphanumeric.take(10).mkString for { - _ <- putObject(bucketName, tmpKey, c.length.toLong, "text/plain", data) + _ <- putObject(bucketName, tmpKey, c.length.toLong, "text/plain", data, metadata = Map.empty) fileSize <- (ZFiles .readAttributes[PosixFileAttributes](root / bucketName / tmpKey) .map(_.size()) <* ZFiles.delete(root / bucketName / tmpKey)).provideLayer(Blocking.live) @@ -197,7 +197,7 @@ object S3Suite { val tmpKey = Random.alphanumeric.take(10).mkString for { - _ <- multipartUpload(bucketName, tmpKey, "application/octet-stream", data) + _ <- multipartUpload(bucketName, tmpKey, "application/octet-stream", data, metadata = Map.empty) fileSize <- (ZFiles .readAttributes[PosixFileAttributes](root / bucketName / tmpKey) .map(_.size()) <* ZFiles.delete(root / bucketName / tmpKey)).provideLayer(Blocking.live)