Home > Net >  Mock AWS s3 service in C#
Mock AWS s3 service in C#

Time:08-24

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

 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 ...,"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/

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("           
  • Related