简体   繁体   中英

How do I force the AWS .NET SDK to utilize the us-east-1 regional endpoint for S3 rather than the legacy global endpoint?

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/

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/

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/

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM