【问题标题】:Laravel Media Conversions not being created with laravel-medialibrary on Amazon S3未在 Amazon S3 上使用 laravel-medialibrary 创建 Laravel 媒体转换
【发布时间】:2021-12-08 14:34:11
【问题描述】:

我正在使用Spatie/Laravel-Medialibrary 处理我的产品模型的文件附件。我想将所有产品图片上传到 Amazon S3,然后将每张图片自动转换为不同的尺寸(即“拇指”、“小”、“中”、“大”),如 documented here

使用本地文件系统时一切正常。该包创建转换,我可以使用我的应用程序访问它们。当我尝试将包的设置更改为使用 Amazon S3 而不是本地文件存储时,问题就出现了。

所以在我的ImageController.php 中,我有以下行:

$product->addMedia(public_path() . '/temp/products/'.$product->id)->toCollection('images', 's3');

config/filesystems.php 中的 S3 配置

's3' => [
        'driver' => 's3',
        'key' => env('AMAZON_S3_ID'),
        'secret' => env('AMAZON_S3_KEY'),
        'region' => env('AMAZON_S3_REGION'),
        'bucket' => env('AMAZON_S3_BUCKET'),
        'visibility' => 'public'
    ],

以及位于我的Product.php 类中的转换设置:

public function registerMediaConversions()
{
    $this->addMediaConversion('thumb')
         ->setManipulations(['w' => 160, 'h' => 120])
         ->performOnCollections('images');

    $this->addMediaConversion('small')
         ->setManipulations(['w' => 280, 'h' => 210])
         ->performOnCollections('images');

    $this->addMediaConversion('medium')
         ->setManipulations(['w' => 400, 'h' => 300])
         ->performOnCollections('images');

    $this->addMediaConversion('large')
         ->setManipulations(['w' => 640, 'h' => 480])
         ->performOnCollections('images');
}

正如我上面提到的,如果我使用本地文件系统,一切都会很好,但是一旦我将其更改为使用 Amazon S3,就不会创建文件转换。原始文件已成功上传到亚马逊,但没有转换。有什么建议吗?

【问题讨论】:

    标签: php amazon-s3 laravel-5.2


    【解决方案1】:

    我不知道为什么,但是当添加 nonQueued() 时。它对我有用。

    public function registerMediaConversions()
    {
        $this->addMediaConversion('thumb')
             ->setManipulations(['w' => 160, 'h' => 120])
             ->performOnCollections('images')
             ->nonQueued();
    
        $this->addMediaConversion('small')
             ->setManipulations(['w' => 280, 'h' => 210])
             ->performOnCollections('images')
             ->nonQueued();
    
        $this->addMediaConversion('medium')
             ->setManipulations(['w' => 400, 'h' => 300])
             ->performOnCollections('images')
             ->nonQueued();
    
        $this->addMediaConversion('large')
             ->setManipulations(['w' => 640, 'h' => 480])
             ->performOnCollections('images')
             ->nonQueued();
    }
    

    【讨论】:

      【解决方案2】:

      由于转换作为“队列”工作,您的服务器不会设置队列。

      阅读更多 Laravel 文档https://laravel.com/docs/5.7/queues

      【讨论】:

        【解决方案3】:

        您可能没有使用php artisan queue:workphp artisan queue:listen 启动队列工作程序。同时确认您已在.env 文件中正确设置队列配置(如QUEUE_CONNECTION)。

        【讨论】:

          【解决方案4】:

          这是一个老问题,但我发现问题是当您的队列中有错误或您的队列由于某种原因没有运行时。确保您的队列正常工作可以解决此问题。

          【讨论】:

            【解决方案5】:

            添加 noQueued 后。现在它也对我有用。

            <?php
            use Illuminate\Database\Eloquent\Model;
            use Spatie\Image\Manipulations;
            use Spatie\MediaLibrary\HasMedia;
            use Spatie\MediaLibrary\InteractsWithMedia;
            use Spatie\MediaLibrary\MediaCollections\Models\Media;
            
            
            class Post extends Model implements HasMedia
            {
                use InteractsWithMedia;
            
                protected $guarded= [];
            
                // for media library
                const MEDIA_COLLECTION_NAME = 'post_featured_image';
                const MEDIA_CONVERSION_NAME = 'ckthumb';
            
                public function registerMediaConversions(Media $media = null): void
                {
                    $this
                        ->addMediaConversion(self::MEDIA_CONVERSION_NAME)
                        ->fit(Manipulations::FIT_CROP, 300, 300)
                        ->nonQueued();
                }
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-10-24
              • 2017-02-26
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多