Installation guide

The most common scenario is creating a pre-signed URL to GET an object. The easiest way to do this is to use the
getObjectUrl method of the Amazon S3 client. This same method can also be used to get an unsigned URL of a
public S3 object.
// Get a plain URL for an Amazon S3 object
$plainUrl = $client->getObjectUrl($bucket, 'data.txt');
// > https://my-bucket.s3.amazonaws.com/data.txt
// Get a pre-signed URL for an Amazon S3 object
$signedUrl = $client->getObjectUrl($bucket, 'data.txt', '+10 minutes');
// > https://my-bucket.s3.amazonaws.com/data.txt?AWSAccessKeyId=[...]&Expires=[...]&Signature=[...]
// Create a vanilla Guzzle HTTP client for accessing the URLs
$http = new \Guzzle\Http\Client;
// Try to get the plain URL. This should result in a 403 since the object is private
try {
$response = $http->get($plainUrl)->send();
} catch (\Guzzle\Http\Exception\ClientErrorResponseException $e) {
$response = $e->getResponse();
}
echo $response->getStatusCode();
// > 403
// Get the contents of the object using the pre-signed URL
$response = $http->get($signedUrl)->send();
echo $response->getBody();
// > Hello!
You can also create pre-signed URLs for any Amazon S3 operation using the getCommand method for creating a
Guzzle command object and then calling the createPresignedUrl() method on the command.
// Get a command object from the client and pass in any options
// available in the GetObject command (e.g. ResponseContentDisposition)
$command = $client->getCommand('GetObject', array(
'Bucket' => $bucket,
'Key' => 'data.txt',
'ResponseContentDisposition' => 'attachment; filename="data.txt"'
));
// Create a signed URL from the command object that will last for
// 10 minutes from the current time
$signedUrl = $command->createPresignedUrl('+10 minutes');
echo file_get_contents($signedUrl);
// > Hello!
If you need more flexibility in creating your pre-signed URL, then you can create a pre-signed URL for a completely
custom Guzzle\Http\Message\RequestInterface object. You can use the get(), post(), head(),
put(), and delete() methods of a client object to easily create a Guzzle request object.
$key = 'data.txt';
$url = "{$bucket}/{$key}";
// get() returns a Guzzle\Http\Message\Request object
$request = $client->get($url);
// Create a signed URL from a completely custom HTTP request that
// will last for 10 minutes from the current time
$signedUrl = $client->createPresignedUrl($request, '+10 minutes');
Amazon Simple Storage Service
108