AWS s3 TransferListener not updating in Android

1.4k Views Asked by At

I am trying to download from s3 and update a progress bar. I set up TransferUtility, TransferObserver and TransferListener.

The problem is that as the file downloads it only updates progress rarely.

For a 1.6mb (1665824) file it will output 0, then 30 seconds later 1050264, then 30 seconds later 1665824, then it repeats itself and outputs 1665824 again.

So basically to the user it looks like the download is frozen or at best jerky.

I also tried putting it in a loop and checking the value ever 100ms but it just returns 0s for the first thiry seconds just like the listener.

This is how I am implementing it:

// Create s3 client and set region and endpoints
        AmazonS3Client s3Client = new AmazonS3Client(IdentityManager.getDefaultIdentityManager().getCredentialsProvider(), new ClientConfiguration());
        s3Client.setRegion(Region.getRegion(Regions.US_EAST_1));

        // Create a tranfer utility and attach to s3 client
        TransferUtility transferUtility = TransferUtility.builder().s3Client(s3Client).context(getActivity()).build();

        // String key for the file to download
        String key = "ExampleVideo.mp4";

        // Location to download files from S3
        File file = new File(Environment.getExternalStorageDirectory().toString() + "/" + key);

        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        // Bucket that contains the file to download
        String bucket = "My_Bucket";

        // Create a tranfer observer to download
        TransferObserver observer = transferUtility.download(bucket,key,file);

        // Attach listener to the download to listen for changes in the the download process
        observer.setTransferListener(new TransferListener() {
            @Override
            public void onStateChanged(int id, TransferState state) {

            }

            @Override
            public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {

            }

            @Override
            public void onError(int id, Exception ex) {

            }
        }); 

Thanks for your help

2

There are 2 best solutions below

1
On

AWS SDK for Android 2.6.6 version contains a fix for fixing the progress update problem. Upgrading to this version should mitigate the problem.
In addition to that, you can tweak the notificationThreshold API of AmazonS3Client to change the interval of notifying the progress.

0
On

Add this service

    <service
        android:name="com.amazonaws.mobileconnectors.s3.transferutility.TransferService"
        android:enabled="true" />