'AWS S3 upload file with pre-signed URL in .NET

I already search on Internet, but no success.

I'm working with C#/.NET 6 and I "simply" have to upload a file to a AWS S3 bucket using a pre-signed URL.

To generate the pre-signed URL, I use the following code:

private static Uri GetPresignedUrl(IAmazonS3 amazonS3, AwsPresignedUrlOptions awsPresignedUrlOptions)
{
    GetPreSignedUrlRequest request = new()
    {
        BucketName = awsPresignedUrlOptions.BucketName,
        Key = awsPresignedUrlOptions.ResourcePath,
        Verb = awsPresignedUrlOptions.HttpVerb,
        Expires = awsPresignedUrlOptions.Expires,
    };

    return new Uri(amazonS3.GetPreSignedURL(request));
}

After that I do the following:

output = GetPresignedUrl(amazonS3, awsPresignedUrlOptions);

using (FileStream tempInFileStream = new(tempFileName.LocalPath, FileMode.Open))
{
    using (HttpClient httpClient = new())
    {
        StreamContent streamContent = new(tempInFileStream);
        HttpResponseMessage response = await httpClient
            .PutAsync(output, streamContent);

        response.EnsureSuccessStatusCode();
    }
}

but the system does absolutely nothing till the Timeout.

If I use the pre-signed URL I calculate and try a PUT in Postman I obtain the following:

<?xml version="1.0" encoding="UTF-8"?>
<Error>
    <Code>AccessDenied</Code>
    <Message>Access Denied</Message>
    <RequestId>A2F70F25SPEMJF8X</RequestId>
    <HostId>V+pbAWrEeLb/xJvPLvkytLoLUEBhyEWv+IV4H/PSLLKV3rLlF7fIntPUONLaLXIow9gUaTkAF/k=</HostId>
</Error>

Any suggestion?

Thank you.



Solution 1:[1]

I spent a lot of time reviewing all the security policies, and the error was right there. If you have the same problem, please check that:

  • The user you are using is still active and has a valid access key and secret key.
  • The user has the correct permissions to access the S3 bucket (I chose "AmazonS3FullAccess" for my tests).

Regards.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Attilio Gelosa