使用OkHttp或Retrofit将文件上载到AWS S3存储桶

时间:2015-06-24 09:29:55

标签: android amazon-s3 retrofit okhttp

是否有使用Square的OkHttp库或Retrofit库将文件上传到S3存储桶的示例?我正在寻找一些示例,我可以使用预先签名的查询使用这些库上传文件。

1 个答案:

答案 0 :(得分:0)

我正在从一些预生产代码中复制或多或少的内容:

OkHttpClient.Builder builder = new OkHttpClient.Builder().proxy(proxy).writeTimeout(10, TimeUnit.MINUTES);
this.client = builder.build();
// ...
Request.Builder requestBuilder = new Request.Builder().url(uploadUrl);
for (TagEntry entry : item.getUploadUrlHeaders()) {
    requestBuilder.addHeader(entry.getKey(), entry.getValue());
}

RequestBody requestBody = RequestBody.create(BINARY_MEDIA_TYPE, file.toFile());
requestBuilder.put(requestBody);
final Response response = client.newCall(requestBuilder.build()).execute();
assert response.isSuccessful();