Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 this.

I assign a image to 'File images3'

images3 = new File(uri.getPath());

public void addEventToDB(){

        Thread thread = new Thread()
        {
            @Override
            public void run() {
                try {
                    CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
                            getActivity().getApplicationContext(), // get the context for the current activity
                            "...",
                            "us-east-1:...",
                            "arn:aws:iam::...:role/Cognito_WitpaAuth_DefaultRole",
                            "arn:aws:iam::...:role/Cognito_WitpaAuth_DefaultRole",
                            Regions.US_EAST_1
                    );

                    String bucket_name = "witpa";
                    String key = "images.jpeg";

                    TransferManager transferManager = new TransferManager(credentialsProvider);
                    transferManager.upload(bucket_name, key, images3);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        thread.start();

    }

I created my bucket, in the permissions I set that everyone can write and read.

In amazon cognito I just left everything as default.

Anybody knows how I can get this to work?

like image 435
Laurenswuyts Avatar asked Sep 29 '22 08:09

Laurenswuyts


2 Answers

Try this one. Since i had the same issue that you faced.

I have fixed by using the below code.

ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentEncoding("UTF-8");
size = inputStream.available();
metadata.setContentLength(size);
TransferManager transferManager = new TransferManager(credentialsProvider);
Upload upload = transferManager.upload(bucket_name, key, images3, metadata);
upload.waitForCompletion();
like image 172
ashokramcse Avatar answered Oct 07 '22 20:10

ashokramcse


Very simple way to download image and upload image in s3 amazon. you make a simple class use this WebserviceAmazon

public class WebserviceAmazon extends AsyncTask<Void, Void, Void> {
private String mParams;
private String mResult = "x";
WebServiceInterface<String, String> mInterface;
private int mRequestType;
private  String UserId;
private Context mContext;


public WebserviceAmazon(Context context,String imagePath,String AppId,int type) {
    this.mContext = context;
    this.mParams = imagePath;
    this.mRequestType = type;
    this.UserId = AppId;
}

public void result(WebServiceInterface<String, String> myInterface) {
    this.mInterface = myInterface;
}

@Override
protected Void doInBackground(Void... params) {
    String ACCESS_KEY ="abc..";
    String SECRET_KEY = "klm...";

    try {
        if (mRequestType == 1) { // POST
            AmazonS3Client s3Client = new AmazonS3Client(new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY));
            PutObjectRequest request = new PutObjectRequest("bucketName", "imageName", new File(mParams));
            s3Client.putObject(request);

            mResult = "success";
        } if (mRequestType == 2) { // For get image data
            AmazonS3Client s3Client = new AmazonS3Client(new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY));
            S3Object object = s3Client.getObject(new GetObjectRequest("bucketName", mParams));
            S3ObjectInputStream objectContent = object.getObjectContent();
            byte[] byteArray = IOUtils.toByteArray(objectContent);

           Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);



            mResult = "success";
        }

    } catch (Exception e) {
        mResult = e.toString();
        e.printStackTrace();
    }
    return null;
}

@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub
    super.onPreExecute();
}

@Override
protected void onPostExecute(Void result) {
    // TODO Auto-generated method stub
  super.onPostExecute(result);
    mInterface.success(this.mResult);

}

public interface WebServiceInterface<E, R> {
    public void success(E reslut);

    public void error(R Error);
}

}

call this webservice any where in project

    WebserviceAmazon amazon = new WebserviceAmazon(getActivity(), imageName, "", 2);
    amazon.result(new WebserviceAmazon.WebServiceInterface<String, String>() {
        @Override
        public void success(String reslut) {

        }

        @Override
        public void error(String Error) {

        }
    });

    return totalPoints;
}
like image 35
Binesh Kumar Avatar answered Oct 07 '22 19:10

Binesh Kumar