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:
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;..).
This is the code sample:
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);}
There are some rules while using "locationConstraint":
In short:
In your example above:
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'})
Both "region" and "LocationConstraint" should match. If you want to create the bucket in "ap-south-1" then both should be set to that value.
The error you received was due to the two values not matching, in other words you connected to one region (likely ap-south-1), and then tried to create a bucket which is intended to exist in another region (ap-northeast-2).
By excluding "LocationConstraint", the location where the bucket is created is entirely based on the "Region" which you are connected to. By using "LocationConstraint" you can ensure you are not trying to create the bucket in a region other than the one you intended.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With