AWS S3 TransferManager
Easiest way to transfer the data/object/file between mobile application and Amazon S3, S3 Transfer Utility for AWS SDK for Android.
What is difference between the Transfer Utility and the Transfer Manager?
Probably when we try to transfer file or object, need to manage all the complicate steps such like starting from managing multiple files, resume, pause and cancel and many more. The Transfer Utility is designed to manage all the mentioned steps and provide the simple and effective way to use.
The Transfer Utility is new client, with many of the same features as Transfer Manager. Some of the enhancements features are:
Uploading and downloading
The Transfer Manager has APIs that support the use of I/O streams, with these APIs, pausing, Resuming was not possible. With using Transfer Utility Manager there is one file based API that support the resume and pause on top of automatic pause or resume mentioned above. The Transfer Utility majorly provided two operations download & upload.
TransferObserver observer = transferUtility.upload(
MY_BUCKET, // The S3 bucket to upload to (String bucket Name)
OBJECT_KEY, // The key for the uploaded object (String)
FILE_TO_UPLOAD // The location of the file to be uploaded (File)
);
For example:
File file = new File(filePath);
TransferObserver observer = transferUtility.upload(Constants.BUCKET_NAME, file.getName(),file);
Downloading is equally simple:
TransferObserver observer = transferUtility.download(
MY_BUCKET, /* The bucket to download from */
OBJECT_KEY, // The key for the object to download
FILE_TO_DOWNLOAD // The file for download the object
);
Tracking Transfer Progress:
The Transfer Utility makes tracking the transfer make mobile friendly. The basic way to track the transfers is thorough instances of the TransferObserver
class. TransferObserver
instance are returned from download()
and upload()
methods. They automatically persist into local database. We can queried for base on id
,type (upload, download, or any)
or state (Such as paused)
from anywhere within the app see blow.
public TransferObserver getTransferById(int id)
public List<TransferObserver> getTransfersWithType(TransferType type)
public List<TransferObserver> getTransfersWithTypeAndState(TransferType type, TransferState state)
The TransferObserver gives access to the state, total bytes transferred thus far, total bytes to transfer and a unique id you can use to keep distinct transfers. You can also attached the listener TransferListener
,which will update onStateChanged
, onProgressChanged
and an onError
occurs.
transferObserver.setTransferListener(new TransferListener() {
@Override
public void onStateChanged(int id, TransferState state){
//Do something on state change
}
@Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
//Do something on progress change.
}
@Override
public void onError(int id, Exception ex) {
//Do something on error
}
});
Pausing Transfers:
With Transfer Manager, there is no guarantee a transfer can paused and there are multiple way to paused. Also paused require developer to serialize the metadata of the transfer to persist to storage.
The Transfer Utility handlers persists all the metadata of the transfer for you. Even if you application killed, crashed or loss connectivity transfers automatically paused. Even you can manually paused the transfer by id with pause(transferd id)
paused all downloads
pauseAllWithType(TransferType.Download)
and pause all transferred by pauseAllWithType(TransferType.ANY)
.
Resuming Transfers:
The Transfer Utility can paused the transfer in many scenarios. In the case of transfer was paused due to of the loss connectivity, the Transfer will automatically Resume when the network connection available again. In case of manually paused the transfer, application killed, crashed, it can be resumed with resume(TransferId)
method.
In summary, Transfer Utility is completely replaced with the Transfer Manager. Going forward AWS will be deprecated the Transfer Manager.
Additional reference related to this topic you can find in blow: