Localstack: C# can't connect to S3

Created on 27 Sep 2018  路  3Comments  路  Source: localstack/localstack

I set up the localstack S3 using docker:

docker run -p 4567-4583:4567-4583 -p 8080:8080 -e SERVICES=apigateway,s3,s3api localstack/localstack

Then I created the bucket and added the file:

aws --endpoint-url=http://localhost:4572 s3 mb s3://apidocportal
aws --endpoint-url=http://localhost:4572 s3 cp .\Documents\test.txt s3://apidocportal/test.txt

Verified file exists:

aws --endpoint-url=http://localhost:4572 s3 ls s3://apidocportal

Result: 2018-09-27 09:00:37 4 test.txt

Then if I use the C# API to specify the ServiceURL and make a call, I get the following error at client.GetObjectAsync:

[10:33:43 FTL] Error making request with Error Code BadGateway and Http Status Code BadGateway. No further error information was returned by the service.
Amazon.S3.AmazonS3Exception: Error making request with Error Code BadGateway and Http Status Code BadGateway. No further error information was returned by the service. ---> Amazon.Runtime.Internal.HttpErrorResponseException: Exception of type 'Amazon.Runtime.Internal.HttpErrorResponseException' was thrown.
   at Amazon.Runtime.HttpWebRequestMessage.GetResponseAsync(CancellationToken cancellationToken) in E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Pipeline\HttpHandler\_mobile\HttpRequestMessageFactory.cs:line 443
   at Amazon.Runtime.Internal.HttpHandler`1.InvokeAsync[T](IExecutionContext executionContext) in E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Pipeline\HttpHandler\HttpHandler.cs:line 175
   at Amazon.Runtime.Internal.RedirectHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.Unmarshaller.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.S3.Internal.AmazonS3ResponseHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.ErrorHandler.InvokeAsync[T](IExecutionContext executionContext)

This is my code:
```c#
var config = new AmazonS3Config
{
ServiceURL = "http://localhost:4572",
UseHttp = true
};

var client = new AmazonS3Client(config);
var request = new GetObjectRequest
{
BucketName = "apidocportal",
Key = "test.txt"
};

using (GetObjectResponse response = await client.GetObjectAsync(request))
{
response.ResponseStream.Position = 0;
return new FileResult(response.Headers["Content-Type"], response.ResponseStream);
}
```

Most helpful comment

I could not reproduce your issue exactly, but have some experience getting LocalStack S3 working with C#. Hope this is useful to you.

You definitely want to add ForcePathStyle = true in your AmazonS3Config-- this is a key configuration change to get LocalStack S3 working in the .NET SDK. Here is a snippet that has worked reliably for me against LocalStack:

AmazonS3Config config = new AmazonS3Config
{
    ServiceURL = "http://localhost:4572",
    UseHttp = true,
    ForcePathStyle = true,
    AuthenticationRegion = "us-east-1",
};
AWSCredentials creds = new AnonymousAWSCredentials();

AmazonS3Client  client = new AmazonS3Client(creds, config);

All 3 comments

I could not reproduce your issue exactly, but have some experience getting LocalStack S3 working with C#. Hope this is useful to you.

You definitely want to add ForcePathStyle = true in your AmazonS3Config-- this is a key configuration change to get LocalStack S3 working in the .NET SDK. Here is a snippet that has worked reliably for me against LocalStack:

AmazonS3Config config = new AmazonS3Config
{
    ServiceURL = "http://localhost:4572",
    UseHttp = true,
    ForcePathStyle = true,
    AuthenticationRegion = "us-east-1",
};
AWSCredentials creds = new AnonymousAWSCredentials();

AmazonS3Client  client = new AmazonS3Client(creds, config);

I could not reproduce your issue exactly, but have some experience getting LocalStack S3 working with C#. Hope this is useful to you.

You definitely want to add ForcePathStyle = true in your AmazonS3Config-- this is a key configuration change to get LocalStack S3 working in the .NET SDK. Here is a snippet that has worked reliably for me against LocalStack:

AmazonS3Config config = new AmazonS3Config
{
    ServiceURL = "http://localhost:4572",
    UseHttp = true,
    ForcePathStyle = true,
    AuthenticationRegion = "us-east-1",
};
AWSCredentials creds = new AnonymousAWSCredentials();

AmazonS3Client  client = new AmazonS3Client(creds, config);

Just tested and had to use

AWSCredentials creds = new BasicAWSCredentials("key", "secret");

As it was complaining about using anonymous credentials.

Thanks for providing solutions to this issue @guillemdj @CurryEleison ! Closing this issue - please report here if the problem persists @holotrek. Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Deblob12 picture Deblob12  路  3Comments

burm87 picture burm87  路  3Comments

jeson114 picture jeson114  路  3Comments

jakubov picture jakubov  路  3Comments

sabrehagen picture sabrehagen  路  3Comments