Bagisto: Using AWS S3 for storage

Created on 6 Jan 2020  路  4Comments  路  Source: bagisto/bagisto

I'm using S3 cloud storage for storing images as it's not a good idea to store files on the server itself.
So I can upload product image successfully to S3 when creating/updating a product, But now I can't get images to show because it's being cached.

In Webkul\Product\Helpers\ProductImage I found that the image url is pointing to /cache/:

public function getProductBaseImage($product)
    {
        $images = $product ? $product->images : null;

        if ($images && $images->count()) {
            $image = [
                'small_image_url' => url('cache/small/' . $images[0]->path),
                'medium_image_url' => url('cache/medium/' . $images[0]->path),
                'large_image_url' => url('cache/large/' . $images[0]->path),
                'original_image_url' => url('cache/original/' . $images[0]->path),
            ];
        } else {
            $image = [
                'small_image_url' => asset('vendor/webkul/ui/assets/images/product/small-product-placeholder.png'),
                'medium_image_url' => asset('vendor/webkul/ui/assets/images/product/meduim-product-placeholder.png'),
                'large_image_url' => asset('vendor/webkul/ui/assets/images/product/large-product-placeholder.png'),
                'original_image_url' => asset('vendor/webkul/ui/assets/images/product/large-product-placeholder.png'),
            ];
        }

        return $image;
    }

Is there a way to store images on S3 and use the cache as well?
Help is much appreciated.

Enhancement

Most helpful comment

Hey! I麓m "dockerizing" an pplication and I had a similar issue and solved it using Redis for cache and a StreamWrapper...

First setup redis.

config/cache.php

'stores' => [
        // ...

        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
        ],
    ],
], //...

config/database.php

// ...
    'redis' => [

        'client' => 'predis',

        'default' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
        ],

    ],
// ...

Then, register a StreamWrapper in app/Providers/AppServiceProvider.php

// ...
use Aws\S3\StreamWrapper;
use Aws\S3\S3Client;

class AppServiceProvider extends ServiceProvider
{
    // ...

    /**
    * Bootstrap any application services.
    *
    * @return void
    */
    public function boot()
    {
        //...

        $options = [
            'credentials' => [
                'key' => env('AWS_ACCESS_KEY_ID'),
                'secret' => env('AWS_SECRET_ACCESS_KEY')
            ],
            //'endpoint' => env('AWS_URL'),
            'region' => env('AWS_DEFAULT_REGION'),
            'version' => '2006-03-01'
        ];

        $s3 = new S3Client($options);
        StreamWrapper::register($s3, 's3');
    }
    //...

And finally add a storage path in config/imagecache.php

//...
/*
|--------------------------------------------------------------------------
| Storage paths
|--------------------------------------------------------------------------
|
| The following paths will be searched for the image filename, submited
| by URI.
|
| Define as many directories as you like.
|
*/

'paths' => array(
    storage_path('app/public'),
    public_path('storage'),
    "s3://<yourbucket-here>"
),
//...

Imagecache search in these paths the images and store in cache.

Well... for all in the same situation I hope it helps. Good coding!

All 4 comments

Hi @MostafaAttia

We have used intervention package for resizing product images into different size and this images are not uploading on s3 bucket so product images are not appearing on shop.

For right now, if you want then you need to write code to upload these resize images on s3 bucket and need to get this images on shop.

We will soon update these things to provide fully s3 bucket support to our system until you can wait.

Hey! I麓m "dockerizing" an pplication and I had a similar issue and solved it using Redis for cache and a StreamWrapper...

First setup redis.

config/cache.php

'stores' => [
        // ...

        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
        ],
    ],
], //...

config/database.php

// ...
    'redis' => [

        'client' => 'predis',

        'default' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
        ],

    ],
// ...

Then, register a StreamWrapper in app/Providers/AppServiceProvider.php

// ...
use Aws\S3\StreamWrapper;
use Aws\S3\S3Client;

class AppServiceProvider extends ServiceProvider
{
    // ...

    /**
    * Bootstrap any application services.
    *
    * @return void
    */
    public function boot()
    {
        //...

        $options = [
            'credentials' => [
                'key' => env('AWS_ACCESS_KEY_ID'),
                'secret' => env('AWS_SECRET_ACCESS_KEY')
            ],
            //'endpoint' => env('AWS_URL'),
            'region' => env('AWS_DEFAULT_REGION'),
            'version' => '2006-03-01'
        ];

        $s3 = new S3Client($options);
        StreamWrapper::register($s3, 's3');
    }
    //...

And finally add a storage path in config/imagecache.php

//...
/*
|--------------------------------------------------------------------------
| Storage paths
|--------------------------------------------------------------------------
|
| The following paths will be searched for the image filename, submited
| by URI.
|
| Define as many directories as you like.
|
*/

'paths' => array(
    storage_path('app/public'),
    public_path('storage'),
    "s3://<yourbucket-here>"
),
//...

Imagecache search in these paths the images and store in cache.

Well... for all in the same situation I hope it helps. Good coding!

I hope that solved you issue. I am closing this issue, feel free to reopen any time.

@jesusnazarethgh estoy con el mismo problema dame una manito Jes煤s ya llevo algunos d铆as tratando de hacer eso solo que mi storage no es Amazon sino Google cloud

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bergstar picture bergstar  路  4Comments

rabol picture rabol  路  3Comments

AkashWeybee picture AkashWeybee  路  3Comments

ludioao picture ludioao  路  6Comments

john7851 picture john7851  路  3Comments