Skip to content

Commit

Permalink
Docs (#14)
Browse files Browse the repository at this point in the history
* update doc

* fix test layer composition
  • Loading branch information
regis-leray authored May 19, 2020
1 parent ffad893 commit c8bcdb2
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
70 changes: 68 additions & 2 deletions docs/quickstart/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,92 @@ Setup
libraryDependencies += "dev.zio" %% "zio-s3" % "<version>"
```


How to use it ?
---------------

ZIO-S3 is a thin wrapper over the s3 async java client. It exposes the main operations of the s3 java client.


```scala
import zio.{Chunk, ZManaged}
import zio.s3._
import zio.stream.{ZSink, ZStream}

//list all files

// list all buckets available
listBuckets.provideLayer(
live("us-east-1", S3Credentials("accessKeyId", "secretAccessKey"))
)

// list all objects of all buckets
val l2: ZStream[S3, S3Exception, String] = (for {
bucket <- ZStream.fromIterableM(listBuckets)
obj <- listObjectsDescendant(bucket.name, "")
} yield obj.bucketName + "/" + obj.key).provideLayer(
live("us-east-1", S3Credentials("accessKeyId", "secretAccessKey"))
)
```

All available s3 combinators and operations are available in the package object `zio.s3`, you only need to `import zio.s3._`


Test / Stub
-----------

a stub implementation of s3 storage is provided for testing purpose and use internally a filesystem to simulate s3 storage

```scala
import zio.nio.core.file.{Path => ZPath}
import zio.s3._
import zio.blocking.Blocking

// required to provide a Blocking context
val testS3: ZLayer[Blocking, Any, S3] = test(ZPath("/tmp/s3-data"))

// use a Blocking context to build s3 Layer
val s3: ZLayer[Any, Any, S3] = Blocking.live >>> test(ZPath("/tmp/s3-data"))

// list all buckets available by using S3 Stub Layer
// will list all directories of `/tmp/s3-data`
listBuckets.provideLayer(s3)
```

More informations here how to use [ZLayer https://zio.dev/docs/howto/howto_use_layers](https://zio.dev/docs/howto/howto_use_layers)


Examples
--------

```scala
import zio.stream.{ZSink, ZStream}
import zio.s3._
import zio._

// upload
val json: Chunk[String] = Chunk.fromArray("""{ "id" : 1 , "name" : "A1" }""".getBytes)
val up: ZIO[S3, S3Exception, Unit] = putObject("bucket-1", "user.json", json.length, "application/json", ZStream.fromChunk(json))

// multipartUpload only for file than bigger 5Mb
import java.io.FileInputStream
import java.nio.file.Paths

val is = ZStream.fromInputStreamEffect(ZIO(new FileInputStream(Paths.get("/my/path/to/myfile").toFile)))
val proc2: ZIO[S3 with Blocking, S3Exception, Unit] = multipartUpload("bucket-1", "upload/myfile","application/zip", is)

// download
import java.io.OutputStream

val os: OutputStream = ???
val proc3: ZIO[Blocking with S3, Exception, Int] = getObject("bucket-1", "upload/myfile.zip").run(ZSink.fromOutputStream(os))
```

Support any commands ?
---

If you need a method which is not wrapped by the library, you can have access to underlying S3 client in a safe manner by using

```scala
import zio.s3._

def execute[T](f: S3AsyncClient => CompletableFuture[T])
```
6 changes: 3 additions & 3 deletions src/test/scala/zio/s3/S3Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import java.nio.file.attribute.PosixFileAttributes
import java.util.UUID

import software.amazon.awssdk.regions.Region
import zio.Chunk
import zio.{ Chunk, ZLayer }
import zio.blocking.Blocking
import zio.nio.core.file.{ Path => ZPath }
import zio.nio.file.{ Files => ZFiles }
Expand All @@ -28,8 +28,8 @@ object S3LiveSpec extends DefaultRunnableSpec {
object S3TestSpec extends DefaultRunnableSpec {
private val root = ZPath("test-data")

private val s3 = zio.s3.test(root).mapError(TestFailure.fail(_))
override def spec = S3Suite.spec("S3TestSpec", root).provideCustomLayerShared(s3 ++ Blocking.live)
private val s3: ZLayer[Blocking, TestFailure[Any], S3] = zio.s3.test(root).mapError(TestFailure.fail(_))
override def spec = S3Suite.spec("S3TestSpec", root).provideCustomLayerShared(Blocking.live >>> s3)
}

object S3Suite {
Expand Down

0 comments on commit c8bcdb2

Please sign in to comment.