Image Upload Amazon S3 Android Sdk 2.0
I want to upload an image to an amazon s3 bucket in android. I don't get any errors but it's just not working can anybody help me? I can't find any good examples or questions about
Solution 1:
Try this one. Since i had the same issue that you faced.
I have fixed by using the below code.
ObjectMetadatametadata=newObjectMetadata();
metadata.setContentEncoding("UTF-8");
size = inputStream.available();
metadata.setContentLength(size);
TransferManagertransferManager=newTransferManager(credentialsProvider);
Uploadupload= transferManager.upload(bucket_name, key, images3, metadata);
upload.waitForCompletion();
Solution 2:
Very simple way to download image and upload image in s3 amazon. you make a simple class use this WebserviceAmazon
publicclassWebserviceAmazonextendsAsyncTask<Void, Void, Void> {
private String mParams;
privateStringmResult="x";
WebServiceInterface<String, String> mInterface;
privateint mRequestType;
private String UserId;
private Context mContext;
publicWebserviceAmazon(Context context,String imagePath,String AppId,int type) {
this.mContext = context;
this.mParams = imagePath;
this.mRequestType = type;
this.UserId = AppId;
}
publicvoidresult(WebServiceInterface<String, String> myInterface) {
this.mInterface = myInterface;
}
@Overrideprotected Void doInBackground(Void... params) {
StringACCESS_KEY="abc..";
StringSECRET_KEY="klm...";
try {
if (mRequestType == 1) { // POSTAmazonS3Clients3Client=newAmazonS3Client(newBasicAWSCredentials(ACCESS_KEY, SECRET_KEY));
PutObjectRequestrequest=newPutObjectRequest("bucketName", "imageName", newFile(mParams));
s3Client.putObject(request);
mResult = "success";
} if (mRequestType == 2) { // For get image dataAmazonS3Clients3Client=newAmazonS3Client(newBasicAWSCredentials(ACCESS_KEY, SECRET_KEY));
S3Objectobject= s3Client.getObject(newGetObjectRequest("bucketName", mParams));
S3ObjectInputStreamobjectContent= object.getObjectContent();
byte[] byteArray = IOUtils.toByteArray(objectContent);
Bitmapbitmap= BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
mResult = "success";
}
} catch (Exception e) {
mResult = e.toString();
e.printStackTrace();
}
returnnull;
}
@OverrideprotectedvoidonPreExecute() {
// TODO Auto-generated method stubsuper.onPreExecute();
}
@OverrideprotectedvoidonPostExecute(Void result) {
// TODO Auto-generated method stubsuper.onPostExecute(result);
mInterface.success(this.mResult);
}
publicinterfaceWebServiceInterface<E, R> {
publicvoidsuccess(E reslut);
publicvoiderror(R Error);
}
}
call this webservice any where in project
WebserviceAmazon amazon = newWebserviceAmazon(getActivity(), imageName, "", 2);
amazon.result(newWebserviceAmazon.WebServiceInterface<String, String>() {
@Overridepublicvoidsuccess(String reslut) {
}
@Overridepublicvoiderror(StringError) {
}
});
return totalPoints;
}
Solution 3:
You should have to do Two Steps;
- Create the PutObjectRequestObject Like this:
PutObjectRequestpor=newPutObjectRequest(
BUCKET_NAME, Key,
stored);
por.setCannedAcl(CannedAccessControlList.PublicReadWrite);
s3Client.putObject(por);
- Change the Policy Of Amazon s3 Server Console By Changing the bucket (Folder) Public.
I was already done that and it's working.
Post a Comment for "Image Upload Amazon S3 Android Sdk 2.0"