Failed to send file to presigned URL from lambda function at AWS

0

I am using the attached .Net C# code to send a file from lambda function at AWS to the presigned URL, I got returned statuscode=NotImplemented, I am using AWSSDK.Core 3.7.5.6 for C#.Net.

<Error> <Code>NotImplemented</Code> <Message>A header you provided implies functionality that is not implemented</Message> <Header>Transfer-Encoding</Header> <RequestId>NS6X05PCVBZK6YXS</RequestId> <HostId>uoNYHWdW/BDO3wC+bo+yy66Q3TB+cGXUzN+jpqibPVJSSAAHmwCLtcTKl+Ow4Y1vrLzRaULXCK4=</HostId> </Error>

Presigned URL:

https://paymentsredux-bankfile-largefile-dev-us-east-1.s3.amazonaws.com/9c58eaa6-cd05-491d-8c70-cd6ce83b99b0?X-Amz-Expires=900&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEFkaCXVzLWVhc3QtMSJHMEUCIQDWyskQw%2F0AyhaeBheoscY5q3bZjpyFt7MQqd5XjsPpGQIgH65J8k%2FLPW3UlcPXl4tbgbkUH8UeGBUkp8OE5Mcp2ssq3gMI8v%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FARADGgw5ODgwMDkzODMyMzAiDBER2wL%2F2Lur6IBJziqyA54MIJ860X1tsfyZhK3Wp%2BLg0so5H9zf3b89nNXSq4Mvf9V5huoLLp5PfCfWmtY5Ifd2sgV6OuGa1N%2BI6%2FNrff1MYsKHGFOan9vYzP5NbaOQr7%2FFj2w3oMK6%2FLT04LNZYgZO1F8FbcuThr5AUCg2xu%2F0cz5gifPKxtw%2BaN6lnW9Ei5N69dGVK2UqyuC514iZ6xqKPeZwA%2FTl9iA7ZxNXkAlYWk5BQu%2Ftxx8Cj2FgLgKSDMzCsyK0csG68tBG2fH5UGEZnp0MCfkOJwye%2F%2B4bUXqH3kPXGHiD1hW1toPdxg4BOqVlzKzE0MMpbJjw3TyEo3Q7DgWE2Ga3ekD1Ba35hIBpenW8VHZXYhnButoUYmZSjr4bVEM%2FLCqW5RpZnpMYKbvvcZPY01ngdYTsoo9qw%2FkMHUy9Ui16cmm8PFFm%2Bx1Cl8hN7dgj2TOzV2nCSlQFTVRYB1P0CXABrVuuUBizceK%2BvBq9ExZuB4J1GotFWU4eRxmkW4215ezZpz9BrjpHx2sR6f%2BcALm%2BGR0uwZUg2t002wrrOEFMTaPkReTZOYw9jxGy84NTChcSL5DfX0eimtMFMNay3p8GOp4B5wEJmoZGNCP3QfIIL4Xa5skelsT7jKsyR4UiBVpFcLFtYSiDwPIhfpknGeXaW%2F6GzZNTP2uFQA7MUvupxiyd6iX18REK16LxJ%2BT8OuwVdmqIXRuKt9yI%2F8NeMVfZ1arPtGexzajF3Jrxzh9T5dTJeK%2FlhE8m6WUV0d%2FEymxq2dtU8S%2B8jE9zxPyPc5pR%2FJnRXnYDozpw2nanpZCdq7s%3D&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=ASIA6MCPSYU7A6TCVHSB/20230223/us-east-1/s3/aws4_request&X-Amz-Date=20230223T165033Z&X-Amz-SignedHeaders=host&X-Amz-Signature=3ca18d3c26179d7f351d38a2e0e8d3b0bd23d23a36e450716eb34a2e8ce1d370

========================================================================================

    private async Task SendFiletoPresignedUrl(Stream stream, string presignedURL)
    {
        try
        {
            var client = _factory.CreateClient();
           
            var uri = new Uri(presignedURL);
            var content = new StreamContent(stream);

            HttpRequestMessage request = new HttpRequestMessage
            {
                Method = HttpMethod.Put,
                RequestUri = uri,                
                Content = content
            };

            HttpResponseMessage response = await client.SendAsync(request);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                var errMsg = $"An error occurred at transfering AchFile: StatusCode={response.StatusCode} : ReasonPhrase={response.ReasonPhrase}";

                errMsg += " : Content=" + await response.Content.ReadAsStringAsync();

                errMsg += " : presignedURL=" + presignedURL;

                throw new AchSenderException(errMsg);
            }              
        }
        catch (Exception ex)
        {
            var errMsg = $"An error occurred at transfering AchFile: {ex.Message}";

            errMsg += " : presignedURL=" + presignedURL;

            throw new AchSenderException(errMsg, ex);
        }
    }
Kev98
preguntada hace un año699 visualizaciones
2 Respuestas
0

Warm Greetings,

I understand you are receiving the error "A header you provided implies functionality that is not implemented" when trying to upload a file from Lambda to a pre-signed S3 URL.

This is a 501 Server Error[1]. While this error isn't the most descriptive, there are a few things we can try. These mostly revolve around the request itself.

A few situations that can cause this error: [+] The 'Content-Length' header is missing and should be added in the request. [+] Confirm that the request content is not empty. [+] Convert the Stream to a Buffer (or string) and pass as the body.

I would also recommend updating the AWS SDK.Core version to the latest release in the case that a bug is present with this version. The latest release is 3.7.105.8 [2].

As this error may require further investigation and the sharing of account-information, I recommend creating a technical support case[3] with AWS Premium Support and they will gladly assist further.

For further testing purposes, I will be leaving the example .NET SDK implementation for uploading an object to a pre-signed URL here[4].

I have reviewed the documentation[5] for the .NET SDK and the PutObject method. A workaround to the issue might be to use the AWS SDK and S3 client to send the Stream, without the need for a pre-signed URL. If this is suitable for your use-case, see the example code for using the PutObject method with a Stream:

==========================

// Create a client
AmazonS3Client client = new AmazonS3Client();

// Create a PutObject request
PutObjectRequest request = new PutObjectRequest
{
    BucketName = "SampleBucket",
    Key = "Item1",
};
using (FileStream stream = new FileStream("contents.txt", FileMode.Open))
{
    request.InputStream = stream;

    // Put object
    PutObjectResponse response = client.PutObject(request);
}

==========================

Take care and have a great week ahead!

Resources: [1] https://docs.aws.amazon.com/AmazonS3/latest/API/ErrorResponses.html#ErrorCodeList [2] https://www.nuget.org/packages/AWSSDK.Core/#versions-body-tab [3] https://console.aws.amazon.com/support/home#/case/create [4] https://docs.aws.amazon.com/AmazonS3/latest/userguide/PresignedUrlUploadObject.html#generating-presigned-url [5] https://docs.aws.amazon.com/sdkfornet1/latest/apidocs/html/M_Amazon_S3_AmazonS3_PutObject.htm

AWS
INGENIERO DE SOPORTE
James_H
respondido hace un año
0

Thank you James for your response, we need to use presigned URL for uploading large file with size exceeds 6 MB. Adding the 'Content-Length' header caused another issue "System.NotSupportedException: Specified method is not supported. at System.Net.Http.HttpBaseStream.get_Length(), when the sending file to presigned URL is executed inside the lambda function.

I did create a support case at AWS support center couple weeks ago, working with AWS support engineers online for several times, but so far no solution yet

Thanks Kebin

Kev98
respondido hace un año

No has iniciado sesión. Iniciar sesión para publicar una respuesta.

Una buena respuesta responde claramente a la pregunta, proporciona comentarios constructivos y fomenta el crecimiento profesional en la persona que hace la pregunta.

Pautas para responder preguntas