laravel get image s3 code example

Example 1: store image to s3 laravel

// Products controller
use Illuminate\Support\Facades\Storage;

$image = $request->file('image');
$filePath = 'images/' . $image->getClientOriginalName();
Storage::disk('s3')->put($filePath, file_get_contents($image), 'public');

Example 2: how to get private images in s3 laravel

use Storage;
use Config;

$client = Storage::disk('s3')->getDriver()->getAdapter()->getClient();
$bucket = Config::get('filesystems.disks.s3.bucket');

$command = $client->getCommand('GetObject', [
    'Bucket' => $bucket,
    'Key' => '344772707_360.mp4'  // file name in s3 bucket which you want to access
]);

$request = $client->createPresignedRequest($command, '+20 minutes');

// Get the actual presigned-url
echo $presignedUrl = (string)$request->getUri();

Tags:

Php Example