Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uploading Files to s3 sometimes fails due to Amazon.S3.AmazonS3Exception: 'The request signature we calculated does not match the signature you provided' #3621

Closed
1 task
MhamzaM opened this issue Jan 24, 2025 · 4 comments
Labels
bug This issue is a bug. p3 This is a minor priority issue response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. s3

Comments

@MhamzaM
Copy link

MhamzaM commented Jan 24, 2025

Describe the bug

The below code was working fine until I was using .net AWSSDK.S3 Version="3.7.8.2", after I updated the version to "3.7.411.7" the fileTransferUtility.Upload() function started resulting in unexpected behavior. Sometime the call get successful and sometimes it throws an exception with message "The request signature we calculated does not match the signature you provided. Check your key and signing method".

public static async Task UploadFileToAWS(AWSAppConfig aWSAppConfig, int locationId, string sourceFileKey, string bucketName, MemoryStream file, string path = null)
{
    try
    {
        
        IAmazonS3 s3Client = GetAmazonS3Client(aWSAppConfig);

        var fileTransferUtility = new TransferUtility(s3Client);
        
        if (!await AmazonS3Util.DoesS3BucketExistV2Async(s3Client, bucketName))
        {
            var request = new PutBucketRequest
            {
                BucketName = bucketName,
                UseClientRegion = true,
                ObjectOwnership = ObjectOwnership.ObjectWriter,

            };
            await s3Client.PutBucketAsync(request);
            
            DeletePublicAccessBlockRequest deletePublicAccessBlockRequest = new DeletePublicAccessBlockRequest
            {
                BucketName = bucketName
            };
            await s3Client.DeletePublicAccessBlockAsync(deletePublicAccessBlockRequest);
       
        }

        if(path != null)
        {
            bucketName += "/" + path;
        }

        var fileTransferUtilityRequest = new TransferUtilityUploadRequest
        {
            BucketName = bucketName,
            InputStream = file,
            StorageClass = S3StorageClass.StandardInfrequentAccess,
            PartSize = 6291456, // 6 MB.
            Key = sourceFileKey,
            CannedACL = S3CannedACL.PublicRead
            
        };

        fileTransferUtility.Upload(fileTransferUtilityRequest);                
    }
    catch (Exception)
    {
        throw;
    }
}

Regression Issue

  • Select this option if this issue appears to be a regression.

Expected Behavior

The TransferUtility.Upload should always upload the file instead of throwing exception. I have downgraded the version to the previous one and it's now working flawlessly.

Current Behavior

Amazon.S3.AmazonS3Exception: 'The request signature we calculated does not match the signature you provided. Check your key and signing method.'

Reproduction Steps

public static async Task UploadFileToAWS(AWSAppConfig aWSAppConfig, int locationId, string sourceFileKey, string bucketName, MemoryStream file, string path = null)
{
    try
    {
        
        IAmazonS3 s3Client = GetAmazonS3Client(aWSAppConfig);

        var fileTransferUtility = new TransferUtility(s3Client);
        
        if (!await AmazonS3Util.DoesS3BucketExistV2Async(s3Client, bucketName))
        {
            var request = new PutBucketRequest
            {
                BucketName = bucketName,
                UseClientRegion = true,
                ObjectOwnership = ObjectOwnership.ObjectWriter,

            };
            await s3Client.PutBucketAsync(request);
            
            DeletePublicAccessBlockRequest deletePublicAccessBlockRequest = new DeletePublicAccessBlockRequest
            {
                BucketName = bucketName
            };
            await s3Client.DeletePublicAccessBlockAsync(deletePublicAccessBlockRequest);
       
        }

        if(path != null)
        {
            bucketName += "/" + path;
        }

        var fileTransferUtilityRequest = new TransferUtilityUploadRequest
        {
            BucketName = bucketName,
            InputStream = file,
            StorageClass = S3StorageClass.StandardInfrequentAccess,
            PartSize = 6291456, // 6 MB.
            Key = sourceFileKey,
            CannedACL = S3CannedACL.PublicRead
            
        };

        fileTransferUtility.Upload(fileTransferUtilityRequest);                
    }
    catch (Exception)
    {
        throw;
    }
}

Possible Solution

No response

Additional Information/Context

No response

AWS .NET SDK and/or Package version used

AWSSDK.S3 Version="3.7.411.7"

Targeted .NET Platform

.net 8

Operating System and version

windows 11

@MhamzaM MhamzaM added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jan 24, 2025
@github-actions github-actions bot added the potential-regression Marking this issue as a potential regression to be checked by team member label Jan 24, 2025
@dscpinheiro
Copy link
Contributor

dscpinheiro commented Jan 24, 2025

Are you using S3 or a 3rd-party implementation? We made a change last week so that the SDK automatically calculates checksums for all upload calls: #3610 (here's a similar report - where they were using MinIO: #3615)

Ignore the above message, the error you're seeing is not related to the change we made last week. Starting in version 3.7.100 of AWSSDK.S3, you cannot use part of the key prefix in the bucket name (see #2622 (comment) for more details).

To fix your problem, you'll need to change your code to:

if (path != null)
{
    sourceFileKey = path + "/" + sourceFileKey;
}

var fileTransferUtilityRequest = new TransferUtilityUploadRequest
{
    BucketName = bucketName,
    InputStream = file,
    StorageClass = S3StorageClass.StandardInfrequentAccess,
    PartSize = 6291456, // 6 MB.
    Key = sourceFileKey,
    CannedACL = S3CannedACL.PublicRead
};

@dscpinheiro dscpinheiro added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed needs-triage This issue or PR still needs to be triaged. potential-regression Marking this issue as a potential regression to be checked by team member labels Jan 24, 2025
@github-actions github-actions bot added potential-regression Marking this issue as a potential regression to be checked by team member and removed potential-regression Marking this issue as a potential regression to be checked by team member labels Jan 24, 2025
@dscpinheiro dscpinheiro changed the title Uploading Files to s3 sometimes fails due to Amazon.S3.AmazonS3Exception: 'The request signature we calculated does not match the signature you provided. Check your key and signing method.' Uploading Files to s3 sometimes fails due to Amazon.S3.AmazonS3Exception: 'The request signature we calculated does not match the signature you provided' Jan 24, 2025
@MhamzaM
Copy link
Author

MhamzaM commented Jan 24, 2025

@dscpinheiro Where is this change documented? I couldn't find it.

@dscpinheiro
Copy link
Contributor

dscpinheiro commented Jan 24, 2025

No, there are more details in the comment I linked but the previous behavior worked by accident.

In retrospect we should've communicated it better, but the current version of the S3 package is working as expected.

@ashishdhingra ashishdhingra added s3 p3 This is a minor priority issue labels Jan 24, 2025
Copy link

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. p3 This is a minor priority issue response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. s3
Projects
None yet
Development

No branches or pull requests

3 participants