简体   繁体   English

在 C# 中模拟 AWS s3 服务

[英]Mock AWS s3 service in C#

I'm trying to mock AWS s3 service (more specifically the upload of files to s3), for that I'm using https://docs.getmoto.org/en/latest/docs/server_mode.html , the problem is I'm getting我正在尝试模拟 AWS s3 服务(更具体地说是将文件上传到 s3),因为我正在使用https://docs.getmoto.org/en/latest/docs/server_mode.html ,问题是我得到

 error during upload","@i":"4c86b2a2","@l":"Fatal","@x":"Amazon.Runtime.AmazonServiceException: Unable to get IAM security credentials from EC2 Instance Metadata Service.\n   at Amazon.Runtime.DefaultInstanceProfileAWSCredentials.FetchCredentials()\n   at Amazon.Runtime.DefaultInstanceProfileAWSCredentials.GetCredentials()\n   at Amazon.Runtime.DefaultInstanceProfileAWSCredentials.GetCredentialsAsync()\n   at Amazon.Runtime.Internal.CredentialsRetriever.InvokeAsync[T](IExecutionContext executionContext)\n   at Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext executionContext)\n   at Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext executionContext)\n   at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)\n   at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)\n   at Amazon.S3.Internal.AmazonS3ExceptionHandler.InvokeAsync[T](IExecutionContext executionContext)\n   at Amazon.Runtime.Internal.ErrorCallbackHandler.InvokeAsync[T](IExecutionContext executionContext)\n   at Amazon.Runtime.Internal.MetricsHandler.InvokeAsync[T](IExecutionContext executionContext)\n   at LiteBit.Exchange.Savior.Infrastructure.SaviorS3Client.Upload(MemoryStream stream, String key, Int32 callerIndex) in /Users/diegocolli/Projects/LiteBit-Exchange-Savior/src/Savior/Infrastructure/SaviorS3Client.cs:line 41","SaviorIndex":0,"SourceContext":"LiteBit.Exchange.Savior.Infrastructure.SaviorS3Client","ProcessId":32227,"ThreadId":25,"Application":"Savior","ExceptionDetail":{"HResult":-2146233088,"Message":"Unable to get IAM security credentials from EC2 Instance Metadata Service.","Source":"AWSSDK.Core","TargetSite":"CredentialsRefreshState FetchCredentials()","ErrorType":"Sender","ErrorCode":null,"RequestId":null,"StatusCode":"0","Retryable":null,"Type":"Amazon.Runtime.AmazonServiceException"}}

I'm using the following configuration我正在使用以下配置

  "AWS": {
    "Profile": "default",
    "Region": "eu-central-1",
    "ServiceURL": "http://127.0.0.1:5000/"
  },

Obs.: looks like Moto is indeed running at http://127.0.0.1:5000/观察:看起来 Moto 确实在http://127.0.0.1:5000/上运行

registration登记

        builder.Register(componentContext => 
        {
            var configuration = componentContext.Resolve<IConfiguration>();
            return configuration.GetAWSOptions();
        }).As<AWSOptions>().SingleInstance();
    }

        builder.Register(componentContext =>
            {
                var options = componentContext.Resolve<AWSOptions>();
                var client = options.CreateServiceClient<IAmazonS3>();
                return client;
            })
            .As<IAmazonS3>()
            .InstancePerDependency();

usage:用法:

        var s3PutRequest = new PutObjectRequest
        {
            BucketName = _settings.S3BucketName,
            Key = key,
            InputStream = stream,
            CalculateContentMD5Header = true
        };

        try
        {
            var response = await _s3Client.PutObjectAsync(s3PutRequest);
            _logger.LogInformation("💾 Uploaded {File} - {StatusCode}", s3PutRequest.Key, response.HttpStatusCode);
        }
        catch (Exception ex)
        {
            _logger.LogCritical(ex, " error during upload");
            throw;
        }

I have moto running at: http://127.0.0.1:5000/moto-api/ , There is not much documentation so I'm not sure what I should do.我的 moto 运行在: http://127.0.0.1:5000/moto-api/ ,没有太多文档,所以我不确定我应该做什么。

Please feel free to suggest a possible easier solution for my problem.请随时为我的问题提出一个可能更简单的解决方案。

An alternate solution to Mock AWS s3 service in C# is by using MinIO . C# 中模拟 AWS s3 服务的替代解决方案是使用MinIO With MinIO you can use same AWS S3 client that you would use in your production application.借助 MinIO,您可以使用在生产应用程序中使用的相同 AWS S3 客户端。 You would just change the URL pointing to the server.您只需更改指向服务器的 URL 即可。 That means you would use exactly the same code but just with different URLs for different environments.这意味着您将使用完全相同的代码,但在不同的环境中使用不同的 URL。 Please take a look at the example from docs请看一下文档中的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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