Location constraint exception while trying to access bucket from aws ec2 client
<p>I am trying to create bucket from java web application. My tomcat is configured on AWS EC2 instance. It is giving following error, while it tries to connect to AWS S3:</p>

<pre><code>com.amazonaws.services.s3.model.AmazonS3Exception:  
The unspecified location constraint is incompatible for the region specific endpoint this request was sent to. 
(Service: Amazon S3; Status Code: 400;..).
</code></pre>

<p>This is the code sample:</p>

<pre><code>public class FileOperationsUtil {
 private final BasicAWSCredentials awsCreds = new BasicAWSCredentials("xyz", "zyz");
private final  AmazonS3 s3Client = new AmazonS3Client(awsCreds); private final String bucketName = "grex-prod"; 
      //public static final Region ap-south-1;
 public void uploadFile(InputStream fileInputStream,
String fileUploadLocation, String fileName) throws IOException {
s3Client.setRegion(Region.getRegion(Regions.AP_SOUTH_1));
// Region apsouth1 = Region.getRegion(Regions.ap-south-1);                  //   s3Client.setRegion(apsouth1);                                                                  //  s3Client.setRegion(Region.getRegion(Regions.ap-south-1));
//s3Client.create_bucket(bucket, CreateBucketConfiguration={'LocationConstraint': 'ap-northeast-2'})
s3Client.createBucket(bucketName);
File fileToUpload = new File(fileUploadLocation);
        fileToUpload.mkdirs();
// Full file path
        String fullFilePath = (fileUploadLocation + fileName);
        ObjectMetadata meta = new ObjectMetadata();

        // meta.setContentLength(contents.length);
        meta.setContentType("image/png");

        // Upload files to a specific AWS s3 bucket
        s3Client.putObject(new PutObjectRequest("grex-prod", fullFilePath,
                fileInputStream, meta)
                .withCannedAcl(CannedAccessControlList.Private));
     }
    public void deleteFolder(String oldFullFilePath) {
       // System.out.println("inside");
       ObjectListing objects = s3Client.listObjects(bucketName, oldFullFilePath);
        for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {
s3Client.deleteObject(bucketName, objectSummary.getKey());}
s3Client.deleteObject(bucketName, oldFullFilePath);}
</code></pre>