Skip to content

Instantly share code, notes, and snippets.

@tiqwab
Created November 9, 2018 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiqwab/98b66f7a31673063422c59fe2bfc5358 to your computer and use it in GitHub Desktop.
Save tiqwab/98b66f7a31673063422c59fe2bfc5358 to your computer and use it in GitHub Desktop.
version: "3"
services:
minio:
image: minio/minio
environment:
- MINIO_ACCESS_KEY=TESTKEY
- MINIO_SECRET_KEY=TESTSECRET
ports:
- "9001:9000"
command: ["server", "/data"]
package sample
import java.io.{BufferedReader, InputStreamReader}
import com.amazonaws.auth.{AWSStaticCredentialsProvider, BasicAWSCredentials}
import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration
import com.amazonaws.services.s3.{AmazonS3, AmazonS3ClientBuilder}
import sample.FilePath
import org.scalatest._
import scala.collection.JavaConverters._
trait UseMinio extends BeforeAndAfterAll { self: Suite =>
private val accessKey = "TESTKEY"
private val secret = "TESTSECRET"
private val endpointUrl = "http://localhost:9001"
private val staticProvider = new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secret))
lazy val s3Client: AmazonS3 = AmazonS3ClientBuilder.standard
.withCredentials(staticProvider)
.withEndpointConfiguration(new EndpointConfiguration(endpointUrl, "ap-northeast-1"))
.withPathStyleAccessEnabled(true)
.build()
def bucketName: String = "test-bucket"
override def beforeAll(): Unit = {
super.beforeAll()
if (s3Client.doesBucketExistV2(bucketName)) {
emptyS3Bucket(bucketName)
} else {
createS3Bucket(bucketName)
}
}
private def emptyS3Bucket(bucketName: String): Unit = {
val list = s3Client.listObjects(bucketName)
list.getObjectSummaries.asScala foreach { obj =>
s3Client.deleteObject(bucketName, obj.getKey)
}
}
private def createS3Bucket(bucketName: String): Unit =
s3Client.createBucket(bucketName)
def putS3(s3File: FilePath.S3, content: String): Unit =
s3Client.putObject(s3File.bucketName, s3File.key, content)
def getLines(s3File: FilePath.S3): Seq[String] = {
val (bucketName, key) = (s3File.bucketName, s3File.key)
val br = new BufferedReader(new InputStreamReader(s3Client.getObject(bucketName, key).getObjectContent))
var lines = Seq.empty[String]
try {
var line = br.readLine()
while (line != null) {
lines = lines :+ line
line = br.readLine()
}
} finally {
br.close()
}
lines
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment