I am working on project with Amazon S3 and I am using AWS SDK 2 to interact with S3. I have many old buckets created into my S3 account and now I want to change the code of authentication and setting the ACL.
I have created S3Client :
AwsBasicCredentials awsCreds = AwsBasicCredentials.create(
ACCESS_KEY,
SECRET_KEY);
S3ClientBuilder s3ClientBuilder = S3Client.builder().credentialsProvider(StaticCredentialsProvider.create(awsCreds));
if (regionString != null) {
Region region = Region.of(regionString);
s3ClientBuilder.region(region);
}
S3Client s3Client = s3ClientBuilder.build();
return s3Client;
Default region I have given is : US_EAST_1.
If I do not specify this region then it showing the creation dates of bucket wrong.
Now by using above S3client, I am moving forward and working with ACL :
software.amazon.awssdk.services.s3.model.GetBucketAclRequest getBucketAclRequest =
software.amazon.awssdk.services.s3.model.GetBucketAclRequest.builder().bucket(sbktname).build();
GetBucketAclResponse bucketAcl1 = s3Client.getBucketAcl(getBucketAclRequest);
But it throwing issue : software.amazon.awssdk.services.s3.model.S3Exception: The authorization header is malformed; the region 'us-east-1' is wrong; expecting 'eu-central-1'
My question is that how can I get region from existing s3 client which already created using bucket name only. I do not have bucket object, I have bucket name only.
Using bucket name I tried to get the location but it throwing same issue of region mentioned above.
I can't give you an example in Java, but you need to use
GetBucketLocation
API.https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketLocation.html
From the following example look that I am forcing to run the command on
sa-east-1
region but my bucket is located onus-east-2
.You can see the same behavior from Python SDK
Buckets in Region
us-east-1
have a LocationConstraint ofnull
.