Dhaval Chheda
Dhaval Chheda

Reputation: 5147

setting expiration on s3 signed Url

I am trying to set expiration to the url but for some reason it is not working as expected and the url stays alive even after the expiration is passed

php code

$object = 'uploads/496c53309bac48e4d65f55d9d66c0ac0.txt';
$url = $s3->getObjectUrl($config['s3']['bucket'], $object, '10 seconds');

html code

<body>

<a href="<?php echo $url; ?>">Download Link</a>

</body>

I am using AWS SDK 2.7.5

Upvotes: 2

Views: 987

Answers (1)

Steve E.
Steve E.

Reputation: 9343

'getObjectUrl' will only create a regular S3 url and it only takes two arguments.

To use presigned URLs, requires a little more as per the SDK documentation

// 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!

Upvotes: 2

Related Questions