Created
April 19, 2018 20:28
-
-
Save DevPicon/8f8c371d72ff02771ed576d2a6151c11 to your computer and use it in GitHub Desktop.
Uploading files to S3 with OKHttp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cornershopapp.shopper.android.utils; | |
import java.io.File; | |
import java.io.IOException; | |
import okhttp3.MediaType; | |
import okhttp3.MultipartBody; | |
import okhttp3.OkHttpClient; | |
import okhttp3.Request; | |
import okhttp3.RequestBody; | |
import okhttp3.Response; | |
public class UploadToS3Test { | |
/* | |
{ 'url': 'https://devel.s3.amazonaws.com/', | |
'fields': { | |
'policy': 'eyJjb25kaXRpb2dzIjogW3siYWNsIjogInB1YmxpYy1yZWFkIn0sIHsiYnVja2V0IjogImNvcm5lcnNob3Atc2hvcHBlci1hcGktdGVzdCJ9LCB7ImtleSI6ICJjb3JuZXJzaG9wLXNob3BwZXItYXBpLXRlc3QtZmlsZS0zOTIuanBnIn1dLCAiZXhwaXJhdGlvbiI6ICIyMDE4LTA0LTEzVDIyOjQxOjA0WiJ9', | |
'AWSAccessKeyId': 'AKIAIVQWW6OWBKLD4TXQ', | |
'signature': 'CsM12nmHOEUdf9Z50KF2rYhahko=', | |
'key': 'cornershop-shopper-api-test-file-392.jpg', | |
'acl': 'public-read' | |
} | |
} | |
*/ | |
public static void main(String[] params) { | |
String receivedUrl = "https://cornershop-shopper-api-test.s3.amazonaws.com/"; | |
String fileName = "/Users/devpicon/Dev/javautils/IMG_20180413_115857_609.jpg"; | |
File uploadFile = new File(fileName); | |
String boundary = "===" + System.currentTimeMillis() + "==="; | |
System.out.println("receivedUrl=" + receivedUrl); | |
System.out.println("fileName=" + fileName); | |
System.out.println("boundary=" + boundary); | |
try { | |
uploadObject(uploadFile); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void uploadObject(File file) throws IOException { | |
OkHttpClient client = new OkHttpClient(); | |
MediaType mediaType = MediaType.parse("image/jpg"); | |
RequestBody requestBody = RequestBody.create(mediaType, file); | |
MultipartBody multipartBody = new MultipartBody.Builder("---DevApp") | |
.addFormDataPart("AWSAccessKeyId", "AKIAJCBCKQITFUEAAITA") | |
.addFormDataPart("acl", "public-read") | |
.addFormDataPart("key", "formtasks/images/19463-selfie.jpg") | |
.addFormDataPart("signature", "kos3NHJW+qUaQMpYZfaOs19hUMRw=") | |
.addFormDataPart("policy", "eyJjb25kaXRpb25zIjogd3siYWNsIjogInB1YmxpYy1yZWFkIn0sIHsiQ29udGVudC1UeXBlIjogImltYWdlL2pwZyJ9LCB7ImJ1Y2tldCI6ICJzaG9wcGluZy1kZXZlbCJ9LCB7ImtleSI6ICJmb3JtdGFza3MvaW1hZ2VzLzE5NDYzLXNlbGZpZS5qcGcifV0sICJleHBpcmF0aW9uIjogIjIwMTgtMDQtMTdUMjE6NTU6NDhaIn0=") | |
.addFormDataPart("Content-Type", "image/jpg") | |
.addFormDataPart("file", null, requestBody).build(); | |
Request request = new Request.Builder() | |
.url("https://devel.s3.amazonaws.com/") | |
.post(multipartBody) | |
.addHeader("content-type", "multipart/form-data;") | |
.addHeader("cache-control", "no-cache") | |
.build(); | |
Response execute = client.newCall(request).execute(); | |
System.out.println(execute.body().string()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment