Migrate from minio to aws s3 sdk

This commit is contained in:
2025-12-07 17:55:06 +03:00
parent 23dcd998bc
commit 1266b0f440
4 changed files with 36 additions and 19 deletions

View File

@@ -31,7 +31,7 @@ dependencies {
implementation("org.jetbrains.kotlin:kotlin-reflect") implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("io.github.kotlin-telegram-bot.kotlin-telegram-bot:telegram:6.3.0") implementation("io.github.kotlin-telegram-bot.kotlin-telegram-bot:telegram:6.3.0")
implementation("io.github.oshai:kotlin-logging-jvm:7.0.3") implementation("io.github.oshai:kotlin-logging-jvm:7.0.3")
implementation("io.minio:minio:8.6.0") implementation(awssdk.services.s3)
developmentOnly("org.springframework.boot:spring-boot-devtools") developmentOnly("org.springframework.boot:spring-boot-devtools")
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor") annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
testImplementation("org.springframework.boot:spring-boot-starter-test") testImplementation("org.springframework.boot:spring-boot-starter-test")

View File

@@ -4,4 +4,10 @@ dependencyResolutionManagement {
repositories { repositories {
mavenCentral() mavenCentral()
} }
versionCatalogs {
create("awssdk") {
from("aws.sdk.kotlin:version-catalog:1.5.97")
}
}
} }

View File

@@ -1,9 +1,11 @@
package com.pischule.memevizor.upload 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.KotlinLogging
import io.github.oshai.kotlinlogging.withLoggingContext import io.github.oshai.kotlinlogging.withLoggingContext
import io.minio.MinioClient import kotlinx.coroutines.runBlocking
import io.minio.PutObjectArgs
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean import org.springframework.boot.autoconfigure.condition.ConditionalOnBean
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
@@ -11,17 +13,18 @@ private val logger = KotlinLogging.logger {}
@ConditionalOnBean(S3Config::class) @ConditionalOnBean(S3Config::class)
@Service @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) { fun uploadFile(fileBytes: ByteArray, filename: String, contentType: String) {
withLoggingContext("filename" to filename, "bucket" to s3Props.bucket) { withLoggingContext("filename" to filename, "bucket" to s3Props.bucket) {
s3Client.putObject( val request = PutObjectRequest {
PutObjectArgs.builder() bucket = s3Props.bucket
.bucket(s3Props.bucket) key = filename
.`object`(filename) body = ByteStream.fromBytes(fileBytes)
.stream(fileBytes.inputStream(), fileBytes.size.toLong(), -1) this.contentType = contentType
.contentType(contentType) }
.build()
) logger.info { "Started uploading a file" }
runBlocking { s3Client.putObject(request) }
logger.info { "Uploaded a file to S3" } logger.info { "Uploaded a file to S3" }
} }
} }

View File

@@ -1,6 +1,8 @@
package com.pischule.memevizor.upload 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.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Configuration
@@ -8,11 +10,17 @@ import org.springframework.context.annotation.Configuration
@EnableConfigurationProperties(S3Props::class) @EnableConfigurationProperties(S3Props::class)
@Configuration @Configuration
class S3Config { class S3Config {
@Bean @Bean
fun s3Client(s3Props: S3Props): MinioClient = fun s3Client(s3Props: S3Props) = S3Client {
MinioClient.builder() endpointUrl = Url.parse(s3Props.endpoint)
.endpoint(s3Props.endpoint) region = s3Props.region
.region(s3Props.region) credentialsProvider = StaticCredentialsProvider {
.credentials(s3Props.accessKeyId, s3Props.secretAccessKey) accessKeyId = s3Props.accessKeyId
.build() secretAccessKey = s3Props.secretAccessKey
}
// not supported by Yandex Object Storage
continueHeaderThresholdBytes = null
}
} }