Nit*_*ith 5 android amazon-s3 retrofit aws-sdk retrofit2
我正在使用 Retrofit2 处理 Android 应用程序中的 API 调用。该应用程序包含多种图像上传功能。所有图像上传功能都连接到两个 API 调用,并将依次触发。
第一个 API 将图像内容上传到服务器,服务器将生成动态签名的 AWS S3 图像 URL 作为响应。
第二个 API 使用上面签名的 URL 并将数据放入 AWS S3 中。
它非常适合小图像。对于大图像,API 失败。此问题的可能解决方案是将上传 API 更改为分段。所以我将第二个 API 更改为“ Retrofit2-multipart ”。但这里的问题是,上传图像后,S3 中的文件已损坏。
所以我的问题是,改造多部分是否适合 ASW S3 上传?我们有什么办法来解决这个问题吗?
请检查我的实施
改造API接口
@Multipart
@PUT
Call<Void> uploadFile(@Url String url,
@Part MultipartBody.Part file);
Run Code Online (Sandbox Code Playgroud)
改造服务创建者
private ApiServicesList createService() {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(300, TimeUnit.SECONDS)
.connectTimeout(360, TimeUnit.SECONDS)
.cache(null)
.addInterceptor(new NetworkInterceptor(context))
.addInterceptor(createLoggingInterceptor())
.addInterceptor(createSessionExpiryInterceptor())
.addInterceptor(createContextHeaderInterceptor())
.build();
return new Retrofit.Builder()
.baseUrl(FirebaseConfig.getInstance().getStagingBaseURl())
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build()
.create(ApiServicesList.class);
}
Run Code Online (Sandbox Code Playgroud)
文件上传类
public void uploadFileTos3(String url, MultipartBody.Part file, Callback<Void> listener) {
Call<Void> call = mAPIServices.uploadFile(url,file);
call.enqueue(listener);
}
Run Code Online (Sandbox Code Playgroud)
演示者 API 调用
networkServices.uploadFileTos3(credentials.getSignedUrl(), prepareRequestBody(file), new Callback<Void>() {
@Override
public void onFailure(Call<Void> call, Throwable t) {
//App Action
}
@Override
public void
onResponse(Call<Void> call, Response<Void> response) {
//App Action
}
});
Run Code Online (Sandbox Code Playgroud)
请求体方法
public static MultipartBody.Part prepareRequestBody(File file) {
RequestBody requestFile = RequestBody.create(MediaType.parse(CONTENT_TYPE), file); // Checked Both "image/jpeg" and "multipart/form-data"
return MultipartBody.Part.createFormData("image", file.getName(), requestFile);
}
Run Code Online (Sandbox Code Playgroud)
DDMS日志
2019-04-10 19:07:13.926 18761-18874/com.xxx D/OkHttp: --> PUT https:Signed URL(Removed actual URL)
2019-04-10 19:07:13.926 18761-18874/com.xxx D/OkHttp: Content-Type: multipart/form-data; boundary=60561d1c-ff3f-4a43-8022-ca2be3e8ec4e
2019-04-10 19:07:13.926 18761-18874/com.xxx D/OkHttp: Content-Length: 71154
2019-04-10 19:07:13.931 18761-18874/com.xxx D/OkHttp: --60561d1c-ff3f-4a43-8022-ca2be3e8ec4e
2019-04-10 19:07:13.931 18761-18874/com.xxx D/OkHttp: Content-Disposition: form-data; name="image"; filename="forest-trees-fog-foggy.jpg"
2019-04-10 19:07:13.931 18761-18874/com.xxx D/OkHttp: Content-Type: image/jpeg
2019-04-10 19:07:13.931 18761-18874/com.xxx D/OkHttp: Content-Length: 70934
2019-04-10 19:07:13.931 18761-18874/com.xxx D/OkHttp: Image Body
2019-04-10 19:07:13.942 18761-18874/com.xxx D/OkHttp: --60561d1c-ff3f-4a43-8022-ca2be3e8ec4e--
2019-04-10 19:07:13.942 18761-18874/com.xxx D/OkHttp: --> END PUT (71154-byte body)
2019-04-10 19:07:15.648 18761-18874/com.xxx D/OkHttp: <-- 200 OK
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1094 次 |
最近记录: |