问题
I am trying to replicate a postman put request where it is uploading a csv file on the S3 location given by a code.
The url is like https://us-east-1-e9qpbo283.s3.amazonaws.com/bulk-bucket/a4894e7b-7e42-4fcc-9f84-e7n00db6d581/input/file
with query params as {X-Amz-Date=20200226T113914Z, X-Amz-Algorithm=AWS4-HMAC-SHA256, X-Amz-Signature=<Some Signature>, X-Amz-SignedHeaders=content-type%3Bhost, X-Amz-Security-Token=<SOME TOKEN HERE>, X-Amz-Credential=ASIAV7AYOYCBQB4VDGD7%2F20200226%2Fus-east-1%2Fs3%2Faws4_request, X-Amz-Expires=3600}
But I am always getting 400 bad request. The request also doesn't require any other auth token. Can someone help me with this.
I have already visited and tried the links
Uploading file to S3 using Rest Assured multipart
https://groups.google.com/d/topic/rest-assured/MPzbiozclqg
https://github.com/rest-assured/rest-assured/issues/627
My Code is like
File uploadFile = new File("/home/beast/Downloads/locations.csv");
RequestSpecification request = given().urlEncodingEnabled(false).
config(RestAssured.config().encoderConfig(EncoderConfig.encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false)))
.multiPart("file", uploadFile, "csv");
Response r = request.put(URL);
PS: The error is of Signature Mismatch.
回答1:
Since you are passing the CSV as a binary file in the body you can just open the file and pass it in the body like in below example:
RestAssured.urlEncodingEnabled = false;
File uploadFile = new File("COMPLETE_FILE_PATH");
Response response = given().contentType("text/csv") .config(RestAssured.config().encoderConfig(EncoderConfig.encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false)))
.body(uploadFile)
.when()
.put(uploadUrl)
.then().extract().response();
来源:https://stackoverflow.com/questions/60414926/uploading-csv-file-to-s3-using-rest-assured