使用Backblaze B2 API与OpenFeign上传文件
我正在使用OpenFeign使用Spring Boot,并且正在尝试将文件上传到Backblaze B2。
我已经有b2_authorize_account
和b2_get_upload_url
endpoints工作。现在,我正在尝试将请求发送到b2_upload_file
。
我为假装客户端请求的代码是
@PostMapping()
String b2UploadFile(URI uri,
@RequestHeader("Authorization") String uploadUrlToken,
@RequestHeader("X-Bz-File-Name") String fileName,
@RequestHeader("Content/Type") String contentType,
@RequestHeader("X-Bz-Content-Sha1") String contentSha1,
byte[] file);
当我执行代码时,我会得到java.net.socketException:连接reset Reset
exception。我认为服务器正在关闭连接。
我使用Postman测试了端点,并且可以上传文件。
我应用了相同的标题和值,邮递员和OpenFeign之间的区别在于,在Postman中,我单击了身体 - >二进制 - >在OpenFeign中,上传文件我正在使用byte []
传递文件,而无需任何注释。
在API文档中,它还说我需要内容长度
标题,但是当我将其包含在OpenFeign中时,我将获得数字格式异常。我无法解决它,但是根据我所阅读的openFeign的内容,我应该自动执行此操作。我也没有在Postman中包括标题,它仍然可以正常工作,因为Postman还默认包含它,因此我认为这不是问题。
我尝试使用@requestbody byte []文件
将文件注释,将其作为多部分文件,使用文件类型,使用字符串类型,使用outputStream和InputStream。
API说:
不允许使用JSON参数。要上传的要上传的文件是消息主体,并且不会以任何方式编码。它不是编码URL。它不是哑剧编码。
I'm using Spring Boot with OpenFeign and I'm trying to upload a file to Backblaze B2.
I already have b2_authorize_account
and the b2_get_upload_url
endpoints working. Now I'm trying to send a request to b2_upload_file
.
The code I have for the Feign client request is
@PostMapping()
String b2UploadFile(URI uri,
@RequestHeader("Authorization") String uploadUrlToken,
@RequestHeader("X-Bz-File-Name") String fileName,
@RequestHeader("Content/Type") String contentType,
@RequestHeader("X-Bz-Content-Sha1") String contentSha1,
byte[] file);
When I execute the code I get a java.net.SocketException: Connection reset
exception. I think that the server is closing the connection.
I tested the endpoint using Postman and it works, I was able to upload my file.
I applied the same headers and values, the difference between Postman and OpenFeign is that in Postman I clicked Body -> binary -> upload file where as in OpenFeign I am passing the file using byte[]
without any annotations.
In the API documentation it also says I need the Content-Length
header, however when I include it in OpenFeign I am getting a number format exception. I was unable to resolve it but based on what I have read OpenFeign should do this automatically. I also did not include the header in Postman and it still works as Postman also includes it by default so I don't think that's the issue.
I have tried annotating the file with @RequestBody byte[] file
, making it a multipart file, using the file type, using string type, using outputstream and inputstream.
The API says:
There are no JSON parameters allowed. The file to be uploaded is the message body and is not encoded in any way. It is not URL encoded. It is not MIME encoded.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从评论中的讨论中
OpenFeign不允许您直接设置
content-type
标题,因此您必须指定:原始答案:
in您发布的代码
应该是
From discussion in the comments
OpenFeign doesn't let you set the
Content-Type
header directly, so you have to specify:Original Answer:
In the code you posted,
should be