簡體   English   中英

在 C# 中模擬 AWS s3 服務

[英]Mock AWS s3 service in C#

我正在嘗試模擬 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"}}

我正在使用以下配置

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

觀察:看起來 Moto 確實在http://127.0.0.1:5000/上運行

登記

        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();

用法:

        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;
        }

我的 moto 運行在: http://127.0.0.1:5000/moto-api/ ,沒有太多文檔,所以我不確定我應該做什么。

請隨時為我的問題提出一個可能更簡單的解決方案。

C# 中模擬 AWS s3 服務的替代解決方案是使用MinIO 借助 MinIO,您可以使用在生產應用程序中使用的相同 AWS S3 客戶端。 您只需更改指向服務器的 URL 即可。 這意味着您將使用完全相同的代碼,但在不同的環境中使用不同的 URL。 請看一下文檔中的示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM