Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS S3 Android SDK 2.11.0

My code looks like this:

final AmazonS3Client s3Client = new AmazonS3Client(new BasicAWSCredentials(s3AccessKeyId, s3SecretAccessKey));
final TransferUtility util = TransferUtility.
                    builder().s3Client(s3Client).context(context).build();

When upgrading from 'com.amazonaws:aws-android-sdk-s3:2.7.5' to 'com.amazonaws:aws-android-sdk-s3:2.11.0' I get the following error in my logcat, even though the upload is still working:

E/nsferNetworkLossHandler: TransferNetworkLossHandler is not created. Please call `TransferNetworkLossHandler.getInstance(Context)` to instantiate it before retrieving
E/UploadTask: TransferUtilityException: [com.amazonaws.mobileconnectors.s3.transferutility.TransferUtilityException: TransferNetworkLossHandler is not created. Please call `TransferNetworkLossHandler.getInstance(Context)` to instantiate it before retrieving]

When I change my code to include the TransferNetworkLossHandler:

TransferNetworkLossHandler.getInstance(context);
final AmazonS3Client s3Client = new AmazonS3Client(new BasicAWSCredentials(s3AccessKeyId, s3SecretAccessKey));
final TransferUtility util = TransferUtility.
                    builder().s3Client(s3Client).context(context).build();

without doing anything with it, I get no more error.

However, this is not how this error message is intended. It tells you to use the TransferNetworkLossHandler, but there is no documentation on how to use it.

I just want to display an error (like a Toast or an AlertDialog) to the user, if the upload did not work. So I don't need the handler at all. But I also don't want the error message to be written into my log everytime. Also I don't want to use this hackish solution of just grabbing an instance and not doing anything with it.

What is the proper use of this handler for my case?

like image 774
GC268DM Avatar asked Feb 01 '19 16:02

GC268DM


1 Answers

I read release_v2.11.0 and release_v2.11.1 (released 23hs ago) CHANGELOGS and they indicate the next:

  • Amazon S3 (Enhancements): Introduced TransferNetworkLossHandler, a utility that listens for network connectivity changes. TransferNetworkLossHandler pauses the on-going transfers when the network goes offline and resumes the transfers that were paused when the network comes back online.

  • Amazon S3 (Bug fixes): Fixed a bug in TransferUtility where the state is not set to 'WAITING_FOR_NETWORK' when network goes offline during execution of transfers.

In your case u can include TransferNetworkLossHandler to avoid the error message and use TransferListener to detect state change to 'WAITING_FOR_NETWORK', 'FAILED' or 'ERROR' and notice user:

TransferObserver transferObserver;
transferObserver = transferUtility.upload(UPLOAD_FOLDER+fileName, fileToUpload);
transferObserver.setTransferListener(new UploadListener());

/**
 * Upload listener to check transfer states and progress
 */
private class UploadListener implements TransferListener {

    @Override
    public void onStateChanged(int id, TransferState state) {
        Log.d(TAG, "onStateChanged: " + id + ", " + state.toString());

        // If upload error, failed or network disconnect
        if(state == TransferState.ERROR || state == TransferState.FAILED || state == TransferState.WAITING_FOR_NETWORK){
            // HERE end service and notice user !!!
        }
    }

    @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) {
        Log.e(TAG, ex);
    }
}
like image 161
Adrián Galfioni Avatar answered Nov 15 '22 04:11

Adrián Galfioni