
我正在尝试使用预先签名的URL将文件上传到Amazon的S3.我从生成URL的服务器获取URL.将其作为JSON对象的一部分发送给我.我将URL作为字符串获取,如下所示:
https://com-example-mysite.s3-us-east-1.amazonaws.com/userFolder/ImageName?X-Amz-Security-Token=xxfooxx%2F%2F%2F%2F%2F%2F%2F%2F%2F%2Fxxbarxx%3D&X-Amz-Algorithm=xxAlgoxx&X-Amz-Date=20170831T090152Z&X-Amz-SignedHeaders=host&X-Amz-Expires=3600&X-Amz-Credential=xxcredxx&X-Amz-Signature=xxsignxx
不幸的是,当我将其传递给Retrofit2时,它修改了String并试图将其制成URL.我已经设置了enCoding = true,它可以解决大多数问题,但不能完全解决.我知道字符串按原样工作.我已经在Postman&得到成功的回应.
1st我尝试只是将String(除了我作为baseUrl剪切的东西)整体放入Path
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”之后)转换为“?”.这会导致请求错误/ 400.
我的下一个尝试是使用Retrofit2创建查询,然后将整个String(带有多个查询)转储到查询中.
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);这得到“?”正确呈现,但所有的’&’更改为“&”
最后,我尝试在baseUrl()中传递整个String,但是由于结尾没有’/’,因此给出了IllegalArgumentException.
我知道我可以解析预先签名的URL来进行多个查询,在应进行查询的情况下将它们组装在Retrofit2中,但我想避免这种处理.
要重申这个问题:
有没有一种方法可以使用Retrofit2轻松地(无需进行大量的字符串分析)将带有预签名URL的文件上传到S3?
解决方法:
在同事的帮助下,这就是解决方案.
public interface UpdateImageInterface { @PUT Call<VoID> updateImage(@Url String url, @Body Requestbody image);}调用代码:
String CONTENT_IMAGE = "image/jpeg"; file file = new file(localPhotopath); // create new file on device Requestbody requestfile = Requestbody.create(MediaType.parse(CONTENT_IMAGE), file); /* since the pre-signed URL from S3 contains a host, this dummy URL will * be replaced completely by the pre-signed URL. (I'm using baseURl(String) here * but see baseUrl(okhttp3.httpUrl) in Javadoc for how base URLs are handled */ Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://www.dummy.com/") .build(); UpdateImageInterface imageInterface = retrofit.create(UpdateImageInterface.class); // imageUrl is the String as received from AWS S3 Call<VoID> call = imageInterface.updateImage(imageUrl, requestfile);Javadoc获取@Url(类Url)&
baseUrl()(Retrofit.Builder类)
MediaType是OkHttp库中的一个类,经常与Retrofit(均来自Square)一起使用.可以在Javadoc中找到有关传递给parse方法的常量的信息.
总结以上是内存溢出为你收集整理的java-使用Retrofit2将文件上传到AWS S3预签名URL全部内容,希望文章能够帮你解决java-使用Retrofit2将文件上传到AWS S3预签名URL所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)