Laravel: Make Images Stored in alternative Storage Folder Publicly Accessible
In Laravel, when you store images in an alternative storage folder like Amazon S3, Google Cloud Storage, or any other cloud storage service, by default, these images are not publicly accessible. This is a security feature that prevents unauthorized access to your application's files. However, there are cases where you might want to make these images publicly accessible, such as serving them directly to your users.
To make images stored in an alternative storage folder publicly accessible in Laravel, you can follow these steps:
- Configure the Storage Facade to use the cloud storage driver and set the disk's visibility to public.
First, you need to configure the Laravel's Storage Facade to use your cloud storage driver and set the disk's visibility to public. Open the .env
file in your Laravel project and add the following lines:
AWS_S3_DISK=s3
AWS_S3_KEY=your_access_key
AWS_S3_SECRET=your_secret_access_key
AWS_S3_REGION=your_region
AWS_S3_BUCKET=your_bucket_name
DISK=s3
URL=https://your-bucket-name.s3.amazonaws.com
Replace your_access_key
, your_secret_access_key
, your_region
, and your_bucket_name
with your actual AWS access key, secret access key, region, and bucket name.
Next, open the config/filesystems.php
file and update the disks
array to include your cloud storage configuration:
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => public_path(),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_S3_KEY'),
'secret' => env('AWS_S3_SECRET'),
'region' => env('AWS_S3_REGION'),
'bucket' => env('AWS_S3_BUCKET'),
'url' => env('URL'),
'visibility' => 'public',
],
],
Make sure to set the visibility
option to 'public'
for the s3
disk.
- Update your image uploading logic to use the public disk.
Now that you have configured your cloud storage to be publicly accessible, you need to update your image uploading logic to use the public disk. Open the app/Http/Controllers/ImageController.php
file (or create a new controller if you don't have one) and update the store
method to use the public disk:
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class ImageController extends Controller
{
public function store(Request $request)
{
$request->validate([
'image' => 'required|image|mimes:jpeg,png,jpg,bmp,gif,svg,pdf,msword,excel,powerpoint,text,csv,xlsx,odt,ods,odp,potx,pot,ppsx,pps,pptx,ppt,docx,doc,txt'
]);
$image = $request->file('image');
$imageName = time() . '_' . $image->getClientOriginalName();
$imagePath = Storage::disk('public')->putFileAs('images', $image, $imageName);
return response()->json([
'success' => true,
'image' => Storage::disk('public')->url($imagePath),
]);
}
}
- Serve the images directly from the cloud storage.
Finally, you can serve the images directly from the cloud storage by using the URL provided by the Storage Facade. In the example above, the image is stored in the public
disk, and the URL is returned in the response. You can use this URL to serve the image directly to your users.
<img src="{{ Storage::disk('public')->url('images/' . $imagePath) }}" alt="Image">
By following these steps, you can make images stored in an alternative storage folder publicly accessible in Laravel. However, be aware that making images publicly accessible can pose a security risk, so it's essential to take appropriate measures to secure your cloud storage account and restrict access to sensitive files.