Skip to content

Commit

Permalink
Add support for defining metadata (#90)
Browse files Browse the repository at this point in the history
* Add support for defining metadata
  • Loading branch information
msvaljek authored Jul 24, 2020
1 parent 6dd6914 commit ad3b1d6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
8 changes: 6 additions & 2 deletions src/main/scala/zio/s3/Live.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
)
Expand All @@ -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(
Expand All @@ -132,6 +135,7 @@ final class Live(unsafeClient: S3AsyncClient) extends S3.Service {
.bucket(bucketName)
.key(key)
.contentType(contentType)
.metadata(metadata.asJava)
.build()
)
).map(_.uploadId())
Expand Down
8 changes: 5 additions & 3 deletions src/main/scala/zio/s3/Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand All @@ -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)
}
}
}
17 changes: 11 additions & 6 deletions src/main/scala/zio/s3/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,16 @@ 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](
bucketName: String,
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]

/**
Expand All @@ -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]

/**
Expand Down Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions src/test/scala/zio/s3/S3Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ad3b1d6

Please sign in to comment.