Use with Fs2
Setup
You will need these dependencies:
- sbt
libraryDependencies ++= Seq( "uk.gov.nationalarchives" % "da-s3-client_2.13" % "0.1.123", "co.fs2" % "fs2-reactive-streams_2.13" % "3.7.0" )
- Maven
<dependencies> <dependency> <groupId>uk.gov.nationalarchives</groupId> <artifactId>da-s3-client_2.13</artifactId> <version>0.1.123</version> </dependency> <dependency> <groupId>co.fs2</groupId> <artifactId>fs2-reactive-streams_2.13</artifactId> <version>3.7.0</version> </dependency> </dependencies>
- Gradle
dependencies { implementation "uk.gov.nationalarchives:da-s3-client_2.13:0.1.123" implementation "co.fs2:fs2-reactive-streams_2.13:3.7.0" }
Examples
import cats.effect._
import fs2.Stream
import fs2.interop.reactivestreams._
import software.amazon.awssdk.transfer.s3.model._
import uk.gov.nationalarchives.DAS3Client
val fs2Client = DAS3Client[IO, Stream[IO, Byte]]()
def upload(s: Stream[IO, Byte], fileSize: Long): IO[CompletedUpload] = {
s.chunks.map(_.toByteBuffer).toUnicastPublisher.use { publisher =>
fs2Client.upload("bucket", "key", fileSize, publisher)
}
}
def download(bucket: String, key: String): IO[Stream[IO, Byte]] = {
fs2Client.download(bucket, key)
.map(_.toStreamBuffered[IO](1024 * 1024).map(_.get()))
}
def copy(sourceBucket: String, sourceKey: String, destinationBucket: String, destinationKey: String): IO[CompletedCopy] = {
fs2Client.copy(sourceBucket, sourceKey, destinationBucket, destinationKey)
}
def headObject(bucket: String, key: String): IO[HeadObjectResponse] = {
fs2Client.headObject(bucket, key)
}
def deleteObjects(bucket: String, keys: List[String]): IO[DeleteObjectsResponse] = {
fs2Client.deleteObjects(bucket, keys)
}
def listCommonPrefixes(bucket: String, keysPrefixedWith: String): IO[SdkPublisher[String]] = {
fs2Client.listCommonPrefixes(bucket, keysPrefixedWith)
}