jeudi 27 août 2015

AWS SignatureDoesNotMatch exception when URLEncoding UTF-8 filename with ContentDisposition in Java

I am trying to upload a file to S3 and urlencode the filename with Amazon SDK's ObjectMetadata.setContentDisposition(). The filename can contain any UTF-8 character and spaces. Right now the filename breaks on spaces and I get an error when using special case characters.

The code snippet where I use it is as follows:

ObjectMetadata fileMetadata = new ObjectMetadata();
fileMetadata.setContentLength(file.length());
fileMetadata.setContentType(metaContentType);
fileMetadata.addUserMetadata("Filename", metaFileName); 
fileMetadata.setContentDisposition("attachment; filename*=UTF-8 " + URLEncoder.encode(metaFileName,"UTF-8"));  

Unfortunately this does not work as excpected as I am getting the exception: "AmazonS3Exception: Status Code: 403, AWS Service: Amazon S3, AWS Request ID: 3F2C3AE3420561BA, AWS Error Code: SignatureDoesNotMatch, AWS Error Message: The request signature we calculated does not match the signature you provided. Check your key and signing method.".

The full stacktrace:

at services.AWS_S3_Uploader.AWS_S3_Upload.service(AWS_S3_Upload.java:281)
    at com.sonicsw.xqimpl.service.ServiceMessageHandler.callService(ServiceMessageHandler.java:461)
    at com.sonicsw.xqimpl.service.ServiceMessageHandler.handleMessageCommon(ServiceMessageHandler.java:273)
    at com.sonicsw.xqimpl.service.ServiceMessageHandler.handleMessage(ServiceMessageHandler.java:136)
    at com.sonicsw.xqimpl.service.XQDispatcher.onMessage(XQDispatcher.java:460)
    at com.sonicsw.esb.itinerary.model.EsbStepNode.doExecute(EsbStepNode.java:296)
    at com.sonicsw.esb.itinerary.model.EsbStepNode.execute(EsbStepNode.java:180)
    at com.sonicsw.esb.process.model.impl.DefaultActivityNode.offerIncoming(DefaultActivityNode.java:140)
    at com.sonicsw.esb.itinerary.engine.ItineraryEngine.executeProcess(ItineraryEngine.java:338)
    at com.sonicsw.esb.itinerary.engine.ItineraryEngine.handleToken(ItineraryEngine.java:181)
    at com.sonicsw.esb.itinerary.engine.ItineraryEngine.handleMessage(ItineraryEngine.java:171)
    at com.sonicsw.xqimpl.endpoint.container.EndpointContextContainer.onMessage(EndpointContextContainer.java:116)
    at com.sonicsw.xq.connector.jms.JMSEndpointMessageListener.onMessage(JMSEndpointMessageListener.java:259)
    at progress.message.jimpl.Session.deliver(Session.java:3086)
    at progress.message.jimpl.Session.run(Session.java:2489)
    at progress.message.jimpl.Session$SessionThread.run(Session.java:2881)
Caused by: java.lang.Exception: AmazonServiceException
    at services.AWS_S3_Uploader.AWS_S3.logAmazonServiceException(AWS_S3.java:340)
    at services.AWS_S3_Uploader.AWS_S3.uploadObject(AWS_S3.java:280)
    at services.AWS_S3_Uploader.AWS_S3_Upload.service(AWS_S3_Upload.java:263)
    ... 15 more
Caused by: AmazonS3Exception: Status Code: 403, AWS Service: Amazon S3, AWS Request ID: 3F2C3AE3420561BA, AWS Error Code: SignatureDoesNotMatch, AWS Error Message: The request signature we calculated does not match the signature you provided. Check your key and signing method., S3 Extended Request ID: 0AlM9giKI8hRMWG0Xc84lxXSJ8ZwUPB2DjHKddwa1oVZ4AWUzlyko7jEko9MEtwkd5Tc7xocFJw=
    at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:644)
    at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:338)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:190)
    at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:2979)
    at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1159)
    at com.amazonaws.services.s3.transfer.internal.UploadCallable.uploadInOneChunk(UploadCallable.java:97)
    at com.amazonaws.services.s3.transfer.internal.UploadCallable.call(UploadCallable.java:89)
    at com.amazonaws.services.s3.transfer.internal.UploadMonitor.upload(UploadMonitor.java:179)
    at com.amazonaws.services.s3.transfer.internal.UploadMonitor.call(UploadMonitor.java:137)
    at com.amazonaws.services.s3.transfer.internal.UploadMonitor.call(UploadMonitor.java:48)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

I am using:
Java 1.6
aws-java-sdk-1.10.2.jar

All help and suggestions are welcome. If more information is needed, then just ask.

Br.




Aucun commentaire:

Enregistrer un commentaire