我正在使用Play framework 2.5和aws-java-sdk version 1.11.12。我正在打电话为特定区域创建一个S3存储桶,并且收到一个我不理解的错误。它告诉我该区域是错误的,应该是us-west-2,即使这是我传递给createBucket调用的内容。有人可以帮忙吗?
@Inject
public AmazonS3ComponentImpl(Configuration configuration) {
String accessKey = configuration.getString(AWS_ACCESS_KEY);
String secretKey = configuration.getString(AWS_SECRET_KEY);
amazonS3Bucket = configuration.getString(AWS_S3_BUCKET);
if ((accessKey != null) && (secretKey != null)) {
AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
amazonS3 = new AmazonS3Client(awsCredentials);
try {
amazonS3.createBucket(amazonS3Bucket, Region.US_West_2);
} catch (AmazonS3Exception e) {
if (e.getErrorCode().compareTo("BucketAlreadyOwnedByYou") != 0
&& e.getErrorCode().compareTo("AccessDenied") != 0) {
throw e;
}
} finally {
Logger.info("Using bucket " + amazonS3Bucket);
}
}
}
错误:
Caused by: com.amazonaws.services.s3.model.AmazonS3Exception: The authorization header is malformed; the region 'us-east-1' is wrong; expecting 'us-west-2' (Service: Amazon S3; Status Code: 400; Error Code: AuthorizationHeaderMalformed; Request ID: 261B779D23D15F6F)
at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:1372)
at com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:919)
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:697)
at com.amazonaws.http.AmazonHttpClient.doExecute(AmazonHttpClient.java:449)
at com.amazonaws.http.AmazonHttpClient.executeWithTimer(AmazonHttpClient.java:411)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:360)
at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:3962)
at com.amazonaws.services.s3.AmazonS3Client.createBucket(AmazonS3Client.java:908)
at com.amazonaws.services.s3.AmazonS3Client.createBucket(AmazonS3Client.java:855)
at com.zently.modules.AmazonS3ComponentImpl.<init>(AmazonS3Component.java:42)
答案 0 :(得分:2)
在创建新的AmazonS3Client后立即添加以下代码行就可以了。
amazonS3.setRegion(com.amazonaws.regions.Region.getRegion(Regions.US_WEST_2));
答案 1 :(得分:0)
如果存储区名称已存在于给定区域中,则可能会出现此错误。尝试给出一些其他的桶名称,例如&#34; yournamebucket&#34;这通常是独一无二的。我试过这种方式,它对我有用。