0

我无法使用适用于 PHP 的 AWS 开发工具包创建预签名 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;
        }

它给了我下一个错误-

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

4

2 回答 2

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();
于 2020-01-11T10:02:54.237 回答
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");
于 2015-07-22T17:56:01.713 回答