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

File upload fails with no error on 2.2.11 #93

Closed
jourda-c opened this issue Feb 2, 2016 · 6 comments
Closed

File upload fails with no error on 2.2.11 #93

jourda-c opened this issue Feb 2, 2016 · 6 comments
Labels
question General question

Comments

@jourda-c
Copy link

jourda-c commented Feb 2, 2016

Hi,

It seems that there is a random bug that makes the file upload fail for no reason.

Here is the piece of code i have :

TransferUtility transfer = new TransferUtility(((MyApplication)getActivity().getApplication()).amazons3, getContext());

TransferObserver obs = transfer.upload("bucker", String.format("video/%s", videoName), f);
obs.setTransferListener(new TransferListener() {
            @Override
            public void onStateChanged(int id, TransferState state) {
                Log.d("dev", state.name());
           }

            @Override
            public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
                Log.d("dev", bytesCurrent + "/" + bytesTotal + "-" + ((float)bytesCurrent / (float)bytesTotal) * 100);
            }

            @Override
            public void onError(int id, Exception ex) {
                Log.d("Amazon", "Error on amazon service ! " + ex);
            }
});

This is supposed to upload a video file to amazon servers.
It worked until v2.2.11.
For no reason, it happens that onProgressChanged() is called only once or twice, then nothing is happening. Same for onStateChanged(). It is called only once (when the upload starts).
onError() is never called. The upload process just stop.

Here is the log

02-02 13:27:31.741 5459-5459/com.app D/TransferSerivce: Starting Transfer Service
02-02 13:27:31.741 5459-7229/com.app D/TransferSerivce: Loading transfers from database
02-02 13:27:31.751 5459-7229/com.app D/TransferSerivce: 1 transfers are loaded from database
02-02 13:27:31.751 5459-5459/com.app D/TransferSerivce: Network connected: true
02-02 13:27:31.751 5459-7229/com.app W/TransferSerivce: Transfer has already been added: 9
02-02 13:27:31.761 5459-5459/com.app D/dev: IN_PROGRESS
02-02 13:27:31.801 5459-5459/com.app D/dev: 0/1689585-0.0
02-02 13:27:31.831 5459-7230/com.app D/com.amazonaws.request: Sending Request: PUT https://mybycketname.s3.eu-central-1.amazonaws.com /video/filename.mp4 Headers: [some useless headers]
02-02 13:27:31.831 5459-7230/com.app I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
02-02 13:27:31.871 5459-7230/com.app I/System.out: KnoxVpnUidStorageknoxVpnSupported API value returned is false
02-02 13:27:32.111 5459-5470/com.app V/MediaPlayer-JNI: native_finalize
02-02 13:27:32.111 5459-5470/com.app V/MediaPlayer-JNI: release
02-02 13:27:32.831 5459-7230/com.app D/com.amazonaws.request: Received successful response: 200, AWS Request ID: FDEA11B6AFAC6BC5
02-02 13:28:31.811 5459-7229/com.app D/TransferSerivce: Stop self

We can see the transfer starts (IN_PROGRESS state), a piece of data is sent then nothing is happening. We receive a response from Amazon server then nothing, the service stops itself.
This piece of code works perfectly on v2.2.10, transfer works everytime so it's not a internet issue or an amazon servers issue

@fosterzhang fosterzhang added the bug Something isn't working label Feb 2, 2016
@fosterzhang
Copy link
Contributor

Thanks for reporting. I've identified the bug. The listener is garbage collected before the transfer finishes. I am working on the fix. A temporary workaround at the moment is to make the listener live longer, e.g. make it a class member of the containing activity/fragment, or have the container implements the listener. Stay tuned for the fix.

@fosterzhang fosterzhang added question General question and removed bug Something isn't working labels Feb 3, 2016
@fosterzhang
Copy link
Contributor

Actually this is a design change that isn't disclosed clearly in the change log. Previously, a listener is attached as a content observer. Like all other content observers, they must be unregistered when no longer needed, or else it will cause memory leak.

The following is a very typical usage of TransferUtility:

public class MainActivity extends Activity {
    // set up in onCreate()
    private TransferUtility transferUtility;
    private AmazonS3 s3;

    // upload a file
    private void upload() {
        TransferObserver transfer = transferUtility.upload(bucket, key, file);
        transfer.setTransferListener(new TransferListener() {
            // implement listener
        });
    }
}

Here the transfer listener is an anonymous class defined inside an activity. Thus it has an implicit reference of the activity. If it is not unregistered via TransferObserver.cleanTransferListener(), the activity won't be garbage collected.

We notice it's inconvenient to clear the listener. One has to save a reference of the transfer and then clear it inside TransferObserver.onStatusChange(int, long, long) or in Activity.onPause(). To simplify the usage and solve the potential memory leak issue, we move away from content observer and only keep the weak reference of the listener. The trade off is that one has to manage the life cycle of the listener carefully. This can be done by making the listener as part of an activity. Here is an example:

public class MainActivity extends Activity {
    // set up in onCreate()
    private TransferUtility transferUtility;
    private AmazonS3 s3;
    private TransferListener listener;

    @Override
    protected void onCreated() {
        listener = new TransferListener() {
            @Override
            public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
                // update progress listener
            }
        }
    }
    // upload a file
    private void upload() {
        TransferObserver transfer = transferUtility.upload(bucket, key, file);
        transfer.setTransferListener(listener);
    }
}

@jitendraramoliya
Copy link

I need to pass parameter in Transferlisterner for updating progress through progress bar So I have to create listener when need to upload video. so according your suggestion create listener in OnCreate method not work for me. Could you provide me some reliable solution for this issue?

@hsyadav733
Copy link

I am using an Intent Service to upload in background and i need to pass some arguments on changing the status to COMPLETE, But facing the above said issue with sdk version 2.2.12. Please suggest a solution.

@fosterzhang
Copy link
Contributor

This is improved in v2.2.13 release. Please update the SDK at https://aws.amazon.com/mobile/sdk/. Check out what is changed in the change log.

@fosterzhang
Copy link
Contributor

Closing this as it's improved in v2.2.13. Feel free to reopen it.

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

No branches or pull requests

4 participants