配置S3客户端的ServiceURL使用AWSCredentials或RegionEndPoint。

huangapple go评论57阅读模式
英文:

Configure S3 client ServiceURL with AWSCredentials or RegionEndPoint

问题

我正在尝试为一个使用AmazonS3Client上传文件到Amazon S3存储桶的服务编写集成测试。我在http://localhost:4556上运行了一个Localstack实例。我遇到的问题是服务中的代码如下设置S3客户端:

var region = RegionEndpoint.GetBySystemName(config.Region);
var client = new AmazonS3Client(awsCredentials, region);

我能够使用以下代码设置自定义的AWS凭据:

const string ProfileName = "localstack";
const string AccessKey = "foo";
const string SecretKey = "goo";
var profileOptions = new CredentialProfileOptions()
{
    AccessKey = AccessKey,
    SecretKey = SecretKey,
};
var profile = new CredentialProfile(ProfileName, profileOptions);
var file = new SharedCredentialsFile();
file.RegisterProfile(profile);
var profileManager = new CredentialProfileStoreChain();

profileManager.RegisterProfile(profile);

profileManager.TryGetProfile(ProfileName, out CredentialProfile profileResult);

profileResult.Options.AccessKey = AccessKey;
profileResult.Options.SecretKey = SecretKey;
profileManager.RegisterProfile(profileResult);

是否有办法确保将创建的客户端将使用我的Localstack实例作为服务器?

我尝试查找一种通过RegionEndpoint或AWSCredentials类配置ServiceURL的方法,但没有找到任何有用的信息。我可能遗漏了一些东西。

英文:

I'm trying to write an integration test for a service that uses the AmazonS3Client in order to upload files to amazon s3 buckets.
I am running an instance of localstack on http://localhost:4556
The problem I'm having is the code in the service is setting up the s3 clients the following way:

var region = RegionEndpoint.GetBySystemName(config.Region);
var client = new AmazonS3Client(awsCredentials, region);

I am able to set custom AWS Credentials using the following code:

const string ProfileName = "localstack";
            const string AccessKey = "foo";
            const string SecretKey = "goo";
            var profileOptions = new CredentialProfileOptions()
            {
                AccessKey = AccessKey,
                SecretKey = SecretKey,
            };
            var profile = new CredentialProfile(ProfileName, profileOptions);
            var file = new SharedCredentialsFile();
            file.RegisterProfile(profile);
            var profileManager = new CredentialProfileStoreChain();

            profileManager.RegisterProfile(profile);

            profileManager.TryGetProfile(ProfileName, out CredentialProfile profileResult);

            profileResult.Options.AccessKey = AccessKey;
            profileResult.Options.SecretKey = SecretKey;
            profileManager.RegisterProfile(profileResult);

Is there a way to make sure that the client that will be created will use my localstack instance as a server?

I tried looking for a way to configure the ServiceURL through the RegionEndPoint or AWSCredentials class but without any luck. I might have missed somehting.

答案1

得分: 1

你可以在文档中查看更多关于如何配置客户端的信息:

https://docs.localstack.cloud/user-guide/integrations/sdks/dotnet/

有这个注意事项:

如果您想在创建客户端时指定区域和凭据,请将它们设置为AuthenticationRegion和BasicAWSCredentials,就像在这个示例中一样:

var lambdaClient = new AmazonLambdaClient(new BasicAWSCredentials("test", "test"), new AmazonLambdaConfig(
    {
        ServiceURL = "http://localhost:4566",
        AuthenticationRegion = "eu-west-1"
    }));

注意

请确保您设置的是AuthenticationRegion,而不是RegionEndpoint。将RegionEndpoint设置为常量,如RegionEndpoint.EUWest1,将覆盖ServiceURL,您的请求将发送到AWS。

另外,由于虚拟主机寻址存储桶(如在此GitHub问题评论中所见),S3在某种意义上有些特殊。使用上面的示例,您可以将ForcePathStyle = true添加到配置中,以确保不会遇到任何问题。

使用您的示例:

var config = new AmazonS3Config(
    {
        ServiceURL = "http://localhost:4566",
        AuthenticationRegion = config.region,
        ForcePathStyle = true
    }
);
var client = new AmazonS3Client(awsCredentials, config);
英文:

You can see here in the documentation some more informations about how to configure your client:

https://docs.localstack.cloud/user-guide/integrations/sdks/dotnet/

There's this note:

> If you want to specify a region and credentials when creating the client, please set them as AuthenticationRegion and BasicAWSCredentials, like in this example:
>
>
>var lambdaClient = new AmazonLambdaClient(new BasicAWSCredentials("test", "test"), new AmazonLambdaConfig(
> {
> ServiceURL = "http://localhost:4566",
> AuthenticationRegion = "eu-west-1"
> }));
>

> #### Note
>
> Make sure you are setting the AuthenticationRegion and not the RegionEndpoint. Setting the RegionEndpoint to a constant like RegionEndpoint.EUWest1 will override the ServiceURL, and your request will end up against AWS.

Also, S3 is a bit specific in a sense, because of virtual host addressed buckets (as seen in this GitHub issue comment). Using the example above, you could add ForcePathStyle = true to the config to be sure to not encounter any issue.

Using your example:

var config = new AmazonS3Config(
    {
        ServiceURL = "http://localhost:4566",
        AuthenticationRegion = config.region,
        ForcePathStyle = true
    }
);
var client = new AmazonS3Client(awsCredentials, config);

huangapple
  • 本文由 发表于 2023年3月15日 19:36:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定