Hi! I'm using S3 TransferUtility to upload the files, the issue that i'm facing is that it stops uploading when application is closed. I want it to keep uploading running in background, for that purpose i started upload in WorkManager but still it finishes upload. What is the issue?
public class TestPhotoUploadManager extends BaseWorker {
private FileTransferUtil fileTransferUtil;
@NonNull
@Override
public Result doWork() {
try {
fileTransferUtil = new FileTransferUtil();
String filePath = getExtras().getInputData().getString(CommonTags.URL);
if (filePath != null) {
uploadWithTransferUtility(filePath);
}
return workerResult[0];
} catch (Exception e) {
e.printStackTrace();
setWorkerResult(Result.FAILURE);
return workerResult[0];
}
}
private void uploadWithTransferUtility(String filePath) {
TransferUtility transferUtility = fileTransferUtil.getTransferUtility(getApplicationContext());
String fileName = filePath.substring(filePath.lastIndexOf("/"));
AppLogger.d("usm_s3_file", "name= " + fileName + " ,path= " + filePath);
String folderName = AmazonHelper.BUCKET_NAME;
TransferObserver uploadObserver =
transferUtility.upload(AmazonHelper.BUCKET_NAME, folderName + fileName, new File(filePath));
uploadObserver.setTransferListener(new TransferListener() {
@Override
public void onError(int id, Exception e) {
setWorkerResult(Result.FAILURE);
}
@Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
}
@Override
public void onStateChanged(int id, TransferState newState) {
switch (newState) {
case WAITING_FOR_NETWORK:
case PENDING_NETWORK_DISCONNECT:
setWorkerResult(Result.RETRY);
break;
case COMPLETED:
setWorkerResult(Result.SUCCESS);
break;
case WAITING:
case IN_PROGRESS:
break;
default:
setWorkerResult(Result.FAILURE);
break;
}
}
});
}
@usmanrana07 What is the reason you are using a WorkManager? The TransferUtility implements a service where it spawns threads to do the upload/download and supports background operations.
TransferUtility stops uploading when application is closed. I want to use WorkManager so it can continue upload regarless of app lifecycle and can be rescheduled automatically when network is lost and reconnected.
@usmanrana07 Sorry for the delayed response. TransferUtility uses a service (TransferService) to perform uploads/downloads in the background. TransferService spawns threads from the shared thread pool to accomplish this task. However, if the app is closed, all the processes/threads in the TransferUtility will be killed. This is a limitation of the TransferUtility.
@kvasukib Is there anyway in the SDK to just do a blocking upload? I too am using WorkManager, here's why. When picking a file from an intent you get a Uri, this Uri isn't always of a file scheme. So unless I'm missing something in the AWS SDK I need to use the contentResolver to create a temp file from that Uri so I now have a File that I can give the AWS SDK. If my user's pick many files from the intent I'd much rather off load the process of creating the temp files and uploading the files to WorkManager. This way I can handle file creation errors and upload errors in a much more user friendly and reliable way.
@Shakezulla57 You could use the AmazonS3Client (Low-level client) which does blocking (synchronous) transfers. For example:
PutObjectResult result = amazonS3Client.putObject(putObjectRequest);
is a blocking upload that makes network calls on the same thread where this is been invoked and blocks until the operation completes successfully or not.
You could wrap a call to putObject in WorkManager.
@kvasukib I'll check that out. Thank you.
Most helpful comment
@Shakezulla57 You could use the
AmazonS3Client(Low-level client) which does blocking (synchronous) transfers. For example:is a blocking upload that makes network calls on the same thread where this is been invoked and blocks until the operation completes successfully or not.
You could wrap a call to
putObjectinWorkManager.