axlan
axlan

Reputation: 963

Amazon s3 upload using transfer manager seems to never stop waiting

I'm trying to create a service to upload a list of files in the background. When I periodically check the MultipleFileUpload object, it always is in the TransferState WAITING, and the bytes uploaded and total bytes to upload are always 0. Is there some problem with running the TransferManager in a service, or periodically checking the MultipleFileUpload this way?

I've tested on a phone and emulator. I previously was able to upload successfully with the same AmazonS3Client when I was using PutObjectRequest instead of the TransferManager.

Here is the code so far:

// OnCreate
    @Override
    public void onCreate() {

        Log.v(TAG, "Creating Upload Service");

        ClientConfiguration clientConfig=new ClientConfiguration();
        clientConfig.setConnectionTimeout(10*1000);

        AmazonS3Client s3Client = new AmazonS3Client(
                new BasicAWSCredentials(ACCESS_KEY_ID,
                        SECRET_KEY),clientConfig);

        s3Client.setEndpoint(AMAZON_ENDPOINT);

        transferManager= new TransferManager(s3Client);

        seekIntent = new Intent(BROADCAST_ACTION);
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        // Insert notification start
        initNotification();

        String[] fileNames=intent.getExtras().getStringArray(MainActivity.UPLOAD_FILE_NAMES);

        String path=intent.getExtras().getString(MainActivity.UPLOAD_FILE_PATH);

        ArrayList<File> files=new ArrayList<File>();

        for (String fileName : fileNames) {
            files.add(new File(fileName));
        }

        upload= transferManager.uploadFileList(RECORDING_BUCKET, null,new File(path),files);
        seekIntent.putExtra(UPLOAD_BYTES_TOTAL_KEY, upload.getProgress().getTotalBytesToTransfer());

        transferManager.

        setupHandler();

        return START_STICKY;
    }

    // ---Send seekbar info to activity----
    private void setupHandler() {
        handler.removeCallbacks(sendUpdatesToUI);
        handler.postDelayed(sendUpdatesToUI, 1000); // 1 second
    }

    private Runnable sendUpdatesToUI = new Runnable() {
        public void run() {
            // // Log.d(TAG, "entered sendUpdatesToUI");

            seekIntent.putExtra(UPLOAD_BYTES_TRANSFERED_KEY, upload.getProgress().getBytesTransfered());

            if(upload.isDone())
            {
                int retCode=(upload.getState()==TransferState.Completed)?0:1;

                seekIntent.putExtra(UPLOAD_COMPLETION_CODE_KEY, retCode);
            }
            else
            {
                handler.postDelayed(this, 1000); 
            }

            sendBroadcast(seekIntent);
        }
    };

Upvotes: 2

Views: 1184

Answers (2)

heights1976
heights1976

Reputation: 500

Another problem that I discovered is that if using the new TransferUtility, you must declare the Service

<service
android:name="com.amazonaws.mobileconnectors.s3.transferutility.TransferService" 
android:enabled="true" />

in your AndroidManifest.xml. This is highlighted in some but not all of the documentation and is easy to miss.

Upvotes: 0

axlan
axlan

Reputation: 963

Turns out I had made a mistake in my understanding of the parameters for the uploadFileList method. The files I was passing in were relative to the directory file. It turns out that the files in the file list still need their full paths and the directory argument just figures out how they should be shown in the bucket. The weird thing is this did not cause an error, it just causes the behavior I mentioned in the question.

Upvotes: 3

Related Questions