Skip to content

Unable to upload file to MinIO #6123

Closed
Closed
@sheng930920

Description

@sheng930920

Describe the bug

I use AWS SDK v2 to upload files, but I can't upload files to MinIO. Here is my test code

implementation("software.amazon.awssdk:s3:2.31.45")
implementation("software.amazon.awssdk:sts:2.31.45")
implementation("software.amazon.awssdk:url-connection-client:2.31.45")
import jakarta.annotation.PostConstruct
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials
import software.amazon.awssdk.auth.credentials.AwsCredentials
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider
import software.amazon.awssdk.core.async.AsyncRequestBody
import software.amazon.awssdk.core.sync.RequestBody
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient
import software.amazon.awssdk.regions.Region.*
import software.amazon.awssdk.services.s3.S3AsyncClient
import software.amazon.awssdk.services.s3.S3Client
import software.amazon.awssdk.services.s3.S3Configuration
import software.amazon.awssdk.services.s3.model.PutObjectRequest
import java.io.File
import java.net.URI
import java.nio.file.Paths
import java.util.concurrent.CompletableFuture


@Component
class UploadTest {

    private val logger = LoggerFactory.getLogger(UploadTest::class.java)


    @PostConstruct
    fun uploadToMinio() {
        println("开始上传!")

        val endpoint = "https://oss.ucanfly.com.cn:8081"
        val accessKey = "ucanfly2022"
        val secretKey = "ucanfly82321258"
        val bucketName = "ucanfly"
        val objectKey = "WechatIMG8038.png"
        val filePath = "/Users/liqisheng/Downloads/WechatIMG8038.png"

        val credentials = AwsBasicCredentials.create(accessKey, secretKey)

        val serviceConfiguration = S3Configuration.builder()
            //.pathStyleAccessEnabled(true) // ✅ 必须开启 Path-Style
            .build()

        val s3Client = S3Client.builder()
            .endpointOverride(URI.create(endpoint)) // ✅ 自定义 MinIO endpoint
            .region(US_EAST_1) // ✅ Region 必填,但可任意值
            .forcePathStyle(true)
            .credentialsProvider(StaticCredentialsProvider.create(credentials))
            .serviceConfiguration(serviceConfiguration)
            .httpClient(UrlConnectionHttpClient.create())
            .build()

        // 上传对象
        s3Client.putObject(
            PutObjectRequest.builder()
                .bucket(bucketName)
                .key("test/$objectKey")
                .build(),
            RequestBody.fromFile(Paths.get(filePath))
        )

        println("✅ 上传成功!")
    }




   @PostConstruct
    fun uploadToMinioAsync() {
        val accessKey = "ucanfly2022"
        val secretKey = "ucanfly82321258"
        val endpoint = "https://oss.ucanfly.com.cn:8081"
        val bucket = "ucanfly"
        val objectKey = "WechatIMG8038.png"
        val filePath = "/Users/liqisheng/Downloads/WechatIMG8038.png"

        val credentials = StaticCredentialsProvider.create(
            AwsBasicCredentials.create(accessKey, secretKey)
        )

        val s3AsyncClient = S3AsyncClient.builder()
            .endpointOverride(URI.create(endpoint))
            .credentialsProvider(credentials)
            .region(US_EAST_1)
            .forcePathStyle(true)
            .build()

        val putObjectRequest = PutObjectRequest.builder()
            .bucket(bucket)
            .key(objectKey)
            .build()

        val putObjectFuture: CompletableFuture<Void> = s3AsyncClient.putObject(
            putObjectRequest,
            AsyncRequestBody.fromFile(File(filePath))
        ).thenAccept {
            println("✅ 文件上传成功: $objectKey")
        }.exceptionally { ex ->
            println("❌ 上传失败: ${ex.message}")
            ex.printStackTrace()
            null
        }

        // 等待异步上传完成
        putObjectFuture.join()
    }


}

The following is the error log information

上传失败: software.amazon.awssdk.services.s3.model.S3Exception: (Service: S3, Status Code: 400, Request ID: null) (SDK Attempt Count: 1)
java.util.concurrent.CompletionException: software.amazon.awssdk.services.s3.model.S3Exception: (Service: S3, Status Code: 400, Request ID: null) (SDK Attempt Count: 1)
	at software.amazon.awssdk.utils.CompletableFutureUtils.errorAsCompletionException(CompletableFutureUtils.java:64)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.AsyncExecutionFailureExceptionReportingStage.lambda$execute$0(AsyncExecutionFailureExceptionReportingStage.java:51)
	at java.base/java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:934)
	at java.base/java.util.concurrent.CompletableFuture$UniHandle.tryFire(CompletableFuture.java:911)
	at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510)
	at java.base/java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:2162)
	at software.amazon.awssdk.utils.CompletableFutureUtils.lambda$forwardExceptionTo$0(CompletableFutureUtils.java:78)
	at java.base/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863)
	at java.base/java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:841)
	at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510)
	at java.base/java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:2162)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.AsyncRetryableStage$RetryingExecutor.maybeAttemptExecute(AsyncRetryableStage.java:135)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.AsyncRetryableStage$RetryingExecutor.maybeRetryExecute(AsyncRetryableStage.java:152)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.AsyncRetryableStage$RetryingExecutor.lambda$attemptExecute$1(AsyncRetryableStage.java:123)
	at java.base/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863)
	at java.base/java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:841)
	at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510)
	at java.base/java.util.concurrent.CompletableFuture.complete(CompletableFuture.java:2147)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.MakeAsyncHttpRequestStage.lambda$execute$0(MakeAsyncHttpRequestStage.java:110)
	at java.base/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863)
	at java.base/java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:841)
	at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510)
	at java.base/java.util.concurrent.CompletableFuture.complete(CompletableFuture.java:2147)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.MakeAsyncHttpRequestStage.completeResponseFuture(MakeAsyncHttpRequestStage.java:253)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.MakeAsyncHttpRequestStage.lambda$executeHttpRequest$3(MakeAsyncHttpRequestStage.java:167)
	at java.base/java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:934)
	at java.base/java.util.concurrent.CompletableFuture$UniHandle.tryFire(CompletableFuture.java:911)
	at java.base/java.util.concurrent.CompletableFuture$Completion.run(CompletableFuture.java:482)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: software.amazon.awssdk.services.s3.model.S3Exception: (Service: S3, Status Code: 400, Request ID: null) (SDK Attempt Count: 1)
	at software.amazon.awssdk.services.s3.model.S3Exception$BuilderImpl.build(S3Exception.java:113)
	at software.amazon.awssdk.services.s3.model.S3Exception$BuilderImpl.build(S3Exception.java:61)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.utils.RetryableStageHelper.retryPolicyDisallowedRetryException(RetryableStageHelper.java:168)
	... 20 more

Regression Issue

  • Select this option if this issue appears to be a regression.

Expected Behavior

Can generate upload files to minio server

Current Behavior

The following is the error log information

上传失败: software.amazon.awssdk.services.s3.model.S3Exception: (Service: S3, Status Code: 400, Request ID: null) (SDK Attempt Count: 1)
java.util.concurrent.CompletionException: software.amazon.awssdk.services.s3.model.S3Exception: (Service: S3, Status Code: 400, Request ID: null) (SDK Attempt Count: 1)
	at software.amazon.awssdk.utils.CompletableFutureUtils.errorAsCompletionException(CompletableFutureUtils.java:64)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.AsyncExecutionFailureExceptionReportingStage.lambda$execute$0(AsyncExecutionFailureExceptionReportingStage.java:51)
	at java.base/java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:934)
	at java.base/java.util.concurrent.CompletableFuture$UniHandle.tryFire(CompletableFuture.java:911)
	at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510)
	at java.base/java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:2162)
	at software.amazon.awssdk.utils.CompletableFutureUtils.lambda$forwardExceptionTo$0(CompletableFutureUtils.java:78)
	at java.base/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863)
	at java.base/java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:841)
	at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510)
	at java.base/java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:2162)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.AsyncRetryableStage$RetryingExecutor.maybeAttemptExecute(AsyncRetryableStage.java:135)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.AsyncRetryableStage$RetryingExecutor.maybeRetryExecute(AsyncRetryableStage.java:152)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.AsyncRetryableStage$RetryingExecutor.lambda$attemptExecute$1(AsyncRetryableStage.java:123)
	at java.base/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863)
	at java.base/java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:841)
	at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510)
	at java.base/java.util.concurrent.CompletableFuture.complete(CompletableFuture.java:2147)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.MakeAsyncHttpRequestStage.lambda$execute$0(MakeAsyncHttpRequestStage.java:110)
	at java.base/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863)
	at java.base/java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:841)
	at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510)
	at java.base/java.util.concurrent.CompletableFuture.complete(CompletableFuture.java:2147)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.MakeAsyncHttpRequestStage.completeResponseFuture(MakeAsyncHttpRequestStage.java:253)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.MakeAsyncHttpRequestStage.lambda$executeHttpRequest$3(MakeAsyncHttpRequestStage.java:167)
	at java.base/java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:934)
	at java.base/java.util.concurrent.CompletableFuture$UniHandle.tryFire(CompletableFuture.java:911)
	at java.base/java.util.concurrent.CompletableFuture$Completion.run(CompletableFuture.java:482)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: software.amazon.awssdk.services.s3.model.S3Exception: (Service: S3, Status Code: 400, Request ID: null) (SDK Attempt Count: 1)
	at software.amazon.awssdk.services.s3.model.S3Exception$BuilderImpl.build(S3Exception.java:113)
	at software.amazon.awssdk.services.s3.model.S3Exception$BuilderImpl.build(S3Exception.java:61)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.utils.RetryableStageHelper.retryPolicyDisallowedRetryException(RetryableStageHelper.java:168)
	... 20 more

Reproduction Steps

I use AWS SDK v2 to upload files, but I can't upload files to MinIO. Here is my test code

import jakarta.annotation.PostConstruct
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials
import software.amazon.awssdk.auth.credentials.AwsCredentials
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider
import software.amazon.awssdk.core.async.AsyncRequestBody
import software.amazon.awssdk.core.sync.RequestBody
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient
import software.amazon.awssdk.regions.Region.*
import software.amazon.awssdk.services.s3.S3AsyncClient
import software.amazon.awssdk.services.s3.S3Client
import software.amazon.awssdk.services.s3.S3Configuration
import software.amazon.awssdk.services.s3.model.PutObjectRequest
import java.io.File
import java.net.URI
import java.nio.file.Paths
import java.util.concurrent.CompletableFuture


@Component
class UploadTest {

    private val logger = LoggerFactory.getLogger(UploadTest::class.java)


    @PostConstruct
    fun uploadToMinio() {
        println("开始上传!")

        val endpoint = "https://oss.ucanfly.com.cn:8081"
        val accessKey = "ucanfly2022"
        val secretKey = "ucanfly82321258"
        val bucketName = "ucanfly"
        val objectKey = "WechatIMG8038.png"
        val filePath = "/Users/liqisheng/Downloads/WechatIMG8038.png"

        val credentials = AwsBasicCredentials.create(accessKey, secretKey)

        val serviceConfiguration = S3Configuration.builder()
            //.pathStyleAccessEnabled(true) // ✅ 必须开启 Path-Style
            .build()

        val s3Client = S3Client.builder()
            .endpointOverride(URI.create(endpoint)) // ✅ 自定义 MinIO endpoint
            .region(US_EAST_1) // ✅ Region 必填,但可任意值
            .forcePathStyle(true)
            .credentialsProvider(StaticCredentialsProvider.create(credentials))
            .serviceConfiguration(serviceConfiguration)
            .httpClient(UrlConnectionHttpClient.create())
            .build()

        // 上传对象
        s3Client.putObject(
            PutObjectRequest.builder()
                .bucket(bucketName)
                .key("test/$objectKey")
                .build(),
            RequestBody.fromFile(Paths.get(filePath))
        )

        println("✅ 上传成功!")
    }




   @PostConstruct
    fun uploadToMinioAsync() {
        val accessKey = "ucanfly2022"
        val secretKey = "ucanfly82321258"
        val endpoint = "https://oss.ucanfly.com.cn:8081"
        val bucket = "ucanfly"
        val objectKey = "WechatIMG8038.png"
        val filePath = "/Users/liqisheng/Downloads/WechatIMG8038.png"

        val credentials = StaticCredentialsProvider.create(
            AwsBasicCredentials.create(accessKey, secretKey)
        )

        val s3AsyncClient = S3AsyncClient.builder()
            .endpointOverride(URI.create(endpoint))
            .credentialsProvider(credentials)
            .region(US_EAST_1)
            .forcePathStyle(true)
            .build()

        val putObjectRequest = PutObjectRequest.builder()
            .bucket(bucket)
            .key(objectKey)
            .build()

        val putObjectFuture: CompletableFuture<Void> = s3AsyncClient.putObject(
            putObjectRequest,
            AsyncRequestBody.fromFile(File(filePath))
        ).thenAccept {
            println("✅ 文件上传成功: $objectKey")
        }.exceptionally { ex ->
            println("❌ 上传失败: ${ex.message}")
            ex.printStackTrace()
            null
        }

        // 等待异步上传完成
        putObjectFuture.join()
    }


}

Possible Solution

No response

Additional Information/Context

No response

AWS Java SDK version used

2.31.45

JDK version used

17

Operating System and version

mac0S Ventura 13.6.9

Metadata

Metadata

Assignees

No one assigned

    Labels

    closed-for-stalenessguidanceQuestion that needs advice or information.third-partyThis issue is related to third-party libraries or applications.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions