尝试从AWS EC2客户端访问存储桶时出现位置约束异常 [英] Location constraint exception while trying to access bucket from aws ec2 client

查看:1593
本文介绍了尝试从AWS EC2客户端访问存储桶时出现位置约束异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Java Web应用程序创建存储桶。我的tomcat是在AWS EC2实例上配置的。在尝试连接到AWS S3时,出现以下错误:

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

这是代码示例:

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


推荐答案

在上面的示例中:

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

区域和 LocationConstraint都应该匹配。如果要在 ap-south-1中创建存储桶,则应将两者都设置为该值。

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.

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

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).

通过排除 LocationConstraint,创建存储桶的位置完全基于您所连接的 Region。通过使用 LocationConstraint,可以确保您不在试图在想要的区域之外的区域中创建存储桶。

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.

这篇关于尝试从AWS EC2客户端访问存储桶时出现位置约束异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆