事件
此扩展包中可用的事件有 PRE_RESOLVE
和 POST_RESOLVE
。两者都接收 CacheResolveEvent 作为事件。
PRE_RESOLVE
在生成缓存过滤图像的 URL 之前调用。
POST_RESOLVE
在生成缓存过滤图像的 URL 之后调用。
示例:签名 URL
这是一个关于用户媒体与不同过滤器和 IONOS 的 S3 的实现示例。最后,我们将更新 URL 以获取对私有资源的临时签名 URL
首先,我们需要配置适配器和将加载图像的文件系统。通常不需要配置缓存图像的存储位置,但下一步我们将需要它来生成签名 URL。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#oneup_flysystem.yaml
oneup_flysystem:
adapters:
# Original image addapter
user_adapter:
awss3v3:
client: Aws\S3\S3Client
bucket: '%env(IONOS_S3_BUCKET_NAME)%'
prefix: "users" # Original image location
# One adapter per filter and the location of the generated images, with the cache_prefix
user_thumbnail_adapter:
awss3v3:
client: Aws\S3\S3Client
bucket: '%env(IONOS_S3_BUCKET_NAME)%'
prefix: "cache/user_thumbnail"
user_medium_adapter:
awss3v3:
client: Aws\S3\S3Client
bucket: '%env(IONOS_S3_BUCKET_NAME)%'
prefix: "cache/user_medium"
filesystems:
user:
adapter: user_adapter
mount: user
userThumbnail:
adapter: user_thumbnail_adapter
mount: userThumbnail
userMedium:
adapter: user_medium_adapter
mount: userMedium
要使缓存资源成为私有的,我们需要将解析器的 acl 配置为私有,否则生成的图像将是公开的,这不是我们在此示例中想要的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
liip_imagine:
driver: "gd"
loaders:
user_loader:
flysystem:
filesystem_service: oneup_flysystem.user_filesystem
resolvers:
aws_s3_resolver:
aws_s3:
bucket: '%env(IONOS_S3_BUCKET_NAME)%'
client_config:
credentials:
key: '%env(IONOS_S3_ACCESS_ID)%'
secret: '%env(IONOS_S3_ACCESS_SECRET)%'
endpoint: '%env(IONOS_S3_ENDPOINT)%'
region: '%env(IONOS_S3_REGION)%'
version: '%env(IONOS_S3_VERSION)%'
acl: private
cache_prefix: cache
get_options:
Scheme: 'https'
put_options:
CacheControl: 'max-age=86400'
cache: aws_s3_resolver
filter_sets:
cache: ~
user_thumbnail:
cache: aws_s3_resolver
quality: 75
filters:
thumbnail: { size: [ 130, 130 ], mode: outbound }
data_loader: user_loader
user_medium:
cache: aws_s3_resolver
quality: 75
filters:
thumbnail: { size: [ 302, 180 ], mode: outbound }
data_loader: user_loader
最后,我们创建一个 post resolve 订阅器来更新到私有资源位置的 URL。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
namespace App\EventSubscriber;
use App\Enum\MediaFilterEnum;
use App\Repository\MediaRepository;
use League\Flysystem\FilesystemOperator;
use Liip\ImagineBundle\Events\CacheResolveEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class LiipImagineFilterSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly FilesystemOperator $userThumbnailFilesystem,
private readonly FilesystemOperator $userMediumFilesystem
)
{
}
public function onPostResolve(CacheResolveEvent $event): void
{
$path = $event->getPath();
$filter = $event->getFilter();
$date = new \DateTime();
// We set the expiration in 10 minutes for example.
$date = $date->add(new \DateInterval('PT10M'));
if ($filter === MediaFilterEnum::USER_THUMBNAIL->value) {
$url = $this->userThumbnailFilesystem->temporaryUrl($path, $date);
}
else if ($filter === MediaFilterEnum::USER_MEDIUM->value) {
$url = $this->userMediumFilesystem->temporaryUrl($path, $date);
}
if (isset($url)) {
$event->setUrl($url);
}
}
public static function getSubscribedEvents(): array
{
return [
'liip_imagine.post_resolve' => 'onPostResolve'
];
}
}
现在,您将获得一个正确的签名 URL 来获取您的私有资源。
本作品,包括代码示例,根据 Creative Commons BY-SA 3.0 许可协议获得许可。