skip to Main Content

Context : I trying to upload a file to aws s3 via http endpoint built using quarkus framework

Deployment on EKS : enter image description here

Code Snippet:

@Path("/test")
@POST
public Response testEndpoint(){
    String message = "Hello, this is an arbitrary string message!";
    byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8);
    PutObjectRequest putObjectRequest = PutObjectRequest.builder()
            .bucket(bucketname)
            .key("your-object-key")
            .contentType("text/plain")
            .contentLength((long) messageBytes.length)
            .build();


    PutObjectResponse putResponse = s3Client.putObject(putObjectRequest, RequestBody.fromBytes(messageBytes));

    if (putResponse != null) {
        return Response.ok().status(Status.CREATED).build();
    } else {
        return Response.serverError().build();
    }
}

application.properties:

bucket.name=${BUCKET_NAME:s3://fileupload-test} 
quarkus.s3.aws.region=${AWS_REGION:us-east-1}
quarkus.s3.aws.credentials.type=${AWS_CRED_TYPE:default}

Getting below error , any input will be helpful

2023-12-13 09:10:22,665 ERROR [io.qua.ver.htt.run.QuarkusErrorHandler] (executor-thread-2) HTTP Request to /test failed, error id: 60b1f0f5-6c1f-4808-8197-3c3280b778d9-4: software.amazon.awssdk.services.s3.model.S3Exception: null (Service: S3, Status Code: 400, Request ID: 36F61VZ3D0PHJ2GN, Extended Request ID: 7NvFarwcR64a8KxaKUbjRGpfYcr8TXa1txKoblVWkCgXlVY/0uXnrg+jefCx3DD+2HKowEYoSGc=)
at software.amazon.awssdk.protocols.xml.internal.unmarshall.AwsXmlPredicatedResponseHandler.handleErrorResponse(AwsXmlPredicatedResponseHandler.java:156)
at software.amazon.awssdk.protocols.xml.internal.unmarshall.AwsXmlPredicatedResponseHandler.handleResponse(AwsXmlPredicatedResponseHandler.java:108)
at software.amazon.awssdk.protocols.xml.internal.unmarshall.AwsXmlPredicatedResponseHandler.handle(AwsXmlPredicatedResponseHandler.java:85)
at software.amazon.awssdk.protocols.xml.internal.unmarshall.AwsXmlPredicatedResponseHandler.handle(AwsXmlPredicatedResponseHandler.java:43)
at software.amazon.awssdk.awscore.client.handler.AwsSyncClientHandler$Crc32ValidationResponseHandler.handle(AwsSyncClientHandler.java:93)
at software.amazon.awssdk.core.internal.handler.BaseClientHandler.lambda$successTransformationResponseHandler$7(BaseClientHandler.java:279)
at com.resource.RestResource.testEndpoint(RestResource.java:62)
at com.datastore.resource.RestResource$quarkusrestinvoker$testEndpoint_0f46b3e633df39664d00b95fa37bc49935818083.invoke(Unknown Source)
at org.jboss.resteasy.reactive.server.handlers.InvocationHandler.handle(InvocationHandler.java:29)
at io.quarkus.resteasy.reactive.server.runtime.QuarkusResteasyReactiveRequestContext.invokeHandler(QuarkusResteasyReactiveRequestContext.java:141)
at org.jboss.resteasy.reactive.common.core.AbstractResteasyReactiveContext.run(AbstractResteasyReactiveContext.java:147)
at io.quarkus.vertx.core.runtime.VertxCoreRecorder$14.runWith(VertxCoreRecorder.java:582)
at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2513)
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1538)
at org.jboss.threads.DelegatingRunnable.run(DelegatingRunnable.java:29)
at org.jboss.threads.ThreadLocalResettingRunnable.run(ThreadLocalResettingRunnable.java:29)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.base/java.lang.Thread.run(Thread.java:833)

2

Answers


  1. Chosen as BEST ANSWER
    1. From the exception that was thrown it was clear that there was an issue with the request but the error message was not clear about the problem with the request.
    2. Built the whole application in springboot(initial app was built on quarkus) and deployed and tried the same , this time the error message was "s3 bucket is not present"
    3. The problem was with how the bucket name was specified, it should not be provided as "s3://fileupload-test" and should be just "fileupload-test". After making this change was able to upload the file to s3.

  2. I justed tested your code using JDK 17 in IntelliJ. I am not using the other framework (quarkus) you are. Your code looks ok; however, you should put a txt file extension for the key. You should also debug your S3 Service Client to ensure it’s fine.

    This AWS S3 Java V2 code works:

    package com.example.s3.test;
    
    import software.amazon.awssdk.regions.Region;
    import software.amazon.awssdk.services.s3.S3Client;
    import software.amazon.awssdk.services.s3.model.PutObjectRequest;
    import software.amazon.awssdk.services.s3.model.PutObjectResponse;
    import  software.amazon.awssdk.core.sync.RequestBody;
    import software.amazon.awssdk.services.s3.model.S3Exception;
    
    import java.nio.charset.StandardCharsets;
    
    public class PutObject {
    
        public static void main(String[] args) {
            String bucketName = "myjunebucket";
            String objectKey = "mytestfile.txt";
    
            System.out.println("Putting object " + objectKey + " into bucket " + bucketName + " with checksum in algorithm sha256.");
            System.out.println("  in bucket: " + bucketName);
    
            Region region = Region.US_EAST_1;
            S3Client s3Client = S3Client.builder()
                .region(region)
                .build();
    
            putObj( s3Client, bucketName,  objectKey);
        }
    
        public static void putObj( S3Client s3Client, String bucketName,  String objectKey) {
            String message = "Hello, this is an arbitrary string message!";
            byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8);
            PutObjectRequest putObjectRequest = PutObjectRequest.builder()
                .bucket(bucketName)
                .key(objectKey)
                .contentType("text/plain")
                .contentLength((long) messageBytes.length)
                .build();
    
            try {
                PutObjectResponse putResponse = s3Client.putObject(putObjectRequest, RequestBody.fromBytes(messageBytes));
                if (putResponse != null) {
                    System.out.println("File was uploaded");
                }
            } catch (S3Exception e) {
                System.err.println(e.awsErrorDetails().errorMessage());
                System.exit(1);
            }
        }
    }
    

    See this screenshot. The Response Object is valid:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search