【发布时间】:2017-09-01 00:13:30
【问题描述】:
我正在尝试使用预签名 URL 将文件上传到 Amazon 的 S3。我从生成 URL 的服务器获取 URL,并将其作为 JSON 对象的一部分发送给我。我将 URL 作为字符串获取,如下所示:
不幸的是,当我将它传递给 Retrofit2 时,它会修改试图使其成为 URL 的字符串。我设置了encoding=true,它解决了大部分问题,但没有完全解决。我知道字符串按原样工作。我已经在 Postman 中尝试过,并获得了成功的响应。
第一次我尝试将字符串(除了我剪切为 baseUrl 的内容)作为一个整体放入路径中
public interface UpdateImageInterface {
@PUT("{url}")
Call<Void> updateImage(@Path(value="url", encoded=true) String url, Body RequestBody image);
}
调用代码:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://com-example-mysite.s3-us-east-1.amazonaws.com/userFolder/")
.build();
UpdateImageInterface imageInterface = retrofit.create(UpdateImageInterface.class);
// imageUrl is "ImageName..."
Call<Void> call = imageInterface.updateImage(imageUrl, requestFile);
这主要工作除了'?' (在“ImageName”之后)被转换为“%3F”。这会导致错误请求/400。
我的下一个尝试是使用 Retrofit2 创建一个查询,然后将整个字符串(带有多个查询)转储到查询中。
public interface UpdateImageInterface {
@PUT("ImageName")
Call<Void> updateProfilePhoto(@Query(value="X-Amz-Security-Token", encoded = true) String token, @Body RequestBody image);
}
调用代码:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://com-example-mysite.s3-us-east-1.amazonaws.com/userFolder/")
.build();
UpdateImageInterface imageInterface = retrofit.create(UpdateImageInterface.class);
// imageUrl is "xxfooxx..."
Call<Void> call = imageInterface.updateImage(imageUrl, requestFile);
这会得到“?”正确渲染,但所有 '&' 都更改为 "%26"
最后,我尝试在 baseUrl() 中传递整个字符串,但这会导致 IllegalArgumentException,因为最后没有“/”。
我知道我可以解析预签名的 URL 以进行多个查询并在 Retrofit2 中组装它们,因为应该完成查询,但我想避免这种处理。
重申问题:
有没有一种方法可以使用 Retrofit2 轻松(无需繁重的字符串解析)将文件上传到 S3,并使用预先签名的 URL?
【问题讨论】:
-
图书馆,或者它所依赖的东西,似乎有点坏了。看看 cmets,在这里:github.com/square/retrofit/issues/1199... 请特别注意最后一个。
标签: java android amazon-s3 retrofit2