I'm trying to delete an object from an S3 bucket located in the me-south-1
region, but I'm getting the following error:
The me-south-1 location constraint is incompatible for the region specific endpoint this request was sent to.
However, when I perform the same operation on an S3 bucket in the us-east-1
region, it works without any issues.
export const deleteOneFeed = async ({ shop_name, file_name }) => {
const key =
process.env.NODE_ENV === "development"
? `app-ame/${shop_name}/${file_name}`
: `${shop_name}/${file_name}`;
const command = new DeleteObjectCommand({
Key: key,
Bucket: process.env.BUCKET_NAME,
});
await s3Client.send(command);
return true;
};
What could be causing this issue? Is there a specific endpoint I should be using for me-south-1
?
I'm trying to delete an object from an S3 bucket located in the me-south-1
region, but I'm getting the following error:
The me-south-1 location constraint is incompatible for the region specific endpoint this request was sent to.
However, when I perform the same operation on an S3 bucket in the us-east-1
region, it works without any issues.
export const deleteOneFeed = async ({ shop_name, file_name }) => {
const key =
process.env.NODE_ENV === "development"
? `app-ame/${shop_name}/${file_name}`
: `${shop_name}/${file_name}`;
const command = new DeleteObjectCommand({
Key: key,
Bucket: process.env.BUCKET_NAME,
});
await s3Client.send(command);
return true;
};
What could be causing this issue? Is there a specific endpoint I should be using for me-south-1
?
2 Answers
Reset to default 0When performing operations on a bucket in a particular Region (eg me-south-1
), you must use a client that is configured to use that Region.
You did not show us how you created the s3Client
, but it is likely that you either specified another region, or you did not specify a region (in which case it defaults to us-east-1
).
From Set the AWS Region - AWS SDK for JavaScript:
When you instantiate a service object, you can specify the AWS Region for that resource as part of the client class constructor, as shown here.
const s3Client = new S3.S3Client({region: 'us-west-2'});
Just make sure the region
specified matches the region of the bucket you are wanting to use.
me-south-1
is an opt-in region Ref: https://docs.aws.amazon.com/controltower/latest/userguide/opt-in-region-considerations.html
When you send a request to a bucket in opt-in region you need to send the request with region
Reason: There a restriction in place for the entire S3 service. S3 does not allow cross region calls between opt-in regions and other regions.
In order to set the region you need to use this : https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/setting-region.html
const s3Client = new S3.S3Client({region: 'us-west-2'});
Ref: https://docs.aws.amazon.com/AmazonS3/latest/userguide/create-bucket-overview.html (see using the AWS SDK section)
When you create a client, the Region maps to the Region-specific endpoint. The client uses this endpoint to communicate with Amazon S3: s3.region.amazonaws.com. If your Region launched after March 20, 2019, your client and bucket must be in the same Region. However, you can use a client in the US East (N. Virginia) Region to create a bucket in any Region that launched before March 20, 2019. For more information, see Legacy endpoints.
Opt-in regions don't have request rerouting. There isn't a workaround for this as far as I know, but for what it's worth, including the region in the request is considered best practice, even for non-opt-in regions.
There was a similar issue with AWS CLI same reason: https://github.com/aws/aws-cli/issues/8289