Uploading file to S3 using Rest Assured multipart

会有一股神秘感。 提交于 2021-02-08 10:18:29

问题


I am trying to do a PUT request to S3 in order to upload a file. Below the Rest Assured code that I have so far,

String putURL = "A lengthy URL that is generated dynmaically";
String fileId = "A random 40 digit key generated by our server";
String cKey = "some key given by amazon";
String cMD5 = "some md5 hash value";

Response r = given().contentType("multipart/mixed").
                headers("x-amz-header1",cKey,
                        "x-amz-header2",cMD5,
                        "x-amz-header7",accountId,
                        "x-amz-header6",fileId,
                        "x-amz-header5","abc",
                        "x-amz-header4","image/jpeg",
                        "x-amz-header3","true",
                        "Content-Type","application/octet-stream").
                multiPart(new File("src/test/resources/media/ToBeRemoved.jpg")).
                put(putURL);
System.out.println("*********Response code: "+r.getStatusCode());

I always get 400(Bad Request) from the server. I am not sure if I am using the multipart upload correctly.

But it works fine when I do the same using the any rest client like POSTMAN,

PUT <A lengthy URL that is generated dynmaically>
Host: abcd.s3-accelerate.amazonaws.com
x-amz-header1: cKey
x-amz-header2:cMD5
x-amz-header7:accountId
x-amz-header6:fileId
x-amz-header5:abc
x-amz-header4:image/jpeg
x-amz-header3:true
Content-Type:application/octet-stream"
Cache-Control: no-cache
Postman-Token: 687761ef-e059-4a07-eee4-9755931d060a

It would be helpful, if anyone can throw some light on the multipart upload in rest assured.

I have already checked the below links,

  • Rest-assured docs
  • Rest-assured examples

EDIT1:

I tried converting the above POSTMAN request to curl and tried with CURL and even that works fine.

So there is something wrong in the way I use multipart in rest assured.

EDIT2:

It turns out that I am using rest assured properly, I get 403 Signature mismatch error from S3. Although the signature it shows in the logs is same as what AWS is expecting. After a little more research, I understand that "Signature Mismatch" error is sent even if any of the header parameters are missing.


回答1:


Finally I found a solution for this.

First, the URL was already encoded. So I had to instruct RA not to encode it again,

given().urlEncodingEnabled(false). ...

Second, AWS expects the charset to be empty. You can instruct RA to do this using the below line of code,

given().
 config(RestAssured.config().encoderConfig(EncoderConfig.encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false))).

Reference links:

GitHub issue link that talks about this particular issue.

Special thanks to my friend Saurabh who helped me analyzing the issue and guiding in the right direction to help find the solution online.




回答2:


There are indications that REST-assured does not support multipart/mixed properly such as this issue: https://github.com/rest-assured/rest-assured/issues/374 - and I know one team within my organization which encountered the same issue in the past.

If you are open to evaluating alternative JVM-based tools, may I recommend Karate (disclaimer: I'm the dev) - since it has comprehensive support for file-upload, including multipart/mixed.



来源:https://stackoverflow.com/questions/45151838/uploading-file-to-s3-using-rest-assured-multipart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!