uploading an image to amazon s3 from android app

  • Replies:5
Pradeepkumar Reddy
  • Forum posts: 11

Sep 12, 2016, 6:33:33 AM via Website

// Initialize the Amazon Cognito credentials provider
CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
getApplicationContext(),
"us-west-2:45af92a8-2a30-4540-bbd6-a7ec68b9fcd4", // Identity Pool ID
Regions.US_WEST_2 // Region
);

    // Create an S3 client
    s3 = new AmazonS3Client(credentialsProvider);
    TransferUtility transferUtility = new TransferUtility(s3, getBaseContext());
    TransferObserver observer = transferUtility.upload(
                "krayon-event-photos",      /* The bucket to upload to */
                "eagleton",     /* The key for the uploaded object */
                new File(imagePath)        /* The file where the data to upload exists */
        );
        observer.setTransferListener(new TransferListener(){

            @Override
            public void onStateChanged(int id, TransferState state) {
                // do something
                Log.d("log", "state changed. id = "+id+"\tstate = "+state);
            }

            @Override
            public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
                int percentage = (int) (bytesCurrent/bytesTotal * 100);

                //Display percentage transfered to user
                publishProgress(percentage);
                Log.d("log", "onProgressChanged = "+percentage);
            }

            @Override
            public void onError(int id, Exception ex) {
                // do something
                Log.d("log", "error in uploading. id = "+id+"\nException = "+ex);
            }
        });

I'm getting the following error:-

Exception = com.amazonaws.AmazonClientException: Unable to execute HTTP request: Write error: ssl=0xb7461c28: I/O error during system call, Connection reset by peer

Reply
Ashish Tripathi
  • Forum posts: 211

Sep 26, 2016, 1:19:36 PM via Website

Seems the host has some issue. check the host .

Reply
Pradeepkumar Reddy
  • Forum posts: 11

Sep 26, 2016, 2:33:45 PM via Website

That problem got solved. it was because, it did not set the region for bucket in the code. I have created a bucket in some region on the server and i forgot to set the same region on client. so that was the reason for that exception.

i need to do something like this s3.setRegion(Regions.AP_SOUTH_1);

Reply
James Watson
  • Forum posts: 1,584

Sep 27, 2016, 3:24:00 AM via Website

Pradeepkumar Reddy

That problem got solved. it was because, it did not set the region for bucket in the code. I have created a bucket in some region on the server and i forgot to set the same region on client. so that was the reason for that exception.

i need to do something like this s3.setRegion(Regions.AP_SOUTH_1);

Do you mean the region, Regions.US_WEST_2, in your code above?

Download size < 0.15 MB. But also accurate enough, ad-free & free.
The minimalist app available on Play Store: https://goo.gl/ws42fN
Blog: https://okblackcafe.blogspot.com Your 5-star is appreciated.

Reply
Pradeepkumar Reddy
  • Forum posts: 11

Sep 27, 2016, 10:31:32 AM via Website

yes i had to do s3.setRegion(Regions.AP_SOUTH_1). In my case i had created the bucket in "AP_SOUTH_1" region on server.
So on the client for the s3 object i need to set the same region.

Reply
James Watson
  • Forum posts: 1,584

Sep 28, 2016, 3:30:01 AM via Website

Thanks for sharing your experience.

Download size < 0.15 MB. But also accurate enough, ad-free & free.
The minimalist app available on Play Store: https://goo.gl/ws42fN
Blog: https://okblackcafe.blogspot.com Your 5-star is appreciated.

Reply