Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS S3 getting presigned URL Guzzle "argument must be of type array, object given" when it's an array

I don't understand why this doesn't work, and I have scoured the Internet and can't find anything matching my specific command I'm using.

I am basically trying to generate a presigned URL from Amazon S3 and I am following the directions in the docs to a T and it's not working.. Actually not to a T, I was doing it to a T. The docs say to make the array like this : [ 'Key' => 'Value' ] ... I saw another question here where the solved answer was to make it using array() .... but it doesn't change anything.

It still gives this error:

[01-Jan-2016 13:28:56 America/Los_Angeles] PHP Catchable fatal error: Argument 2 passed to Guzzle\Service\Client::getCommand() must be of the type array, object given, called in /Users/alex/Development/theshrineofdionysus-com/vendor/guzzle/guzzle/src/Guzzle/Service/Client.php on line 76 and defined in /Users/alex/Development/theshrineofdionysus-com/vendor/guzzle/guzzle/src/Guzzle/Service/Client.php on line 79

This is the code I am using related to the S3 part of it. Trust me when I saw the constants regarding the keys, region and bucket are correct, as I have other S3 code using them elsewhere that works flawlessly.

<?php
$s3 = Aws\S3\S3Client::factory(array(
  'key'    => AWS_ACCESS_KEY,
  'secret' => AWS_SECRET_KEY,
  'region' => AWS_REGION,
));
$cmd = $s3->getCommand('GetObject', array(
  'Bucket' => AWS_BUCKET,
  'Key' => $row['video_id']
));
$request = $s3->createPresignedRequest($cmd, '+120 minutes');
$url = (string) $request->getUri();
?>

I also know that $row['video_id'] is equal to an existing filename because without this code there, and I'm echoing it out it is the correct filename.

Here is my composer.json:

{
    "require": {
        "aws/aws-sdk-php": "2.*",
        "php": ">=5.2.0"
    }
}

This is my amazon code on the other page that works fine:

$s3 = Aws\S3\S3Client::factory(array(
  'key'    => AWS_ACCESS_KEY,
  'secret' => AWS_SECRET_KEY,
  'region' => AWS_REGION
));
$objects = $s3->getIterator('ListObjects', array('Bucket' => AWS_BUCKET));
foreach ($objects as $object) {
  echo '<option value="' . $object['Key'] . '">' . $object['Key'] . '</option>' . PHP_EOL;
}
like image 913
Rose Nation Avatar asked Jan 01 '16 21:01

Rose Nation


1 Answers

It looks like you're following the guide for v3 but have v2 installed. You can create a presigned URL in v2 by calling: $url = $s3->getObjectUrl(AWS_BUCKET, $row['video_id'], '+120 minutes');

A full guide can be found here.

like image 84
giaour Avatar answered Nov 04 '22 13:11

giaour