https://storage.yandexcloud.net/bucket-name/folder-name/2571087.zip
https://bucket-name.storage.yandexcloud.net/folder-name/2571087.zip
$s3 = new S3Client([
'version' => 'latest',
'endpoint' => 'https://storage.yandexcloud.net',
'region' => 'ru-central1',
'credentials' => [
'key' => $this->s3Key,
'secret' => $this->s3Secret,
],
]);
$cmd = $s3->getCommand('GetObject', [
'Bucket' => $this->bucketName,
'Key' => $this->directoryPath . '/' . wp_basename($filePath),
]);
$request = $s3->createPresignedRequest($cmd, '+20 minutes');
return (string) $request->getUri();
if ($previousUrl) {
// Invalidate the previous URL by deleting the object
$s3->deleteObject([
'Bucket' => $bucket,
'Key' => $key,
]);
}
$cmd = $s3->getCommand('GetObject', [
'Bucket' => $bucket,
'Key' => $key,
]);
$url = $s3->createPresignedRequest($cmd, '+5 minutes')->getUri();
return $url;
The function then generates a new pre-signed URL as before and returns it to the caller. Because the previous URL has been invalidated by deleting the object, any attempts to use it will result in an error.
Note that this approach can also result in increased S3 API usage, as objects are deleted and recreated each time a file is downloaded. However, it may be a suitable approach in situations where you need to limit access to files to a single download per URL and ensure that the URL cannot be used again after the file has been downloaded.