Skip to main content
 首页 » 编程设计

php之Amazon S3之如何修复 'The request signature we calculated does not match the signature' 错误

2024年04月17日608wayfarer

我已经在网上搜索了两天多,并且可能浏览了大多数在线记录的场景和解决方法,但到目前为止没有任何效果。

我使用的是在 PHP 5.3 上运行的 PHP V2.8.7 的 AWS SDK

我尝试使用以下代码连接到我的 Amazon S3 存储桶:

// Create a `Aws` object using a configuration file 
$aws = Aws::factory('config.php'); 
 
// Get the client from the service locator by namespace 
$s3Client = $aws->get('s3'); 
 
$bucket = "xxx"; 
$keyname = "xxx"; 
 
try { 
    $result = $s3Client->putObject(array( 
        'Bucket' => $bucket, 
        'Key' => $keyname, 
        'Body' => 'Hello World!' 
    )); 
 
    $file_error = false; 
} catch (Exception $e) { 
    $file_error = true; 
 
    echo $e->getMessage(); 
 
    die(); 
} 

我的config.php文件如下:

return [ 
    // Bootstrap the configuration file with AWS specific features 
    'includes' => ['_aws'], 
    'services' => [ 
        // All AWS clients extend from 'default_settings'. Here we are 
        // overriding 'default_settings' with our default credentials and 
        // providing a default region setting. 
        'default_settings' => [ 
            'params' => [ 
                'credentials' => [ 
                    'key'    => 'key', 
                    'secret' => 'secret' 
                ] 
            ] 
        ] 
    ] 
]; 

它产生以下错误:

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

我已经检查了我的访问 key 和 secret 至少 20 次,生成了新的,使用不同的方法传递信息(即配置文件并在代码中包含凭据),但目前没有任何效果。

请您参考如下方法:

经过两天的调试,终于发现了问题所在...

我分配给对象的键以句点开头,即..\images\ABC.jpg,这导致了错误的发生。

我希望 API 提供更有意义且相关的错误消息,唉,我希望这能对其他人有所帮助!