How to use form fields in the same order for Amazon S3 upload file using presigned url

我怕爱的太早我们不能终老 提交于 2020-05-31 04:16:53

问题


I have a postdata presigned URL of Amazon S3. I want to use it in a Karate feature file to upload a file (say: pdf)

Here is a sample Curl request that I need to perform Using Karate POST request

curl --location --request POST '<s3bucketURL>' \
--form 'key=some_key_fileName' \
--form 'x-amz-meta-payload={JsonObject}' \
--form 'Content-Type=application/pdf' \
--form 'bucket=<BucketName>' \
--form 'X-Amz-Algorithm=AWS4-HMAC-SHA256' \
--form 'X-Amz-Credential=<AWS_Credential>' \
--form 'X-Amz-Date=<Date>' \
--form 'Policy=<Policy_Hash>' \
--form 'X-Amz-Signature=<Signature_Hash>' \
--form 'file=@/Users/sahildua/validfile.pdf'  

I got a response (having the preSignedUrl) from a server and used using below code in a feature-file

  "url": "<s3bucketURL>",
  "fields": {
    "key": "some_key_fileName",
    "x-amz-meta-payload": "{JsonObject}",
    "Content-Type": "application/pdf",
    "bucket": "<BucketName>",
    "X-Amz-Algorithm": "AWS4-HMAC-SHA256",
    "X-Amz-Credential": "<AWS_Credential>",
    "X-Amz-Date": "<Date>",
    "Policy": "<Policy_Hash>",
    "X-Amz-Signature": "<Signature_Hash>"

I tried

Given url response.url
* def fieldData = response.fields
* print fieldData
* form fields fieldData
And multipart file file = { read: '../testData/validPdfFile.pdf'}
When method post
Then match responseStatus ==  204

I see the request format of Karate log as: 18:29:08.560 [ForkJoinPool-1-worker-3] DEBUG com.intuit.karate - request:

2 > POST https://s3.amazonaws.com/devtest
2 > Accept-Encoding: gzip,deflate
2 > Connection: Keep-Alive
2 > Content-Length: 10485
2 > Content-Type: multipart/form-data; boundary=xdW8JqUa4Z2DxRAQSN1i0LntgmN8ZII7VEBuJUs
2 > Host: s3.amazonaws.com
2 > User-Agent: Apache-HttpClient/4.5.11 (Java/13.0.1)

18:29:10.891 [ForkJoinPool-1-worker-3] DEBUG com.intuit.karate - response time in milliseconds: 2328.56
2 < 400
2 < Connection: close
2 < Content-Type: application/xml
2 < Date: Fri, 29 May 2020 12:59:09 GMT
2 < Server: AmazonS3
2 < Transfer-Encoding: chunked
2 < x-amz-id-2: IA9kNim1i7HTUx5RpMmyT34KrRdRj1UfbKGtiWQ7uoj4virV8Oq2UMU8vY/OBqn7T2SLk8VVgo4=
2 < x-amz-request-id: BF19F92FE6DB16E2
<?xml version="1.0" encoding="UTF-8"?>

I see the form data is not even passed in the actual request But I get a validation XML error from Amazon S3 for incorrect order of field values

<Error> 
<Code>InvalidArgument</Code> 
<Message>Bucket POST must contain a field named 'key'. If it is specified, please check the order of the fields.</Message>
<ArgumentName>key</ArgumentName>
<ArgumentValue></ArgumentValue> 
<RequestId><id></RequestId> 
<HostId><someid></HostId> 
</Error>

I expect 204 No Content and the file to be uploaded the S3 bucket


回答1:


I just used multipart fields instead of form fields and it worked Changed: * form fields fieldData to And multipart fields fieldData



来源:https://stackoverflow.com/questions/62087433/how-to-use-form-fields-in-the-same-order-for-amazon-s3-upload-file-using-presign

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