Last active
August 29, 2015 14:05
-
-
Save hectorups/004f30bdbdb7ad14514b to your computer and use it in GitHub Desktop.
class to upload to S3
This file contains hidden or 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
| public class AWSClient implements AWSApi { | |
| private final AWSApi awsApi; | |
| @Inject OkHttpClient client; | |
| @Inject ErrorHandler errorHandler; | |
| public AWSClient(final DottieApplication application) { | |
| application.inject(this); | |
| RestAdapter restAdapter = new RestAdapter.Builder().setClient(new OkClient(client)) | |
| .setEndpoint(Constants.AWS_ENDPOINT) | |
| .setErrorHandler(errorHandler) | |
| .build(); | |
| awsApi = restAdapter.create(AWSApi.class); | |
| } | |
| @Override public Observable<Response> uploadImage(@Part("key") TypedString key, | |
| @Part("acl") TypedString acl, @Part("AWSAccessKeyId") TypedString awsAccessKeyId, | |
| @Part("Policy") TypedString policy, @Part("Signature") TypedString signature, | |
| @Part("Content-Type") TypedString contentType, @Part("file") TypedFile file) { | |
| return awsApi.uploadImage(key, acl, awsAccessKeyId, policy, signature, contentType, file); | |
| } | |
| public Observable<String> uploadJpeg(TypedString fileName, TypedFile file) { | |
| return uploadImage(fileName, new TypedString("public-read"), | |
| new TypedString(Constants.AWS_ACCESS_KEY), new TypedString(Constants.AWS_POLICY), | |
| new TypedString(Constants.AWS_SIGNATURE), new TypedString("image/jpeg"), file).flatMap( | |
| new Func1<Response, Observable<String>>() { | |
| @Override public Observable<String> call(Response response) { | |
| List<Header> headers = response.getHeaders(); | |
| String locationHeader = null; | |
| for (Header header : headers) { | |
| if (header.getName() != null && header.getName().contentEquals("Location")) { | |
| // Uploaded File Url | |
| locationHeader = header.getValue(); | |
| break; | |
| } | |
| } | |
| final String fileUrl = locationHeader; | |
| return Observable.create(new Observable.OnSubscribe<String>() { | |
| @Override public void call(Subscriber<? super String> subscriber) { | |
| if (TextUtils.isEmpty(fileUrl)) { | |
| subscriber.onError(new Exception()); | |
| return; | |
| } | |
| subscriber.onNext(fileUrl); | |
| subscriber.onCompleted(); | |
| } | |
| }); | |
| } | |
| } | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hector: Do you have code for AWSApi? Thanks!