Reputation: 145
When utilizing the .Net AWS SDK for S3, I've noticed that by default when talking to a bucket in us-east-1, that it is utilizing the legacy global endpoint BUCKETNAME.s3.amazonaws.com
rather then the regional endpoint BUCKETNAME.s3.us-east-1.amazonaws.com
.
How do I force the SDK to utilize the regional endpoint?
Example .Net 6 code:
using Amazon;
using Amazon.Runtime;
using Amazon.S3;
AmazonS3Config config = new Amazon.S3.AmazonS3Config();
config.RegionEndpoint = RegionEndpoint.GetBySystemName("us-east-1");
Console.WriteLine($"S3 Client Service URL: {config.DetermineServiceURL()}");
S3 Client Service URL: https://s3.amazonaws.com/
Upvotes: 1
Views: 715
Reputation: 23602
To send the us-east-1
S3 requests to the regional endpoint, set the USEast1RegionalEndpointValue
property on the AmazonS3Config
object to S3UsEast1RegionalEndpointValue.Regional
.
This will force the routing of us-east-1
S3 requests to the regional endpoint instead of the legacy global endpoint.
using Amazon;
using Amazon.Runtime;
using Amazon.S3;
AmazonS3Config config = new AmazonS3Config
{
USEast1RegionalEndpointValue = S3UsEast1RegionalEndpointValue.Regional,
RegionEndpoint = RegionEndpoint.USEast1
};
Console.WriteLine($"S3 Client Service URL: {config.DetermineServiceURL()}");
S3 Client Service URL: https://s3.us-east-1.amazonaws.com/
GetPreSignedURL
example for the us-east-1
region using the regional endpoint:
var bucketName = "your-bucket-name";
var amazonS3Config = new AmazonS3Config
{
RegionEndpoint = RegionEndpoint.USEast1,
USEast1RegionalEndpointValue = S3UsEast1RegionalEndpointValue.Regional
};
using var s3Client = new AmazonS3Client(amazonS3Config);
var objectKey = "your-object-key";
var request = new GetPreSignedUrlRequest
{
BucketName = bucketName,
Key = objectKey,
Expires = DateTime.UtcNow.AddHours(1),
};
var preSignedUrl = s3Client.GetPreSignedURL(request);
Console.WriteLine("Pre-signed URL: " + preSignedUrl);
Pre-signed URL: https://your-bucket-name.s3.us-east-1.amazonaws.com/your-object-key?AWSAccessKeyId=xxx&Expires=xxx&Signature=xxx
Upvotes: 1
Reputation: 1
You can do this more easily if you simply pass the appropriate region to the S3 client object's constructor. For example:
_s3Client = new AmazonS3Client(RegionEndpoint.USEast1);
Upvotes: 0
Reputation: 145
You'll need to tell the client to explicitly utilize the regional endpoint rather than the legacy global endpoint which is the default.
config.USEast1RegionalEndpointValue = S3UsEast1RegionalEndpointValue.Regional;
By modifying the code with this change:
using Amazon;
using Amazon.Runtime;
using Amazon.S3;
AmazonS3Config config = new Amazon.S3.AmazonS3Config();
config.RegionEndpoint = RegionEndpoint.GetBySystemName("us-east-1");
config.USEast1RegionalEndpointValue = S3UsEast1RegionalEndpointValue.Regional;
Console.WriteLine($"S3 Client Service URL: {config.DetermineServiceURL()}");
You now get the regional endpoint service url:
S3 Client Service URL: https://s3.us-east-1.amazonaws.com/
Upvotes: 1