Managing images with Laravel-stapler and Amazon s3

Published: September 01, 2015

Category: Laravel

In order to lighten the load on our web server, we can use a remote storage service to host and serve our static assets, such as images. A popular and very cost effective storage service is provided by Amazon Simple Storge ( Amazon s3 ).

Laravel-Stapler is a Stapler-based file upload package for the Laravel framework. We walkthrough how to integrate it in Lavarel 4 with s3, allowing users to upload and view image files in our app

If you do not have an s3 bucket, set one up as per Amazon s3 instructions.

To install lavarel-stapler as per the guidance at , place the following in composer.json. You will need the aws sdk to access s3.

"require": {
    "codesleeve/laravel-stapler": "1.0.*",
    "aws/aws-sdk-php": "2.4.*@dev"

Add the following line to the providers array in app/config/app.php


In the terminal:

composer update
php artisan config:publish codesleeve/stapler

By default, Stapler will save images to the folder public/system. We need to create this folder, and give our server permissions to write to it. From the root folder of your application run the following in the terminal:

mkdir public/system
chmod 777 public/system

This is referencing the same public folder that contains your css, js, and images folders.

In our User model:

use Codesleeve\Stapler\ORM\StaplerableInterface;
use Codesleeve\Stapler\ORM\EloquentTrait;

class User extends Eloquent implements StaplerableInterface {
    use EloquentTrait;

    // Add the 'avatar' attachment to the fillable array so that it's mass-assignable on this model.
    protected $fillable = ['avatar', 'first_name', 'last_name'];

    public function __construct(array $attributes = array()) {
        $this->hasAttachedFile('avatar', [
            'styles' => [
                'medium' => '300x300',
                'thumb' => '100x100'
         'storage' => 's3'


Make sure that the hasAttachedFile() method is called right before parent::__construct() of your model.

To create the necessary databse columns for stapler, in the terminal:

php artisan stapler:fasten users avatar
php artisan migrate

In app/config/packages/codesleeve/lavarel-stapler/s3.php. Take care to reference your AWS keys from the .env.php file as $_ENV['AWS_SECRET_KEY'] etc.

return [

's3_client_config' => [
        'key' => 'your_s3_key_here',
        'secret' => 'your_s3_secret_here',
        'region' => 'your_region_here',
        'scheme' => 'http'
's3_object_config' => [
        'Bucket' => 'your_bucket-name',
        'ACL' => 'public-read'
'path' => ':attachment/:id/:style/:filename',


In app/config/packages/codesleeve/lavarel-stapler/stapler.php alter Stapler Storage Driver to point to s3

    'storage' => 's3',

To upload an image , in your new view:

= Form::open(['url' => action('UsersController@store'), 'method' => 'POST', 'files' => true]) ?>
    = Form::input('first_name') ?>
    = Form::input('last_name') ?>
    = Form::file('avatar') ?>
    = Form::submit('save') ?>
= Form::close() ?>

In your controller

public function store()
    // Create and save a new user, mass assigning all of the input fields (including the 'avatar' file field).
    $user = User::create(Input::all());

In your show view

<img src== $user->avatar->url() ?>" >
<img src== $user->avatar->url('medium') ?>" >
<img src== $user->avatar->url('thumb') ?>" >

Back to News


No Thanks, I have enough traffic already!

google certified SEO agency
Copyright ©2021 8Ball DigitalTM 163 Treffry Rd, Truro, Cornwall TR1 1UF