Skip to content
This repository has been archived by the owner on Mar 8, 2022. It is now read-only.

KentoMoriwaki/s3-observer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Observer to sync your Eloquent with Amazon S3

You can easily upload and delete images by S3Observer

$user = User::find($id);
$user->fill(Input::all());
if (Input::hasFile('profile_image')) {
    $user->profile_image = Input::file('profile_image');
}
if (Input::has('delete_profile_image')) {
    $user->profile_image = null;
}
$user->save();

Now the uploaded file is on Amazon S3!

How To Use

Before

You need setup aws/aws-sdk-php-laravel before use S3Observer

Installation

Require the translucent/s3-observer in your composer.json

{
    "require": {
        "translucent/s3-observer": "0.4.*"
    }
}

Configuration

In order to use S3Observer, update settings files.

php artisan config:publish translucent/s3-observer

Sample configuration

return array(
    'public' => true,
    'bucket' => '',
    'base' => null,
    'acl' => null,
);

Add S3Observer provider and facade(optional) to app/config/app.php

'providers' => array(
     // ...
    'Translucent\S3Observer\S3ObserverServiceProvider',
),
'aliases' => array(
    // ...
    'S3Observer' => 'Translucent\S3Observer\Facades\S3Observer',
)

Sample Usage

In your model,

protected static function boot()
{
    parent::boot();
    // Setup observer
    $observer = S3Observer::setUp('User', array(
        'bucket' => 'user-bucket'
    ));
    // Observe fields
    $observer->setFields('profile_image', 'thumbnail');
    // Fields configuration
    $observer->config('thumbnail.image', array(
        'width' => 150,
        'height' => 150
    ));
    static::observe($observer);
}

And in your controller...

public function postEdit($id)
{
    $user = User::findOrFail($id);
    $user->fill(Input::all());
    if (Input::hasFile('profile_image')) {
        $user->profile_image = Input::file('profile_image');
        $user->thumbnail = Input::file('profile_image');
    }
    if (Input::has('delete_profile_image')) {
        $user->profile_image = null;
				$user->thumbnail = null;
    }
    $user->save();
    return Redirect::to('/');
}

More information

To see more information, please check Wiki!

License

Under the MIT license.

© Kento Moriwaki 2014.

About

Observer to sync your Eloquent with Amazon S3

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages