Skip to content

Instantly share code, notes, and snippets.

@UttamPanchasara
Last active May 3, 2019 13:27
Show Gist options
  • Save UttamPanchasara/7d3e7cfbb5b66bb699e9d0d10e771ce6 to your computer and use it in GitHub Desktop.
Save UttamPanchasara/7d3e7cfbb5b66bb699e9d0d10e771ce6 to your computer and use it in GitHub Desktop.
RxAWSUploadUtil
package com.sample.awsutils;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.CognitoCachingCredentialsProvider;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferListener;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferState;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility;
import com.amazonaws.services.s3.AmazonS3Client;
import java.io.File;
import java.util.List;
import java.util.concurrent.Callable;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;
public class AWSUploadUtil implements ObservableOnSubscribe<String> {
private String COGNITO_POOL_ID = "";
private String COGNITO_POOL_REGION = "";
private String BUCKET_NAME = "";
private static final String TAG = "AWSUploadUtil";
private AmazonS3Client sS3Client;
private CognitoCachingCredentialsProvider sCredProvider;
private TransferUtility sTransferUtility;
private static final AWSUploadUtil ourInstance = new AWSUploadUtil();
private Context context;
private String fileToUpload;
public static AWSUploadUtil getInstance() {
return ourInstance;
}
private AWSUploadUtil() {
}
private AWSUploadUtil(Context context, String fileToUpload) {
this.context = context;
this.fileToUpload = fileToUpload;
}
/**
* Gets an instance of a S3 client which is constructed using the given
* Context.
*
* @param context An Context instance.
* @return A default S3 client.
*/
public AmazonS3Client getS3Client(Context context) {
if (sS3Client == null) {
ClientConfiguration clientConfiguration = new ClientConfiguration();
clientConfiguration.setMaxErrorRetry(10);
clientConfiguration.setConnectionTimeout(50 * 1000); // default is 10 secs
clientConfiguration.setSocketTimeout(100 * 1000); // default is 50 secs
sS3Client = new AmazonS3Client(getCredProvider(context.getApplicationContext()), clientConfiguration);
}
return sS3Client;
}
/**
* Gets an instance of CognitoCachingCredentialsProvider which is
* constructed using the given Context.
*
* @param context An Context instance.
* @return A default credential provider.
*/
private CognitoCachingCredentialsProvider getCredProvider(Context context) {
if (sCredProvider == null) {
sCredProvider = new CognitoCachingCredentialsProvider(
context.getApplicationContext(),
COGNITO_POOL_ID,
COGNITO_POOL_REGION);
}
return sCredProvider;
}
/**
* Gets an instance of the TransferUtility which is constructed using the
* given Context
*
* @param context
* @return a TransferUtility instance
*/
public TransferUtility getTransferUtility(Context context) {
if (sTransferUtility == null) {
sTransferUtility = TransferUtility.builder()
.context(context.getApplicationContext())
.s3Client(getS3Client(context.getApplicationContext()))
.defaultBucket(BUCKET_NAME)
.build();
}
return sTransferUtility;
}
@Override
public void subscribe(final ObservableEmitter e) throws Exception {
if (e.isDisposed()) {
return;
}
final TransferUtility transferUtility = getTransferUtility(context);
final String key = System.currentTimeMillis() + "_.jpg";
TransferObserver uploadObserver = transferUtility.upload(key, new File(fileToUpload));
uploadObserver.setTransferListener(new TransferListener() {
@Override
public void onStateChanged(int id, TransferState state) {
TransferObserver transferObserver = transferUtility.getTransferById(id);
String keyName = "";
if (transferObserver != null) {
keyName = transferObserver.getKey();
}
if (TransferState.COMPLETED == state) {
e.onNext(key);
}
}
@Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
float percentDonef = ((float) bytesCurrent / (float) bytesTotal) * 100;
int percentDone = (int) percentDonef;
Log.d(TAG, "ID:" + id + " bytesCurrent: " + bytesCurrent + " bytesTotal: " + bytesTotal + " " + percentDone + "%");
}
@Override
public void onError(int id, Exception ex) {
e.onError(ex);
}
});
}
public static Observable<String> create(final Context context, final String fileName) {
return Observable.defer(new Callable<ObservableSource<? extends String>>() {
@Override
public ObservableSource<? extends String> call() throws Exception {
return Observable.create(new AWSUploadUtil(context, fileName));
}
});
}
public static Observable<String> create(final Context context, final List<String> fileNameList) {
return Observable.defer(new Callable<ObservableSource<? extends String>>() {
@SuppressLint("CheckResult")
@Override
public ObservableSource<? extends String> call() throws Exception {
return Observable.fromIterable(fileNameList).flatMap(new Function<String, ObservableSource<String>>() {
@Override
public ObservableSource<String> apply(String fileName) throws Exception {
return Observable.create(new AWSUploadUtil(context, fileName));
}
});
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment