Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

About Downloading #2

Open
SomnathS09 opened this issue Jul 17, 2021 · 0 comments
Open

About Downloading #2

SomnathS09 opened this issue Jul 17, 2021 · 0 comments

Comments

@SomnathS09
Copy link
Owner

SomnathS09 commented Jul 17, 2021

Tracking Download Progress:

1.How Transfer Utility works at Low Level ?:

Every call to download(key, localFile) returns a TransferObserver object to later identify this download task.
To get the progress of a transfer, we've to set TransferListener() implementing onStateChanged, onProgressChanged, and onErrorfor for every TransferObserver:
https://docs.amplify.aws/sdk/storage/transfer-utility/q/platform/android#track-transfer-progress

TransferObserver transferObserver = download(MY_BUCKET, OBJECT_KEY, MY_FILE);
transferObserver.setTransferListener(new TransferListener(){

    @Override
    public void onStateChanged(int id, TransferState state) {
        // do something
    }

    @Override
    public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
        int percentage = (int) (bytesCurrent/bytesTotal * 100);
        //Display percentage transferred to user
    }

    @Override
    public void onError(int id, Exception ex) {
        // do something
    }
});

TransferUtility also provides methods : resume( id ), cancel ( id ), pause( id )
The id (transfer ID) can be retrieved from the TransferObserver object that is returned from the upload or download function.

Value of TransferState gives the status of a download task, possible states are:
All Possible TransferState - Doc
All Possible TransferState - Java File

Encountered states on Amplify StorageHub Channel :

  1. COMPLETED
    This state represents a transfer that is completed
  2. IN_PROGRESS
    This state represents a transfer that is currently uploading or downloading data
  3. WAITING_FOR_NETWORK
    This state represents a transfer that is currently on hold, waiting for the network to become available

While using Amplify SDK, we don't have access to TransferUtility and can't set our own TransferListener for TransferObserver
Default TransferListener used by Amplify is :

private final class DownloadTransferListener implements TransferListener {
        @Override
        public void onStateChanged(int transferId, TransferState state) {
            Amplify.Hub.publish(HubChannel.STORAGE,
                    HubEvent.create(StorageChannelEventName.DOWNLOAD_STATE, state.name()));
            switch (state) {
                case COMPLETED:
                    onSuccess.accept(StorageDownloadFileResult.fromFile(file));
                    return;
                case FAILED:
                    // no-op;
                default:
                    // no-op;
            }
        }

        @Override
        public void onProgressChanged(int transferId, long bytesCurrent, long bytesTotal) {
            onProgress.accept(new StorageTransferProgress(bytesCurrent, bytesTotal));
        }

        @Override
        public void onError(int transferId, Exception exception) {
            Amplify.Hub.publish(HubChannel.STORAGE,
                    HubEvent.create(StorageChannelEventName.DOWNLOAD_ERROR, exception));
            onError.accept(new StorageException(
                    "Something went wrong with your AWS S3 Storage download file operation",
                    exception,
                    "See attached exception for more information and suggestions"
            ));
        }
    }

So, How can we get the update to states?
In Amplify's DownloadTransferListener above, on every state change it publishes current state to Amplify.Hub <HubChannel.STORAGE> and by subscribing to it we can get the flow of current states (but can't uniquely identify the download task)


More Details on internal working of TransferUtililty - archived Java Files:
TransferNetworkLossHandler.java gives status of network
TransferDBUtil.java maintains database of all transfers
DownloadTask.java uses TransferNetworkLossHandler.java & TransferDBUtil.java to automatically pause or resume download

More archived links of aws-sdk (not Amplify)
*Android Transfer Utility Sample (aws-sdk-android)
*Introducing the Transfer Utility for the AWS SDK for Android
*AWS SDK for Android Transfer Manager to Transfer Utility Migration Guide
*Using the S3 Transfer Utility

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant