koel/app/Services/S3Service.php

37 lines
1 KiB
PHP
Raw Normal View History

2018-08-29 04:06:17 +00:00
<?php
namespace App\Services;
use App\Models\Song;
use Aws\S3\S3ClientInterface;
use Illuminate\Cache\Repository as Cache;
class S3Service implements ObjectStorageInterface
{
private $s3Client;
private $cache;
2018-08-29 10:18:42 +00:00
public function __construct(?S3ClientInterface $s3Client, Cache $cache)
2018-08-29 04:06:17 +00:00
{
$this->s3Client = $s3Client;
$this->cache = $cache;
}
public function getSongPublicUrl(Song $song): string
{
2019-06-30 14:22:53 +00:00
return $this->cache->remember("OSUrl/{$song->id}", 60, function () use ($song): string {
2018-08-29 04:06:17 +00:00
$cmd = $this->s3Client->getCommand('GetObject', [
'Bucket' => $song->s3_params['bucket'],
'Key' => $song->s3_params['key'],
]);
// Here we specify that the request is valid for 1 hour.
// We'll also cache the public URL for future reuse.
$request = $this->s3Client->createPresignedRequest($cmd, '+1 hour');
$url = (string) $request->getUri();
return $url;
});
}
}