问题
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