aws-sdk-net: Deleting S3 object fails on .NET for iOS.

Describe the bug

We are using the AWS SDK for .Net in an iOS application built using .NET 8.

We are observing a possible bug when trying to delete an object in a bucket:

In the iOS application, we constantly get an exception (AmazonS3Exception: A header you provided implies functionality that is not implemented) while the same code works fine in a .NET 8 console application. Before migrating the app from Xamarin.iOS to .NET 8, it used to work fine as well.

Expected Behavior

The object should be successfully deleted.

Current Behavior

The following exception is thrown:

AmazonS3Exception: A header you provided implies functionality that is not implemented
                     ▷ Trace:
                       ▷ at Amazon.Runtime.Internal.HttpErrorResponseExceptionHandler.HandleExceptionStream(IRequestContext requestContext, IWebResponseData httpErrorResponse, HttpErrorResponseException exception, Stream responseStream)
                       ▷ at Amazon.Runtime.Internal.HttpErrorResponseExceptionHandler.HandleExceptionAsync(IExecutionContext executionContext, HttpErrorResponseException exception)
                       ▷ at Amazon.Runtime.Internal.ExceptionHandler`1.<HandleAsync>d__6[[Amazon.Runtime.Internal.HttpErrorResponseException, AWSSDK.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604]].MoveNext()
                       ▷ at Amazon.Runtime.Internal.ErrorHandler.ProcessExceptionAsync(IExecutionContext executionContext, Exception exception)
                       ▷ at Amazon.Runtime.Internal.ErrorHandler.<InvokeAsync>d__5`1[[Amazon.S3.Model.DeleteObjectResponse, AWSSDK.S3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604]].MoveNext()
                       ▷ at Amazon.Runtime.Internal.CallbackHandler.<InvokeAsync>d__9`1[[Amazon.S3.Model.DeleteObjectResponse, AWSSDK.S3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604]].MoveNext()
                       ▷ at Amazon.Runtime.Internal.Signer.<InvokeAsync>d__1`1[[Amazon.S3.Model.DeleteObjectResponse, AWSSDK.S3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604]].MoveNext()
                       ▷ at Amazon.S3.Internal.S3Express.S3ExpressPreSigner.<InvokeAsync>d__5`1[[Amazon.S3.Model.DeleteObjectResponse, AWSSDK.S3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604]].MoveNext()
                       ▷ at Amazon.Runtime.Internal.EndpointDiscoveryHandler.<InvokeAsync>d__2`1[[Amazon.S3.Model.DeleteObjectResponse, AWSSDK.S3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604]].MoveNext()
                       ▷ at Amazon.Runtime.Internal.EndpointDiscoveryHandler.<InvokeAsync>d__2`1[[Amazon.S3.Model.DeleteObjectResponse, AWSSDK.S3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604]].MoveNext()
                     ► HttpErrorResponseException: Exception of type 'Amazon.Runtime.Internal.HttpErrorResponseException' was thrown.
                       ▷ Trace:
                         ▷ at Amazon.Runtime.HttpWebRequestMessage.ProcessHttpResponseMessage(HttpResponseMessage responseMessage)
                         ▷ at Amazon.Runtime.HttpWebRequestMessage.GetResponseAsync(CancellationToken cancellationToken)
                         ▷ at Amazon.Runtime.Internal.HttpHandler`1.<InvokeAsync>d__9`1[[System.Net.Http.HttpContent, System.Net.Http, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a],[Amazon.S3.Model.DeleteObjectResponse, AWSSDK.S3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604]].MoveNext()
                         ▷ at Amazon.Runtime.Internal.RedirectHandler.<InvokeAsync>d__1`1[[Amazon.S3.Model.DeleteObjectResponse, AWSSDK.S3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604]].MoveNext()
                         ▷ at Amazon.Runtime.Internal.Unmarshaller.<InvokeAsync>d__3`1[[Amazon.S3.Model.DeleteObjectResponse, AWSSDK.S3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604]].MoveNext()
                         ▷ at Amazon.S3.Internal.AmazonS3ResponseHandler.<InvokeAsync>d__1`1[[Amazon.S3.Model.DeleteObjectResponse, AWSSDK.S3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604]].MoveNext()
                         ▷ at Amazon.Runtime.Internal.ErrorHandler.<InvokeAsync>d__5`1[[Amazon.S3.Model.DeleteObjectResponse, AWSSDK.S3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604]].MoveNext()

Reproduction Steps

Use the following code in an application, replacing uppercased values.

var bucket = "BUCKET_NAME";
var credentials = new BasicAWSCredentials("ACCESS_KEY", "SECRET_KEY");

using var s3Client = new AmazonS3Client(credentials, BUCKET_REGION);

var deleteObjectRequest = new DeleteObjectRequest {
    BucketName = bucket,
    Key = "OBJECT_ID",
};

try {
    await s3Client.DeleteObjectAsync(deleteObjectRequest);
} catch (Exception e) {
    Console.WriteLine(e.StackTrace);
}

Possible Solution

No response

Additional Information/Context

No response

AWS .NET SDK and/or Package version used

AWSSDK.S3 3.7.305.5

Targeted .NET Platform

.NET8-iOS

Operating System and version

iOS 17

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16 (6 by maintainers)

Most upvoted comments

The way we solved it was along the lines of:

var client = new AmazonS3Client(credentials, new AmazonS3Config {
    HttpClientFactory = new HttpClientFactoryAWS(),
    RegionEndpoint = RegionEndpoint.GetBySystemName(regionResponse.Location)
});


// Factory implementation
private sealed class HttpClientFactoryAWS : HttpClientFactory {

    public override HttpClient CreateHttpClient(IClientConfig clientConfig) {
        var client = new HttpClient();

        client.DefaultRequestHeaders.CacheControl ??= new CacheControlHeaderValue();
        client.DefaultRequestHeaders.CacheControl.NoCache = true;
        client.DefaultRequestHeaders.IfModifiedSince = null;

        return client;
    }

}

This of course also works on .NET 9, but I have not tested whether it is still necessary.

I believe the issue is that If-Modified-Since header is not supported in PUT and DELETE HTTP requests to S3. Makes sense, If-Modified-Since should only really cache GET requests right? Caching a delete or a put doesn’t make sense. It doesn’t look like the AWS SDK puts that header in there if you inspect the headers at runtime. Instead, here is what I think is happening:

Access a file via GET. That will cause the HttpClient used in the AWS SDK to retrieve the file. If the file exists, the underlying HTTP client “remembers” that URL. So if you call the same Path, with a different VERB (GET or DELETE), somehow the underlying Http Client in iOS sees that its asked for that file, and sends the If-Modified-Since header.

So deleting a file you haven’t accessed yet, or PUTing a file that doesn’t already exist, does not cause this issue.

Thank you for sharing the requests, I couldn’t find anything that might cause this issue from the SDK side. I’ve reached out to S3 with your request to figure out the root cause, will let you know when then respond.