Store Image files publicly in S3 compatible storage with Laravel Nova

I’m using an Image field in Nova for the first time and found that previews were not displaying.

I am using DigitalOcean Spaces with the S3 driver for the relevant disk. It seemed that files were uploaded with private permissions and so the preview URL was not accessible.

Currently I have a simple Image field:

Image::make('Image')
  ->disk('spaces')
  ->storeOriginalName('image_name')
  ->storeSize('image_size')
  ->store(new PublicUpload),

That is using an "invokable" class (PublicUpload) that looks like this:

<?php

namespace AppNova;

use IlluminateHttpRequest;

class PublicUpload
{
    public function __invoke(Request $request, $model, $attribute, $requestAttribute, $disk, $storagePath)
    {
        $currentFile = $request->file($attribute);

        return [
            $attribute => $currentFile->storePublicly($storagePath)
        ];
    }
}

However, StorePublicly just doesn’t seem to be doing its job the way I have this set up. The images upload to the Spaces storage but they are still set as "private".

I realise I could probably just create a route to alter the preview URL in Nova but I have no reason to store these files privately and I know this should be possible.

I’d appreciate any help or advice at all. Thanks in advance.

Source: Ask PHP

LEAVE A COMMENT