Creating a signed URL with AWS SDK v3

I used version 3 of the Amazon Web Services PHP SDK after being a long-time user of version 2.

Previously, you used to be able create a signed URL for an S3 object by specifying an expiry time as the third parameter for the getObjectUrl() method. However, this has changed in version 3 of the SDK.

Now, the code to get a pre-signed URL looks like this:

// Create S3 client and assign it to $client variable…

$command = $client->getCommand('GetObject', [
    'Bucket' => 'your-bucket',
    'Key' => 'your-object-key'
]);

$request = $client->createPresignedRequest($command, '+10 minutes');

$presignedUrl = (string) $request->getUri();

It’s a bit more verbose, but hopefully this helps some one else after it had me stumped for a short while.