使用 Rest Assured multipart 将文件上传到 S3 [英] Uploading file to S3 using Rest Assured multipart

查看:134
本文介绍了使用 Rest Assured multipart 将文件上传到 S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向 S3 发出 PUT 请求以上传文件.在我目前拥有的 Rest Assured 代码下方,

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());

我总是从服务器收到 400(错误请求).我不确定我是否正确使用了分段上传.

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

但是当我使用任何其他客户端(如 POSTMAN)执行相同操作时,它工作正常,

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,

我尝试将上述 POSTMAN 请求转换为 curl 并尝试使用 CURL,即使这样也能正常工作.

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

所以我放心使用 multipart 的方式有问题.

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

事实证明,我使用放心,我从 S3 收到 403 签名不匹配错误.尽管它在日志中显示的签名与 AWS 期望的签名相同.经过更多研究,我了解到即使缺少任何标头参数也会发送签名不匹配"错误.

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.

推荐答案

我终于找到了解决方案.

Finally I found a solution for this.

首先,URL 已经被编码.所以我不得不指示RA不要再次编码,

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

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

其次,AWS 期望字符集为空.您可以使用以下代码行指示 RA 执行此操作,

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))).

参考链接:

GitHub 问题链接,讨论此特定问题.

特别感谢我的朋友 Saurabh,他帮助我分析了问题并指导了正确的方向,帮助我在网上找到了解决方案.

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

这篇关于使用 Rest Assured multipart 将文件上传到 S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆