You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Recently I needed to use multiple s3 buckets at the same time with media library and I had a problem with generating the correct urls depending on the bucket used. I ended up creating a custom s3 url generator which is simple enough.. I was thinking if the changes can be merged back into the package. here is the generator:-
<?php
namespace App\Generator;
use DateTimeInterface;
use Illuminate\Contracts\Config\Repository as Config;
use Illuminate\Filesystem\FilesystemManager;
use Spatie\MediaLibrary\UrlGenerator\BaseUrlGenerator;
class CustomS3MediaUrlGenerator extends BaseUrlGenerator
{
/** @var \Illuminate\Filesystem\FilesystemManager */
protected $filesystemManager;
public function __construct(Config $config, FilesystemManager $filesystemManager)
{
$this->filesystemManager = $filesystemManager;
parent::__construct($config);
}
/**
* Get the url for a media item.
*
* @return string
*/
public function getUrl(): string
{
$url = $this->getPathRelativeToRoot();
if ($root = config('filesystems.disks.'.$this->media->disk.'.root')) {
$url = $root.'/'.$url;
}
$url = $this->rawUrlEncodeFilename($url);
$url = $this->versionUrl($url);
$domain = 'https://'. config('filesystems.disks.'.$this->media->disk.'.bucket') .'.s3.amazonaws.com';
return $domain . '/'. $url;
}
/**
* Get the temporary url for a media item.
*
* @param \DateTimeInterface $expiration
* @param array $options
*
* @return string
*/
public function getTemporaryUrl(DateTimeInterface $expiration, array $options = []): string
{
return $this
->filesystemManager
->disk($this->media->disk)
->temporaryUrl($this->getPath(), $expiration, $options);
}
/**
* Get the url for the profile of a media item.
*
* @return string
*/
public function getPath(): string
{
return $this->getPathRelativeToRoot();
}
/**
* Get the url to the directory containing responsive images.
*
* @return string
*/
public function getResponsiveImagesDirectoryUrl(): string
{
$url = $this->pathGenerator->getPathForResponsiveImages($this->media);
if ($root = config('filesystems.disks.'.$this->media->disk.'.root')) {
$url = $root.'/'.$url;
}
$domain = 'https://'. config('filesystems.disks.'.$this->media->disk.'.bucket') .'.s3.amazonaws.com';
return $domain . '/' . $url;
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Recently I needed to use multiple s3 buckets at the same time with media library and I had a problem with generating the correct urls depending on the bucket used. I ended up creating a custom s3 url generator which is simple enough.. I was thinking if the changes can be merged back into the package. here is the generator:-
Feedback is welcome on this. 😎
Beta Was this translation helpful? Give feedback.
All reactions