0

I could not create Presigned Url using AWS SDK for PHP. My code is -

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;
        }

It gives me next error-

Fatal error: Call to undefined method Aws\Command::createPresignedUrl() in /Users/waverley_lv/WaverleySoftware/workspace/fox.php.auto/sites/default/behat-tests/util/S3Utility.php on line 103

3
  • Do var_dump( $s3); after you call $s3 = self::connect();. You might not be getting back the object you're expecting. Commented Jul 20, 2015 at 17:59
  • What version of the AWS SDK are you using? Commented Jul 20, 2015 at 19:38
  • I'm using AWS SDK S3 - Release v2.1.39 Commented Jul 20, 2015 at 21:11

2 Answers 2

5

To force download any file on browser, you can use :

    $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();
Sign up to request clarification or add additional context in comments.

Comments

2

Using s3.0.0 v3 - I did the following to get this to work.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.