Location constraint exception while trying to access bucket from aws ec2 client

∥☆過路亽.° 提交于 2019-12-23 04:04:20

问题


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);}

回答1:


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.




回答2:


There are some rules while using "locationConstraint":

  1. region "us-east-1" is compatible with all regions in "locationConstraint"
  2. With any other region, region and locationConstraint has to be same.

In short:

  • With "us-east-1" only, You can use any region as locationConstraint to create bucket in desired region.(region='us-east-1' locationConstraint=)
  • With all other region region and locationConstraint should be the same(region='us-west-2', locationConstraint='us-west-2') any other region will throw "code:IllegalLocationConstraintException"


来源:https://stackoverflow.com/questions/41141030/location-constraint-exception-while-trying-to-access-bucket-from-aws-ec2-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!