zahidgani
1507
4
Laravel

How to upload file private mode and retrive by generated url with expired time in aws s3 in laravel 5.3 or 5.4

Posted 2 years ago by zahidgani

Here is complete code:

upload file in aws s3 from laravel 5.3 or 5.4

Run: composer require league/flysystem-aws-s3-v3 set in config/filesystems.php

    's3' => [
        'driver' => 's3',
        'key' => 'your_generated_key',
        'secret' => 'your_generated_secret',
        'region' => 'us-east-1',
        'bucket' => 'your_bucket_folder',//folder will be public permission
    ],

run: php artisan config:cache

make controller: S3ImageController.php

make route: Route::get('s3-image-upload','[email protected]'); Route::post('s3-image-upload','[email protected]');

namespace App\Http\Controllers\classes;

use Illuminate\Http\Request; use App\Http\Controllers\Controller; use Illuminate\Contracts\Filesystem\Filesystem; class S3ImageController extends Controller { //this code for save data with private not public if you save public then not secure your file every one can see public function imageUploadPost(Request $request) { $this->validate($request, [ 'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg,mp4|max:2048', ]);

    $imageName = 'SampleVideo1.'.$request->image->getClientOriginalExtension();
    $image = $request->file('image');
    //$t = \Storage::disk('s3')->put($imageName, file_get_contents($image), 'public');
    $t = \Storage::disk('s3')->put($imageName, file_get_contents($image), 'private');
    $imageName = \Storage::disk('s3')->url($imageName);

    return back()
        ->with('success','Image Uploaded successfully.')
        ->with('path',$imageName);
}
public function imageUpload()
    {
        
        //this code for generate new signed url of your file
        $value="SampleVideo1.mp4";
        $disk = \Storage::disk('s3');
        if ($disk->exists($value)) 
        {
        $command = $disk->getDriver()->getAdapter()->getClient()->getCommand('GetObject', [
            'Bucket'                     => \Config::get('filesystems.disks.s3.bucket'),
            'Key'                        => $value,
            //'ResponseContentDisposition' => 'attachment;'//for download
        ]);

        $request = $disk->getDriver()->getAdapter()->getClient()->createPresignedRequest($command, '+10 minutes');
        //$request = $disk->getDriver()->getAdapter()->getClient()->createPresignedRequest($command, '+15 seconds');

        $generate_url = $request->getUri();
        echo $generate_url;
        }

     //this code for show form  
     return view('admin.image-upload');

    }

}

public function imageUpload() { //Retrive generated url of private file or video (i have upload one video in aws server where store this name but not directly open because private permission $value="SampleVideo1.mp4"; $disk = \Storage::disk('s3'); if ($disk->exists($value)) { $command = $disk->getDriver()->getAdapter()->getClient()->getCommand('GetObject', [ 'Bucket' => \Config::get('filesystems.disks.s3.bucket'), 'Key' => $value, //'ResponseContentDisposition' => 'attachment;'//for download ]);

    $request = $disk->getDriver()->getAdapter()->getClient()->createPresignedRequest($command, '+10 minutes');
    //$request = $disk->getDriver()->getAdapter()->getClient()->createPresignedRequest($command, '+15 seconds');

    $generate_url = $request->getUri();
    echo $generate_url;
}

//now you can access your generated url }

working fine.

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.