Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload file to Amazon S3 from Android slow

I have implemented the file upload to Amazon S3 following the Amazon guide and I have noticed that it is too slow. It takes around 10 sec to upload a simple png file around 20kb.

Initially I thought that the problem was related with threads and I have implemented an AsyncTask to upload the image, but the problem is still there. Following is the code employed to upload the image.

private class UploadFileTask extends AsyncTask<String, Integer, String> {
    String remotePath;
    String remoteFileName;
    File file;
    Context context;
    S3UploadInterface listener;


    public UploadFileTask(Context context,String remotePath,String remoteFileName, File file, S3UploadInterface listener){
        this.context=context;
        this.remotePath=remotePath;
        this.remoteFileName=remoteFileName;
        this.file=file;
        this.listener=listener;
    }

    protected String doInBackground(String... params) {
        credentialsProvider = new CognitoCachingCredentialsProvider(context,
                "MY_PRIVATE_CREDENTIAL",
                Regions.US_EAST_1);
        TransferManager transferManager = new TransferManager(credentialsProvider);
        Upload upload = transferManager.upload(remotePath, remoteFileName, file);
        TransferProgress transferred = upload.getProgress();
        while (!upload.isDone()) {
            try {
                publishProgress((int) transferred.getPercentTransferred());
            } catch (Exception e) {
                listener.uploadFailed(e);
            }
        }
        return "uploaded";
    }

    protected void onProgressUpdate(Integer... progress) {
        if (listener!=null)
            listener.currentUploadProgress(progress[0]);
    }

    protected void onPostExecute(String result) {
        if (listener!=null)
            listener.uploadCompleted();
    }
}

Any idea to solve this problem? Thanks :)

like image 588
rdiaz82 Avatar asked Jul 03 '15 08:07

rdiaz82


People also ask

How do I increase my upload speed on my S3?

Consider the following options for improving the performance of uploads and optimizing multipart uploads: If you're using the AWS CLI, customize the upload configurations. Enable Amazon S3 Transfer Acceleration.

How do I upload 100gb files to my Galaxy S3?

Instead of using the Amazon S3 console, try uploading the file using the AWS Command Line Interface (AWS CLI) or an AWS SDK. Note: If you use the Amazon S3 console, the maximum file size for uploads is 160 GB. To upload a file that is larger than 160 GB, use the AWS CLI, AWS SDK, or Amazon S3 REST API.

How fast can you upload to S3?

Traffic between Amazon EC2 and Amazon S3 can leverage up to 100 Gbps of bandwidth to VPC endpoints and public IPs in the same Region.


1 Answers

This is going to hammer your CPU

    while (!upload.isDone()) {
        try {
            publishProgress((int) transferred.getPercentTransferred());
        } catch (Exception e) {
            listener.uploadFailed(e);
        }
    }

Try adding Thread.sleep to give other threads time to run.

like image 159
frank Avatar answered Oct 21 '22 05:10

frank