Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 573
Mohammad Rajob Avatar asked Mar 12 '14 04:03

Mohammad Rajob


People also ask

How do you check if S3 bucket already exists?

To check whether a bucket already exists before attempting to create one with the same name, call the doesBucketExist method. It will return true if the bucket exists, and false otherwise.

Why is my S3 bucket Access Denied?

If you're getting Access Denied errors on public read requests that are allowed, check the bucket's Amazon S3 Block Public Access settings. Review the S3 Block Public Access settings at both the account and bucket level. These settings can override permissions that allow public read access.

Why can't I access an object that was uploaded to my Amazon S3 bucket by another AWS account?

For these existing buckets, an object owner had to explicitly grant permissions to an object (by attaching an access control list). Otherwise, the bucket owner would be unable to access the object.


1 Answers

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);
like image 134
RajaReddy PolamReddy Avatar answered Sep 27 '22 18:09

RajaReddy PolamReddy