简体   繁体   中英

Laravel Media Conversions not being created with laravel-medialibrary on Amazon S3

I'm using Spatie/Laravel-Medialibrary to handle file attachments to my Product model. I'd like to have all product images upload to Amazon S3 and then have each image converted to different sizes automatically (ie "thumb", "small", "medium", "large"), as documented here .

Everything works great when working with the local filesystem. The package creates the conversions and I'm able to access them with my app. The problem comes when I try to change the package's settings to use Amazon S3 instead of the local file storage.

So in my ImageController.php , I have the following line:

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

S3 config in config/filesystems.php

'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'
    ],

and the conversion settings located in my Product.php class:

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');
}

As I mentioned above, everything works great if I use the local file system, but as soon as I change it to use Amazon S3, the file conversions are not created. The original file is successfully uploaded to Amazon, but the conversions are not there. Any advice?

Because the Conversion work as a "queue", your server do not setup the queue.

Reading more Laravel document https://laravel.com/docs/5.7/queues

I don't know why but when add nonQueued().It's working for me.

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();
}

Probably you have not started the queue worker with php artisan queue:work or php artisan queue:listen . As well confirm that you have properly set queue config (like QUEUE_CONNECTION ) in the .env file.

This is an old question but I found the problem is when you have an error in your queue or your queue is not running for some reason. Making sure your queue is working properly resolves this issue.

After adding noQueued. Now it is working for me as well.

<?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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM