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.