AWS S3 TransferObserver Doesn't Give Any Errors When Internet is Not Available


I'm using this library: com.amazonaws:aws-android-sdk-s3:2.81.1

And the setup for TransferObserver:

val observer = transferUtility.upload(
    fileName,
    context.contentResolver.openInputStream(uri)
)

observer.setTransferListener(object : TransferListener {
    override fun onStateChanged(id: Int, state: TransferState?) {
        Log.d("xxx", "state: $state")    //Problem 1
    }

    override fun onProgressChanged(id: Int, bytesCurrent: Long, bytesTotal: Long) {}

    override fun onError(id: Int, e: Exception) {
        Log.e("xxx", "Error: ${e.message}")    //Problem 2
    }
})

Problem 1: When this runs without internet, the state will stay in WAITING_FOR_NETWORK, I was expecting it to change to FAILED but it never happens.

Problem 2: When this runs without internet, onError() is not called.

Question: Is this normal/intended?

0
Jan 30 at 4:37 AM
User AvatarSam Chen
#android#amazon-web-services#amazon-s3

Accepted Answer

> Problem 1: When this runs without internet, the state will stay in WAITING_FOR_NETWORK, I was expecting it to change to FAILED but it never happens.

The library automatically resumes the task when network connectivity is recovered.

> Problem 2: When this runs without internet, onError() is not called.

The implementation keeps the transfer state in WAITING_FOR_NETWORK until network connectivity is restored.

The listener onError callback is not called for loss of network connectivity. It's implemented this way.

You may utilize a listener-based timer to terminate the upload task if connectivity is not restored within a specific window. e.g.

val observer = transferUtility.upload(bucketName, key, file) var waitingJob: Job? = null override fun onStateChanged(id: Int, state: TransferState?) { if (state == TransferState.WAITING_FOR_NETWORK && waitingJob == null) { waitingJob = lifecycleScope.launch { delay(30_000L) if (observer.state == TransferState.WAITING_FOR_NETWORK) { transferUtility.cancel(id) } } } if (state == TransferState.IN_PROGRESS) { waitingJob?.cancel() waitingJob = null } }
User AvatarOluwafemi Sule
Jan 30 at 9:31 AM
3