Mohammad Rajob
Mohammad Rajob

Reputation: 743

android aws error code bucket already owned by you

I have an android application, which function is to upload image to AWS(Amazon Web Service) S3. When I first time run this app image upload successfully. But when I upload image second time, I am getting following error. How can I fix this error?

Here is the error:

enter image description here

Here is my activity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // getActionBar().setDisplayShowTitleEnabled(false);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    s3Client.setRegion(Region.getRegion(Regions.US_WEST_2));
    setContentView(R.layout.submit);

    submit = (Button) findViewById(R.id.buttonsubmit);
    submit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Uri selectedImage = Uri.parse(Environment
                    .getExternalStorageDirectory().getPath()
                    + File.separator
                    + "Pictures"
                    + File.separator
                    + "Spike" + File.separator + "cubicasa.jpg");

            new S3PutObjectTask().execute(selectedImage);



        }
    });

}
private class S3PutObjectTask extends AsyncTask<Uri, Void, S3TaskResult> {

    ProgressDialog dialog;

    protected void onPreExecute() {
        dialog = new ProgressDialog(SubmitActivity.this);
        dialog.setMessage(SubmitActivity.this.getString(R.string.uploading));
        dialog.setCancelable(false);
        dialog.show();
    }

    protected S3TaskResult doInBackground(Uri... uris) {



        if (uris == null || uris.length != 1) {
            return null;
        }

        // The file location of the image selected.
        String filepath = Environment.getExternalStorageDirectory()
                .toString()
                + File.separator
                + "Pictures"
                + File.separator
                + "Spike" + File.separator + "cubicasa.jpg";
        Uri selectedImage = Uri.fromFile(new File(filepath));
        // Uri selectedImage =
        // Uri.parse("content://media/external/images/media/40894");

        String URLLLLLLLLL = selectedImage.toString();
        Log.e("uRLLLLLLLLLLLLLL", URLLLLLLLLL);
        ContentResolver resolver = getContentResolver();

        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentType(resolver.getType(selectedImage));

        S3TaskResult result = new S3TaskResult();

        // Put the image data into S3.
        try {
            s3Client.createBucket(Constants.getPictureBucket());

            PutObjectRequest por = new PutObjectRequest(
                    Constants.getPictureBucket(), Constants.PICTURE_NAME,
                    resolver.openInputStream(selectedImage), metadata);
            s3Client.putObject(por);
        } catch (Exception exception) {

            result.setErrorMessage(exception.getMessage());
        }

        return result;
    }

    protected void onPostExecute(S3TaskResult result) {

        dialog.dismiss();

        if (result.getErrorMessage() != null) {

            displayErrorAlert(
                    SubmitActivity.this
                            .getString(R.string.upload_failure_title),
                    result.getErrorMessage());
        } else {
            Toast toast = Toast.makeText(getApplicationContext(),
                    "Uploaded Successfully", Toast.LENGTH_SHORT);
            toast.show();

        }
    }
}

Here is my constantclass:

public class Constants {

public static final String ACCESS_KEY_ID = "accesskey";
public static final String SECRET_KEY = "secretkey";

public static final String PICTURE_BUCKET = "picture-bucket5";
public static final String PICTURE_NAME = "NameOfThePicture5";


public static String getPictureBucket() {
    return ("bd.dse.test" + ACCESS_KEY_ID + PICTURE_BUCKET).toLowerCase(Locale.US);
}

Any help will be greatly appreciated. Thanks in advance.

Upvotes: 5

Views: 484

Answers (1)

RajaReddy PolamReddy
RajaReddy PolamReddy

Reputation: 22493

From your code : your are creating bucket every time, don't do like that that will cause duplicate of bucket. create bucket once with name (string) like this below code.

s3Client.createBucket("images");

from next time on wards don't call this creating bucket in your code, just put images in that bucket like this following code.

S3.createObjectForBucket("images", Token, _Image);

Upvotes: 5

Related Questions