8

Are there any examples of uploading a file to an S3 bucket either using Square's OkHttp library or Retrofit library? I'm looking for some examples where I can upload a file using these libraries using a pre-signed query.

Adil B
  • 14,635
  • 11
  • 60
  • 78
  • Right [here](http://stackoverflow.com/questions/32663281/aws-s3-rest-api-with-android-retrofit-v2-library-uploaded-image-is-damaged/32707541), in Stack Overflow. Perhaps the question and some answers can help you there. – Filipe Brito Feb 21 '16 at 15:12

1 Answers1

0

I'm copying this more or less from some pre-production code:

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();
AbuNassar
  • 1,128
  • 12
  • 12