尝试从aws ec2客户端访问存储区时的位置约束异常

时间:2016-12-14 11:04:13

标签: amazon-web-services amazon-s3 amazon-ec2 tomcat7 amazon

我正在尝试从java Web应用程序创建存储桶。我的tomcat是在AWS EC2实例上配置的。它在尝试连接到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;..).

这是代码示例:

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

2 个答案:

答案 0 :(得分:0)

在上面的示例中:

subscribe()

两个"地区"和" LocationConstraint"应该匹配。如果你想在" ap-south-1"中创建一个桶。然后两者都应该设置为该值。

您收到的错误是由于两个值不匹配,换句话说,您连接到一个区域(可能是ap-south-1),然后尝试创建一个打算存在于另一个区域的存储桶(ap -northeast-2)。

通过排除" LocationConstraint",创建存储桶的位置完全基于" Region"你连接到哪个。通过使用" LocationConstraint"你可以确保你没有尝试在你想要的区域之外创建一个桶。

答案 1 :(得分:0)

使用“ locationConstraint”时有一些规则:

  1. “ us-east-1”区域与“ locationConstraint”中的所有区域都兼容
  2. 对于任何其他区域,region和locationConstraint必须相同。

简而言之:

  • 仅使用“ us-east-1”,您可以将任何区域用作locationConstraint以便在所需区域中创建存储桶。(region ='us-east-1'locationConstraint =)
  • 与所有其他区域的region和locationConstraint应该相同(region ='us-west-2',locationConstraint ='us-west-2') 任何其他区域都将引发“ code:IllegalLocationConstraintException”