4

I have localstack (https://github.com/localstack/localstack) running and am able to use the aws s3 cli to upload files to it.

What I want to be able to do is use the .NET AWS ADK with localstack. I'd like the following code to upload a file into localstack:

        using (var tfu = new TransferUtility())
        {
            await tfu.UploadAsync(new TransferUtilityUploadRequest
            {
                Key = key,
                BucketName = bucketName,
                ContentType = document.ContentType,
                Headers = { ["Content-Disposition"] = "attachment; filename=\"test.txt\"" },
                InputStream = stream
            });
        }

My problem is I don't know how to set the endpoints so that localstack is used by the SDK rather than aws. Apparently you can set the AWSEndpointDefinition in appSettings.config as mentioned in the AWS SDK documentation, e.g:

<add key="AWSEndpointDefinition" value="C:\Dev\localstack\endpoints.json"/>

However I have no idea what this endpoints.json config should look like. I tried using this file:

https://raw.githubusercontent.com/aws/aws-sdk-net/master/sdk/src/Core/endpoints.json

When I do this, as soon as I new up a TransferUtility class I get a null reference exception - this is before I point anything to my localstack setup.

The version of AWS ASK is 3.3.0.

Another thing to note is that in some places in the documentation it is implied that the config should be an xml file rather than a json, however, when I try to use an xml file instead I get a different exception when newing up TransferUtility: 'Invalid character '<' in input string'

4

1 回答 1

4

您可以通过创建 S3 客户端并将其传递给 TransferUtility 构造函数来轻松覆盖它。

var config = new AmazonS3Config { ServiceURL = "http://localhost:4572" };
var s3client = new AmazonS3Client(config);

如果您的 localstack 为 S3 使用不同的端口,请不要忘记替换 URL。

希望这可以帮助。

于 2018-06-21T15:05:32.780 回答