到目前为止,我已经在网上搜索了两天多,可能已经浏览了大部分在线记录的场景和变通方法,但到目前为止,没有一个对我有用。

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

我试图连接到我的亚马逊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'
                ]
            ]
        ]
    ]
];

它产生以下错误:

我们计算的请求签名与您提供的签名不匹配。检查您的密钥和签名方法。

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


当前回答

我通过在AWS.S3()中添加apiVersion解决了这个问题,然后它可以完美地为S3签名url工作。

改变

var s3 = new AWS.S3();

to

var s3 = new AWS.S3({apiVersion: '2006-03-01'});

有关更详细的示例,可以参考此AWS Doc SDK示例: https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javascript/example_code/s3/s3_getsignedurl.js

其他回答

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'
]);

我必须将我的bucket/key路径中的catsinhats部分移动到key参数中,如下所示:

$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'
]);

我相信正在发生的是,桶名现在是URL编码。在进一步检查我从SDK接收到的确切消息后,我发现:

在https://s3.amazonaws.com/awesomecatpictures%2Fcatsinhats/whitecats/white_cat_in_hat1.png上执行PutObject错误

AWS HTTP错误:客户机错误: PUT https://s3.amazonaws.com/awesomecatpictures%2Fcatsinhats/whitecats/white_cat_in_hat1.png导致403 Forbidden

这表明提供给Bucket参数的/ I已经通过urlencode(),现在是%2F。

签名的工作方式相当复杂,但问题可以归结为用于生成加密签名的桶和密钥。如果它们在调用客户端和AWS中都不完全匹配,则请求将被拒绝,并返回403。错误信息确实指出了问题:

我们计算的请求签名与你的签名不匹配 提供。检查您的密钥和签名方法。

所以,我的钥匙错了,因为我的桶错了。

实际上在Java中,我得到了相同的错误。在花了4个小时来调试它之后,我发现问题是在S3对象中的元数据中,因为在S3文件中坐缓存控件时有空间。这个空间在1.6中是允许的。*版本,但在1.11。*不允许,因此抛出签名不匹配错误

在我的案例中,问题是用于配置Amplify的API网关URL在结尾有一个额外的斜杠…

查询的url看起来像https://....amazonaws.com/myapi//myendpoint。我删除了额外的斜杠在conf和它工作。

这不是我人生中最明确的错误信息。

我不知道是否有人在尝试测试浏览器中输出的URL时遇到了这个问题,但如果你使用Postman并试图从RAW选项卡复制AWS的生成URL,因为转义反斜杠,你会得到上面的错误。

使用Pretty选项卡复制并粘贴url,看看它是否真的有效。

我最近遇到了这个问题,这个解决方案解决了我的问题。这是为了测试,看看是否真的通过url检索了数据。

这个答案是对那些试图从AWS生成下载、临时链接或通常从AWS生成URL来使用的人的参考。

奇怪的是,我以前有一个错误,你提供的授权机制是不支持的。请使用AWS4-HMAC-SHA256。在Stackoverflow上有一个答案,需要添加AWS_S3_REGION_NAME = 'eu-west-2'(您的地区),AWS_S3_SIGNATURE_VERSION = "s3v4. net "。

这样做之后,以前的错误清除,但我最终再次出现这个签名错误。寻找答案,直到我最终删除了AWS_S3_SIGNATURE_VERSION = "s3v4,然后它工作了。放在这里也许能帮到别人。我用的是Django。