Context : I trying to upload a file to aws s3 via http endpoint built using quarkus framework
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
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:
See this screenshot. The Response Object is valid: