F
Filament15mo ago
Askancy

Get image from S3 without full path in sql column

I am adapting a pre-existing custom admin to filamentphp, now I have images on an S3 structured like this: cdn.blabla.ext/namesite/gallery/{{$game_id}}/{{$image}} With the old admin I used:
$filename = rand().'-gallery.'.$request->file('image')->extension();
Storage::disk('s3')->putFileAs('namesite/gallery/'.$game_id, $request->file('image'), $filename, 'public');
$article->image = $filename;
$filename = rand().'-gallery.'.$request->file('image')->extension();
Storage::disk('s3')->putFileAs('namesite/gallery/'.$game_id, $request->file('image'), $filename, 'public');
$article->image = $filename;
To visualize it I would simply use:
<img src="{{\Storage::disk('s3')->url('namesite/gallery/'.$game_id.'/'.$article->image)}}">
<img src="{{\Storage::disk('s3')->url('namesite/gallery/'.$game_id.'/'.$article->image)}}">
But how does it work with filament? In the documentation I read: >The column in the database must contain the path to the image, relative to the root directory of its storage disk. Currently in the database I saved ONLY the name of the image, if I understand correctly I in the database should save: image = namesite/gallery/123/nameimage.png can I bypass this?
3 Replies
cheesegrits
cheesegrits15mo ago
If you weren't using the dynamic $gameId, you could have used scoped storage and not had to change anything else. But unfortunately, having that dynamic path makes things a little trickier to migrate. Honestly, your best bet is to migrate your existing data so the database field includes the full path. Then going forward you can use a closure in a directory() method on the FileUpload element to set the path to include the game ID. Depending how your data is structured, you could probably do the migration with a single MySQL query by hand, with something like ...
UPDATE your_table
LEFT JOIN your_game_table ON <whatever the relationship is>
SET new_image = CONCAT('namesite/gallery/', your_game_table.id, '/', image)
UPDATE your_table
LEFT JOIN your_game_table ON <whatever the relationship is>
SET new_image = CONCAT('namesite/gallery/', your_game_table.id, '/', image)
Practice the query using new_image. Then you can rename as appropriate once it's working. Then in the FileUpoad, something like ...
FileUpload('image')
->disk('s3')
->directory(function (Get $get) {
// some logic here using $get to figure out the game ID
return 'namesite/gallery/' . $gameId;
}),
FileUpload('image')
->disk('s3')
->directory(function (Get $get) {
// some logic here using $get to figure out the game ID
return 'namesite/gallery/' . $gameId;
}),
Askancy
AskancyOP15mo ago
Thank you very much @cheesegrits !
Penso che sia conveniente scrivere l'url completo dell'immagine nella query, tuttavia non capisco come inserire il "namesite" nel filesystem... I would not like to save that query paths as: "namesite/gallery/1487/starfield-40586589.jpg", I would like to save only "gallery/1487/starfield-40586589.jpg", and get the namesite from the filesystem, but I don't understand how to set it up.... If I put it in the AWS_URL, it doesn't read it....
cheesegrits
cheesegrits15mo ago
If you just want to always have namesite/ prepended to the path (and it doesn't change, it's always the same string) you can use a scoped disk. https://laravel.com/docs/10.x/filesystem#scoped-and-read-only-filesystems
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Want results from more Discord servers?
Add your server