I need to upload a bitmap to Amazon S3. I have never used S3, and the docs are proving less than helpful as I can\'t see anything to cover this specific requirement. Unfortunate
you can use the below mentioned class to Upload data to amazon s3 buckets.
public class UploadAmazonS3{
private CognitoCachingCredentialsProvider credentialsProvider = null;
private AmazonS3Client s3Client = null;
private TransferUtility transferUtility = null;
private static UploadAmazonS3 uploadAmazonS3;
/**
* Creating single tone object by defining private.
*
* At the time of creating
*
*/
private UploadAmazonS3(Context context, String canito_pool_id)
{
/**
* Creating the object of the getCredentialProvider object. */
credentialsProvider=getCredentialProvider(context,canito_pool_id);
/**
* Creating the object of the s3Client */
s3Client=getS3Client(context,credentialsProvider);
/**
* Creating the object of the TransferUtility of the Amazone.*/
transferUtility=getTransferUtility(context,s3Client);
}
public static UploadAmazonS3 getInstance(Context context, String canito_pool_id)
{
if(uploadAmazonS3 ==null)
{
uploadAmazonS3 =new UploadAmazonS3(context,canito_pool_id);
return uploadAmazonS3;
}else
{
return uploadAmazonS3;
}
}
/**
* Upload_data
*
* Method is use to upload data in the amazone server.
*
*
*/
public void uploadData(final String bukkate_name, final File file, final Upload_CallBack callBack)
{
Utility.printLog("in amazon upload class uploadData "+file.getName());
if(transferUtility!=null&&file!=null)
{
TransferObserver observer=transferUtility.upload(bukkate_name,file.getName(),file);
observer.setTransferListener(new TransferListener()
{
@Override
public void onStateChanged(int id, TransferState state)
{
if(state.equals(TransferState.COMPLETED))
{
callBack.sucess(com.tarha_taxi.R.string.AMAZON_END_POINT_LINK+bukkate_name+"/"+file.getName());
}
}
@Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal)
{
}
@Override
public void onError(int id, Exception ex)
{
callBack.error(id+":"+ex.toString());
}
});
}else
{
callBack.error("Amamzones3 is not intialize or File is empty !");
}
}
/**
* This method is used to get the CredentialProvider and we provide only context as a parameter.
* @param context Here, we are getting the context from calling Activity.*/
private CognitoCachingCredentialsProvider getCredentialProvider(Context context,String pool_id)
{
if (credentialsProvider == null)
{
credentialsProvider = new CognitoCachingCredentialsProvider(
context.getApplicationContext(),
pool_id, // Identity Pool ID
AMAZON_REGION // Region
);
}
return credentialsProvider;
}
/**
* This method is used to get the AmazonS3 Client
* and we provide only context as a parameter.
* and from here we are calling getCredentialProvider() function.
* @param context Here, we are getting the context from calling Activity.*/
private AmazonS3Client getS3Client(Context context,CognitoCachingCredentialsProvider cognitoCachingCredentialsProvider)
{
if (s3Client == null)
{
s3Client = new AmazonS3Client(cognitoCachingCredentialsProvider);
s3Client.setRegion(Region.getRegion(AMAZON_REGION));
s3Client.setEndpoint(context.getString(com.tarha_taxi.R.string.AMAZON_END_POINT_LINK));
}
return s3Client;
}
/**
* This method is used to get the Transfer Utility
* and we provide only context as a parameter.
* and from here we are, calling getS3Client() function.
* @param context Here, we are getting the context from calling Activity.*/
private TransferUtility getTransferUtility(Context context,AmazonS3Client amazonS3Client)
{
if (transferUtility == null)
{
transferUtility = new TransferUtility(amazonS3Client,context.getApplicationContext());
}
return transferUtility;
}
/**
* Interface for the sucess callback fro the Amazon uploading .
* */
public interface Upload_CallBack
{
/**
*Method for sucess .
* @param sucess it is true on sucess and false for falure.*/
void sucess(String sucess);
/**
* Method for falure.
* @param errormsg contains the error message.*/
void error(String errormsg);
}
}
use the below method to access the above calss :
private void uploadToAmazon() {
dialogL.show();
UploadAmazonS3 amazonS3 = UploadAmazonS3.getInstance(getActivity(), getString(R.string.AMAZON_POOL_ID));
amazonS3.uploadData(getString(R.string.BUCKET_NAME), Utility.renameFile(VariableConstants.TEMP_PHOTO_FILE_NAME, phone.getText().toString().substring(1) + ".jpg"), new UploadAmazonS3.Upload_CallBack() {
@Override
public void sucess(String sucess) {
if (Utility.isNetworkAvailable(getActivity())) {
dialogL.dismiss();
/**
* to set the image into image view and
* add the write the image in the file
*/
activity.user_image.setTag(setTarget(progress_bar));
Picasso.with(getActivity()).load(getString(R.string.AMAZON_IMAGE_LINK) + phone.getText().toString().substring(1) + ".jpg").
networkPolicy(NetworkPolicy.NO_CACHE).memoryPolicy(MemoryPolicy.NO_CACHE).into((Target) activity.user_image.getTag());
Utility.printLog("amazon upload success ");
new BackgroundForUpdateProfile().execute();
} else {
dialogL.dismiss();
Utility.showToast(getActivity(), getResources().getString(R.string.network_connection_fail));
}
}
@Override
public void error(String errormsg) {
dialogL.dismiss();
Utility.showToast(getActivity(), getResources().getString(R.string.network_connection_fail));
}
});
}