mirror of
https://github.com/pischule/memevizor.git
synced 2025-12-19 06:56:42 +00:00
Migrate from minio to aws s3 sdk
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
package com.pischule.memevizor.upload
|
||||
|
||||
import aws.sdk.kotlin.services.s3.S3Client
|
||||
import aws.sdk.kotlin.services.s3.model.PutObjectRequest
|
||||
import aws.smithy.kotlin.runtime.content.ByteStream
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import io.github.oshai.kotlinlogging.withLoggingContext
|
||||
import io.minio.MinioClient
|
||||
import io.minio.PutObjectArgs
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean
|
||||
import org.springframework.stereotype.Service
|
||||
|
||||
@@ -11,17 +13,18 @@ private val logger = KotlinLogging.logger {}
|
||||
|
||||
@ConditionalOnBean(S3Config::class)
|
||||
@Service
|
||||
class FileUploaderService(private val s3Client: MinioClient, private val s3Props: S3Props) {
|
||||
class FileUploaderService(private val s3Client: S3Client, private val s3Props: S3Props) {
|
||||
fun uploadFile(fileBytes: ByteArray, filename: String, contentType: String) {
|
||||
withLoggingContext("filename" to filename, "bucket" to s3Props.bucket) {
|
||||
s3Client.putObject(
|
||||
PutObjectArgs.builder()
|
||||
.bucket(s3Props.bucket)
|
||||
.`object`(filename)
|
||||
.stream(fileBytes.inputStream(), fileBytes.size.toLong(), -1)
|
||||
.contentType(contentType)
|
||||
.build()
|
||||
)
|
||||
val request = PutObjectRequest {
|
||||
bucket = s3Props.bucket
|
||||
key = filename
|
||||
body = ByteStream.fromBytes(fileBytes)
|
||||
this.contentType = contentType
|
||||
}
|
||||
|
||||
logger.info { "Started uploading a file" }
|
||||
runBlocking { s3Client.putObject(request) }
|
||||
logger.info { "Uploaded a file to S3" }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.pischule.memevizor.upload
|
||||
|
||||
import io.minio.MinioClient
|
||||
import aws.sdk.kotlin.runtime.auth.credentials.StaticCredentialsProvider
|
||||
import aws.sdk.kotlin.services.s3.S3Client
|
||||
import aws.smithy.kotlin.runtime.net.url.Url
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.context.annotation.Configuration
|
||||
@@ -8,11 +10,17 @@ import org.springframework.context.annotation.Configuration
|
||||
@EnableConfigurationProperties(S3Props::class)
|
||||
@Configuration
|
||||
class S3Config {
|
||||
|
||||
@Bean
|
||||
fun s3Client(s3Props: S3Props): MinioClient =
|
||||
MinioClient.builder()
|
||||
.endpoint(s3Props.endpoint)
|
||||
.region(s3Props.region)
|
||||
.credentials(s3Props.accessKeyId, s3Props.secretAccessKey)
|
||||
.build()
|
||||
fun s3Client(s3Props: S3Props) = S3Client {
|
||||
endpointUrl = Url.parse(s3Props.endpoint)
|
||||
region = s3Props.region
|
||||
credentialsProvider = StaticCredentialsProvider {
|
||||
accessKeyId = s3Props.accessKeyId
|
||||
secretAccessKey = s3Props.secretAccessKey
|
||||
}
|
||||
|
||||
// not supported by Yandex Object Storage
|
||||
continueHeaderThresholdBytes = null
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user