使用PutObject将blob上载到S3存储桶不起作用:SignatureDoesNotMatch。为什么?

时间:2017-12-18 22:34:46

标签: php amazon-web-services amazon-s3 aws-sdk

尝试将图像作为blob上传到S3存储桶失败,并显示以下错误:

<Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calcul (truncated...) 
SignatureDoesNotMatch (client): The request signature we calculated does not match the signature you provided. Check your key and signing method. - <?xml version="1.0" encoding="UTF-8"?>

凭据是从/.aws/credentials文件中提取的,我知道它们有效,因为文件中的putObject可以正常运行。这只是无法上传的blob。

S3类具有以下感兴趣的部分,在这种情况下:

class S3_model extends CI_Model {

private $s3Client;
private $s3Config = [
    'version' => '2006-03-01',      // See `version` parameter documentation here: http://docs.aws.amazon.com/aws-sdk-php/v3/guide/guide/configuration.html
    'region' => 'eu-central-1'
];

private $bucket = 'tprdev';

public function __construct ()
{
    parent::__construct();
    $this->load->model('log_model');
    $this->s3Client = new Aws\S3\S3Client($this->s3Config);
}

public function putObjectFromBlob ($key, $blob)
{
    try {
        $params = [
            'Bucket' => $this->bucket,
            'Key'    => $key,
            'Body'   => $blob
        ];

        $this->s3Client->putObject($params);
        $this->s3Client->waitUntil('ObjectExists', $params);
    } catch (Exception $e) {
        $this->log_model->log("{$e->getMessage()}\n{$e->getTraceAsString()}");
        return false;
    }

    return true;
}
}

1 个答案:

答案 0 :(得分:1)

事实证明,我使用的这个新版本的SDK对于带有前导斜杠的对象键非常繁琐。我已删除了前导斜杠,现在上传了blob。