0

What actually i do in my application is capture the image, Save it to disk and then upload it to s3.

My code to upload is

public void credentialsProvider(){

        // Initialize the Amazon Cognito credentials provider
        CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
                getApplicationContext(),
                "ap-south-1:---------------", // Identity Pool ID
                Regions.AP_SOUTH_1  // Region
        );

        setAmazonS3Client(credentialsProvider);
    }

    /**
     *  Create a AmazonS3Client constructor and pass the credentialsProvider.
     * @param credentialsProvider
     */
    public void setAmazonS3Client(CognitoCachingCredentialsProvider credentialsProvider){

        // Create an S3 client
        s3 = new AmazonS3Client(credentialsProvider);

        // Set the region of your S3 bucket
        s3.setRegion(Region.getRegion(Regions.AP_SOUTH_1));


    }

    public void setTransferUtility(){

        transferUtility = new TransferUtility(s3, getApplicationContext());
    }

    /**
     * This method is used to upload the file to S3 by using TransferUtility class
     *
     */
    public void fileUpload(){

        TransferObserver transferObserver = transferUtility.upload(
                "train-faces",     /* The bucket to upload to */
                time1+date1+"_"+"1" + "_pic.jpg",    /* The key for the uploaded object */
                fileToUpload       /* The file where the data to upload exists */
        );

        transferObserverListener(transferObserver);
    }



    /**
     * This is listener method of the TransferObserver
     * Within this listener method, we get status of uploading and downloading file,
     * to display percentage of the part of file to be uploaded or downloaded to S3
     * It displays an error, when there is a problem in  uploading or downloading file to or from S3.
     * @param transferObserver
     */

    public void transferObserverListener(TransferObserver transferObserver){

        transferObserver.setTransferListener(new TransferListener(){

            @Override
            public void onStateChanged(int id, TransferState state) {
                if (state.COMPLETED.equals(transferObserver.getState())) {
                    Toast.makeText(CameraService.this, "File Upload Complete", Toast.LENGTH_SHORT).show();
                    fileToUpload.delete();
                }
                Log.e(TAG, "statechange"+state+"");
            }

            @Override
            public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
                int percentage = (int) (bytesCurrent/bytesTotal * 100);
                Log.e(TAG,"percentage"+percentage +"");
                if (percentage==100){
                    //fileToUpload.delete();
                }

            }

            @Override
            public void onError(int id, Exception ex) {
                Log.e(TAG,"error"+"error");

            }

        });
    }

When the image is saved, i just call fileUpload() sometimes images are getting uploaded successfully some times i get: Failed to upload due to unable to execute Http request 7 unable to resolve host no address associated with hostname

I want to make it more reliable and avoid this few failures of upload in my application how to achieve this.

2 Answers2

0

I haven't encountered the 7 unable to resolve host no address associated with hostname error but this generally occurs when there is a network issue, as mentioned by Hemant.

Regarding the read time out error Failed to upload: 196 due to Unable to execute HTTP request: Read timed out, this may or may not be a network issue. Of course, a bad network can definitely cause this but there were known issues with TransferUtility which showed some uploads as failed even when there were no network issues.

If it is a genuine network issue, increasing the socket timeout as suggested may fix/reduce the errors.

ClientConfiguration cc = new ClientConfiguration(); 
cc.setSocketTimeout(120000); AmazonS3 s3 = new AmazonS3Client(credentialsProvider, cc);

Set the timeout to some high value as per your preference. If this does not fix or reduce the errors & you are sure there are no network issues then this may be a problem with the TransferUtility itself. Users mentioned that uploading large files directly results in this error and a workaround used by one person was to upload a 1 Kb dummy file prior to any upload but this is obviously not a solution. See this post for details. I personally have faced this error only with bad network so I can't verify if the workarounds are effective or not and whether the issue has been fixed in a later release of SDK.

agent420
  • 3,291
  • 20
  • 27
0

I was having the same issue when the internet is not stable, but after making configuration changes it works.

  val configuration = ClientConfiguration()
        configuration.maxErrorRetry = 10
        configuration.maxConnections = 100
        configuration.connectionTimeout = 0
        configuration.socketTimeout = 0
  val s3Client = AmazonS3Client(credentials,configuration)
PPV
  • 107
  • 1
  • 1
  • 8