AWS S3 - How to fix 'The request signature we calculated does not match the signature' error?

后端 未结 30 2680
谎友^
谎友^ 2020-11-29 08:05

I have searched on the web for over two days now, and probably have looked through most of the online documented scenarios and workarounds, but nothing worked for me so far.

30条回答
  •  醉话见心
    2020-11-29 08:43

    In a previous version of the aws-php-sdk, prior to the deprecation of the S3Client::factory() method, you were allowed to place part of the file path, or Key as it is called in the S3Client->putObject() parameters, on the bucket parameter. I had a file manager in production use, using the v2 SDK. Since the factory method still worked, I did not revisit this module after updating to ~3.70.0. Today I spent the better part of two hours debugging why I had started receiving this error, and it ended up being due to the parameters I was passing (which used to work):

    $s3Client = new S3Client([
        'profile' => 'default',
        'region' => 'us-east-1',
        'version' => '2006-03-01'
    ]);
    $result = $s3Client->putObject([
        'Bucket' => 'awesomecatpictures/catsinhats',
        'Key' => 'whitecats/white_cat_in_hat1.png',
        'SourceFile' => '/tmp/asdf1234'
    ]);
    

    I had to move the catsinhats portion of my bucket/key path to the Key parameter, like so:

    $s3Client = new S3Client([
        'profile' => 'default',
        'region' => 'us-east-1',
        'version' => '2006-03-01'
    ]);
    $result = $s3Client->putObject([
        'Bucket' => 'awesomecatpictures',
        'Key' => 'catsinhats/whitecats/white_cat_in_hat1.png',
        'SourceFile' => '/tmp/asdf1234'
    ]);
    

    What I believe is happening is that the Bucket name is now being URL Encoded. After further inspection of the exact message I was receiving from the SDK, I found this:

    Error executing PutObject on https://s3.amazonaws.com/awesomecatpictures%2Fcatsinhats/whitecats/white_cat_in_hat1.png

    AWS HTTP error: Client error: PUT https://s3.amazonaws.com/awesomecatpictures%2Fcatsinhats/whitecats/white_cat_in_hat1.png resulted in a 403 Forbidden

    This shows that the / I provided to my Bucket parameter has been through urlencode() and is now %2F.

    The way the Signature works is fairly complicated, but the issue boils down to the bucket and key are used to generate the encrypted signature. If they do not match exactly on both the calling client, and within AWS, then the request will be denied with a 403. The error message does actually point out the issue:

    The request signature we calculated does not match the signature you provided. Check your key and signing method.

    So, my Key was wrong because my Bucket was wrong.

提交回复
热议问题