Amazon S3 PHP 开发工具包预签名请求


Amazon S3 PHP SDK pre-signed requests

我正在尝试使用 S3 PHP SDK 生成一个预签名请求,如下所示:

$s3Client = Aws'S3'S3Client::factory([
    'credentials' => new Aws'Common'Credentials'Credentials('my-access-code', 'xxx')
]);
$command = $s3Client->getCommand('GetObject', [
    'Bucket' => 'my-bucket-name',
    'Key' => 'awesome-cat-image.png',
]);
$signedUrl = $command->createPresignedUrl('+10 minutes');

但是当我转到 URL 时,我收到一条错误消息:

<Error>
    <Code>SignatureDoesNotMatch</Code>
    <Message>
        The request signature we calculated does not match the signature you provided. Check your key and signing method.
    </Message>
    <AWSAccessKeyId>xxx</AWSAccessKeyId>
    <StringToSign>GET 1422564095 /my-bucket-name/awesome-cat-image.png</StringToSign>
    <SignatureProvided>xxx</SignatureProvided>
    <StringToSignBytes>
        xxx
    </StringToSignBytes>
    <RequestId>xxx</RequestId>
    <HostId>
        xxx
    </HostId>
</Error>

访问 https://my-bucket-name.s3.amazonaws.com/awesome-cat-image.png 在权限设置为允许未经身份验证的用户的情况下工作正常。

$cmd = $s3Client->getCommand('GetObject', [
    'Bucket' => 'my-bucket',
    'Key'    => 'testKey'
]);
$request = $s3Client->createPresignedRequest($cmd, '+20 minutes');
// Get the actual presigned-url
$presignedUrl = (string) $request->getUri();

您可以在此处找到更多详细信息 http://docs.aws.amazon.com/aws-sdk-php/v3/guide/service/s3-presigned-url.html