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

我使用的是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次,生成了新的,使用不同的方法来传递信息(即配置文件和在代码中包含凭据),但目前没有任何工作。


当前回答

由于以下原因,我得到了相同的错误。

我输入了正确的凭据,但复制粘贴。因此,可能会产生垃圾字符插入,而复制粘贴。 我已经手动输入并运行代码,现在它的工作正常

谢谢你!

其他回答

这只是增加了许多不同的表现方式。

如果你在iOS上使用safari,并且连接到safari技术预览控制台,你会看到同样的问题。如果你断开与控制台的连接,问题就会消失。

当然,这使得排除其他问题变得困难,但这是100%的复制。

我试图找出我可以在STP中改变什么来阻止它这样做,但还没有找到它。

当我在~/.aws/credentials的键周围有引号时,我得到了这个。

aws_secret_access_key = "KEY"

如果上面提到的方法都不适合你,那就试试吧

aws configure

这个命令将打开一组选项,要求输入键、区域和输出格式。

希望这能有所帮助!

我在试图复制一个对象时得到这个错误。我通过对copySource进行编码来修复它。这实际上在方法文档中有描述:

参数: copySource -源对象的源桶名和键名,用斜杠(/)分隔。必须是url编码的。

CopyObjectRequest objectRequest = CopyObjectRequest.builder()
                .copySource(URLEncoder.encode(bucket + "/" + oldFileKey, "UTF-8"))
                .destinationBucket(bucket)
                .destinationKey(newFileKey)
                .build();

我通过在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