Skip to content

Instantly share code, notes, and snippets.

@dany1468
Last active May 24, 2018 01:10
Show Gist options
  • Select an option

  • Save dany1468/9f8e0285f3be249a6c0b57cd5ff4dc6b to your computer and use it in GitHub Desktop.

Select an option

Save dany1468/9f8e0285f3be249a6c0b57cd5ff4dc6b to your computer and use it in GitHub Desktop.
AWS SDK for .NET で S3 の動作確認をローカルの fake サービスに向けて行う ref: https://qiita.com/dany1468/items/d895192b9f8e6450621d
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="aws" type="Amazon.AWSSection, AWSSDK.Core"/>
</configSections>
<aws region="us-east-1" endpointDefinition="endpoints.json">
</aws>
<appSettings>
<add key="AWSAccessKey" value="foo"/>
<add key="AWSSecretKey" value="foo"/>
</appSettings>
</configuration>
} ],
"version" : 3
}
version: '3'
services:
localstack-s3:
image: atlassianlabs/localstack:latest
environment:
- SERVICES=s3:5000
- DEFAULT_REGION=us-east-1
- HOSTNAME=localstack-s3
ports:
- "5000:5000"
$ aws --endpoint-url=http://localhost:5000 --region=us-east-1 s3 ls --profile my-profile
var config = new AmazonS3Config {ServiceURL = "http://localhost:5000", AuthenticationRegion = "us-east-1"};
// accesskey/secret は foo/foo と適当に指定
var client = new AmazonS3Client("foo", "foo", config);
// If the existing customer provided the endpoints.json file via
// <aws endpointDefinition=""/>, it's in v2 format. We we will create
// a v2 provider which does a fall through during LoadEndpointDefinitions()
// and loads from the override file provided by the user.
//
// Else, we are loading from the assembly resource. In which case we use the
// latest provider.
//
// It's actually a bug that _regionEndpointProvider is a static member variable
// since the IEndpointProvider should respect the AWSConfigs.EndpointDefinition
// _at_ the time of the service client instantiation. However, since this is
// the existing behavior with v2 endpoint file format, we will preserve this behavior as is.
if (!string.IsNullOrEmpty(AWSConfigs.EndpointDefinition))
{
_regionEndpointProvider = new RegionEndpointProviderV2();
}
else
{
_regionEndpointProvider = new RegionEndpointProviderV3();
}
{
"version": 2,
"endpoints": {
"*/*": {
"endpoint": "{service}.{region}.amazonaws.com"
},
"cn-north-1/*": {
"endpoint": "{service}.{region}.amazonaws.com.cn",
"signatureVersion": "v4"
},
"us-gov-west-1/iam": {
"endpoint": "iam.us-gov.amazonaws.com"
},
"us-gov-west-1/s3": {
"endpoint": "s3-{region}.amazonaws.com"
},
"*/cloudfront": {
"endpoint": "cloudfront.amazonaws.com"
},
"*/iam": {
"endpoint": "iam.amazonaws.com"
},
"*/importexport": {
"endpoint": "importexport.amazonaws.com"
},
"*/route53": {
"endpoint": "route53.amazonaws.com"
},
"*/waf": {
"endpoint": "waf.amazonaws.com"
},
"us-east-1/sdb": {
"endpoint": "sdb.amazonaws.com"
},
"us-east-1/s3": {
- "endpoint": "s3.amazonaws.com"
+ "endpoint": "localhost:5000"
},
"us-west-1/s3": {
"endpoint": "s3-{region}.amazonaws.com"
},
"us-west-2/s3": {
"endpoint": "s3-{region}.amazonaws.com"
},
"eu-west-1/s3": {
"endpoint": "s3-{region}.amazonaws.com"
},
"ap-southeast-1/s3": {
"endpoint": "s3-{region}.amazonaws.com"
},
"ap-southeast-2/s3": {
"endpoint": "s3-{region}.amazonaws.com"
},
"ap-northeast-1/s3": {
"endpoint": "s3-{region}.amazonaws.com"
},
"sa-east-1/s3": {
"endpoint": "s3-{region}.amazonaws.com"
}
}
}
var client = new AmazonS3Client(new AmazonS3Config {UseHttp = true});
// or 設定の有り無しで切り替えるなら以下のような感じ
if (!string.IsNullOrEmpty(AWSConfigs.EndpointDefinition))
new AmazonS3Client(new AmazonS3Config {UseHttp = true});
else
new AmazonS3Client(); // おそらく production は EC2 のインスタンスメタデータ等から接続するはず
public void OneTimeSetUp()
{
if (!string.IsNullOrEmpty(AWSConfigs.EndpointDefinition))
{
if (AWSConfigs.EndpointDefinition.Equals(new FileInfo(AWSConfigs.EndpointDefinition).Name))
{
AWSConfigs.EndpointDefinition =
Path.Combine(TestContext.CurrentContext.TestDirectory, AWSConfigs.EndpointDefinition);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment