Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dseerapu/076756044afae97c26cfb0499fa2ddfb to your computer and use it in GitHub Desktop.
Save dseerapu/076756044afae97c26cfb0499fa2ddfb to your computer and use it in GitHub Desktop.
RxAWSDownloadUtil
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 AWSDownloadUtil implements ObservableOnSubscribe<String> {
private String COGNITO_POOL_ID = "";
private String COGNITO_POOL_REGION = "";
private String BUCKET_NAME = "";
private static final String TAG = "AWSDownloadUtil";
private AmazonS3Client sS3Client;
private CognitoCachingCredentialsProvider sCredProvider;
private TransferUtility sTransferUtility;
private static final AWSDownloadUtil ourInstance = new AWSDownloadUtil();
private Context context;
private String fileToDownload;
public static AWSDownloadUtil getInstance() {
return ourInstance;
}
private AWSDownloadUtil() {
}
private AWSDownloadUtil(Context context, String fileToDownload) {
this.context = context;
this.fileToDownload = fileToDownload;
}
/**
* 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<String> e) throws Exception {
if (e.isDisposed()) {
return;
}
final TransferUtility transferUtility = getTransferUtility(context);
String storagePath = context.getCacheDir();
;
File destinationFile = new File(storagePath + fileToDownload);
if (destinationFile.exists()) {
e.onNext(destinationFile.toString());
return;
}
TransferObserver uploadObserver = transferUtility.download(fileToDownload, destinationFile);
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) {
String file = storagePath + fileToDownload;
e.onNext(file);
}
}
@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) {
ex.printStackTrace();
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 AWSDownloadUtil(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 AWSDownloadUtil(context, fileName));
}
});
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment