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

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


当前回答

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

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

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

其他回答

我在c#中也遇到了同样的问题。事实证明,这个问题来自于restsharp返回身体的方式,当你试图直接访问它。在我们的例子中,它是带有/feeds/2021-06-30/documents端点的主体:

{
    "contentType":"text/xml; charset=UTF-8"
}

问题是当尝试在HashRequestBody方法上的AWSSignerHelper类上签名请求时,您有以下代码:

 public virtual string HashRequestBody(IRestRequest request)
    {
        Parameter body = request.Parameters.FirstOrDefault(parameter => ParameterType.RequestBody.Equals(parameter.Type));
        string value = body != null ? body.Value.ToString() : string.Empty;
        return Utils.ToHex(Utils.Hash(value));
    }

此时body.Value.ToString()的值将是:

{contentType:text/xml; charset=UTF-8}

它缺少了restsharp在发布请求时添加的双引号,但是当你访问这样的值时,它不会给出一个无效的散列,因为该值与发送的值不相同。

我暂时用它替换了代码,它可以工作:

public virtual string HashRequestBody(IRestRequest request)
    {
        Parameter body = request.Parameters.FirstOrDefault(parameter => ParameterType.RequestBody.Equals(parameter.Type));
        string value = body != null ? body.Value.ToString() : string.Empty;
        if (body?.ContentType == "application/json")
        {
            value = Newtonsoft.Json.JsonConvert.SerializeObject(body.Value);
        }
        return Utils.ToHex(Utils.Hash(value));
    }

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

aws configure

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

希望这能有所帮助!

在我的案例中,我使用AWS签名授权方法中的postman进行请求时使用S3(大写)作为服务名

我有同样的问题时,试图复制一个对象与一些UTF8字符。下面是一个JS的例子:

var s3 = new AWS.S3();

s3.copyObject({
    Bucket: 'somebucket',
    CopySource: 'path/to/Weird_file_name_ðÓpíu.jpg',
    Key: 'destination/key.jpg',
    ACL: 'authenticated-read'
}, cb);

通过使用encodeURIComponent()对CopySource进行编码来解决

我必须设置

Aws.config.update({
  credentials: Aws::Credentials.new(access_key_id, secret_access_key)
})

在使用ruby aws SDK v2之前(在其他语言中可能也有类似的东西)