Search code examples
amazon-web-serviceskotlinamazon-s3minio

AWS S3 java SDK: Can't create bucket


I'm tring to create a bucket with following code:

S3 backend i'm using is minio

runCatching {
    bucket = configuration.getString("storage.bucket")
    s3Client = AmazonS3Client.builder()
        .withEndpointConfiguration(AwsClientBuilder.EndpointConfiguration("${configuration.getString("storage.endpoint")}:${configuration.getInt("storage.port")}", ""))
        .withCredentials(AWSStaticCredentialsProvider(BasicAWSCredentials(configuration.getString("storage    .credentials.accessKey"), configuration.getString("storage.credentials.accessToken"))))
        .withPayloadSigningEnabled(false)
        .withClientConfiguration(ClientConfiguration().withProtocol(if(configuration.getBoolean("storage.ssl")) { Protocol.HTTPS } else { Protocol.HTTP }))
        .build()
    s3Client.createBucket(CreateBucketRequest(bucket)) // Exception: com.amazonaws.SdkClientException: Unable to execute HTTP request: usercontent.localhost
                                                       // Exception: Caused by: java.net.UnknownHostException: usercontent.localhost

    }.onFailure {
        it.printStackTrace()
        exitProcess(0)
    }
}

Why create a bucket will send a request to usercontent.localhost? How can i create a bucket?


Solution

  • Hello I had exactly the same problem and the solution was really easy.

    You can find more details in this Issue: https://github.com/localstack/localstack/issues/43

    Here is your coded fixed:

    runCatching {
        bucket = configuration.getString("storage.bucket")
        s3Client = AmazonS3Client.builder()
            .withEndpointConfiguration(AwsClientBuilder.EndpointConfiguration("${configuration.getString("storage.endpoint")}:${configuration.getInt("storage.port")}", ""))
            .withCredentials(AWSStaticCredentialsProvider(BasicAWSCredentials(configuration.getString("storage    .credentials.accessKey"), configuration.getString("storage.credentials.accessToken"))))
            .withPayloadSigningEnabled(false)
            .withClientConfiguration(ClientConfiguration().withProtocol(if(configuration.getBoolean("storage.ssl")) { Protocol.HTTPS } else { Protocol.HTTP }))
            .withPathStyleAccessEnabled(true) // YOU HAVE TO ADD THIS
            .build()
        s3Client.createBucket(CreateBucketRequest(bucket)) // Exception: com.amazonaws.SdkClientException: Unable to execute HTTP request: usercontent.localhost
                                                           // Exception: Caused by: java.net.UnknownHostException: usercontent.localhost
    
        }.onFailure {
            it.printStackTrace()
            exitProcess(0)
        }
    }
    
    

    With this option you avoid using the bucket name in the dns (usercontent.localhost) request and instead uses a path style (localhost/usercontent).