使用AWS SDK for PHP调用未定义的方法createPresignedUrl()

时间:2015-07-20 17:56:11

标签: php amazon-s3 pre-signed-url

我无法使用AWS SDK for PHP创建Presigned Url。我的代码是 -

function connect()
    {
    // Instantiate the S3 class and point it at the desired host
    date_default_timezone_set('GMT');
    return S3Client::factory(array(
    'region'  => 'us-west-2',
    'version' => 'latest',
    'credentials' => [
            'key'    => $key,
            'secret' => $secret
        ]

    ));

 function getSignedS3URLForObject($fileName)
        {
            // GET CURRENT DATE
            $milliseconds = round(microtime(true) * 1000);
            $expiration = $milliseconds + (1000 * 60 * 60 * 24 * 30 * 2);
            $s3 = self::connect();
            $command = $s3->getCommand('GetObject', array(
                'Bucket'      => self::$customerBucket,
                'Key'         => $fileName,
                'ContentType' => 'image/jpeg',
                'Body'        => '',
                'ContentMD5'  => false
            ));
            $signedUrl = $command->createPresignedUrl($expiration);
            echo urldecode($signedUrl);
            return $signedUrl;
        }

它给了我下一个错误 -

  

致命错误:调用未定义的方法   Aws \ Command :: createPresignedUrl()in   /Users/waverley_lv/WaverleySoftware/workspace/fox.php.auto/sites/default/behat-tests/util/S3Utility.php   在第103行

2 个答案:

答案 0 :(得分:4)

要强制在浏览器上下载任何文件,可以使用:

    $command = $s3->getCommand('GetObject', array(
        'Bucket' => 'bucketname',
        'Key' => 'filename',
        'ResponseContentType' => 'application/octet-stream',
        'ResponseContentDisposition' => 'attachment'
    ));

    // Create a signed URL from the command object that will last for
    // 2 minutes from the current time
    $response = $s3->createPresignedRequest($command, '+2 minutes');
    $presignedUrl = (string)$response->getUri();

答案 1 :(得分:2)

使用s3.0.0 v3 - 我做了以下工作以使其工作。

$command = $s3->getCommand('GetObject', array(
            'Bucket'      => $this->customerBucket,
            'Key'         => $fileName,
            'ContentType' => 'image/png',
            'ResponseContentDisposition' => 'attachment; filename="'.$fileName.'"'
        ));
$signedUrl = $s3->createPresignedRequest($command, "+1 week");