webleyson's avatar

Setting spaces image as private to public but available via application

Hi. I wish my users to be able to upload images to spaces and make sure they're not accessible to anyone outside of the application. I would have thought it would be as simple as Storage::get (with secret key) - and access the image... But it doesn't seem to be so easy.

How does one achieve this?

Thank you.

0 likes
3 replies
vincent15000's avatar

You have to use a route and a controller to retrieve the images.

Here is the idea, but it depends on what you are passing to the controller : id, image name, image slug, ... and my example assumes you have an Image model. In the response you need to give the file url in the file method.

Route::get('image/{id}', ImageController::class);
...
// ImageController
public function __invoke($id)
{
	$image = Image::find($id);
	return response()->file($image->url);
}

UPDATED => no url as parameter but an id or a name or a slug, ...

webleyson's avatar
webleyson
OP
Best Answer
Level 1

Hi Thanks for the response. I have solved the issue. You can retrieve private images using the get method using the S3 driver.

Storage::disk('do_spaces')->get($imagePath); Then encode the image $img = base64_encode($imageData);

Pass $img variable back to the view and display using img class="card-img-bottom" src='data:image/jpeg;base64,{{ $img }}

1 like
vincent15000's avatar

@webleyson You didn't say that your images aren't on the server and I could guess. You need to be precise when you create a post and give all needed informations.

Thus my answer was about what I thought.

But what I shared with you works fine too, but is better appropriated for images on the server.

Please or to participate in this conversation.