Skip to content

Instantly share code, notes, and snippets.

@KangYoosam
Last active March 19, 2018 05:34
Show Gist options
  • Save KangYoosam/e6edd3ef16b3a0716b950cb7e0395b39 to your computer and use it in GitHub Desktop.
Save KangYoosam/e6edd3ef16b3a0716b950cb7e0395b39 to your computer and use it in GitHub Desktop.
Laravel5.3のQueue非同期処理 (AWS3へ画像アップロード) ref: https://qiita.com/kangyoosam/items/0ef7822aaac0883175b7
<?php
namespace App\Jobs;
use App\Models\Channel;
use File;
use Storage;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class UploadImage implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
public $channel;
public $fileId;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Channel $channel, $fileId)
{
$this->channel = $channel;
$this->fileId = $fileId;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$path = storage_path() . '/uploads/' . $this->fileId;
$fileName = $this->fileId . '.png';
if(Storage::disk('s3images')->put('profile/' . $fileName, fopen($path, 'r+'))) {
File::delete($path);
}
$this->channel->image_fullname = $fileName;
$this->channel->save();
}
}
<?php
namespace App\Jobs;
use App\Models\Channel;
use File;
use Storage;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class UploadImage implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
public $channel;
public $fileId;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Channel $channel, $fileId)
{
$this->channel = $channel;
$this->fileId = $fileId;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$path = storage_path() . '/uploads/' . $this->fileId;
$fileName = $this->fileId . '.png';
if(Storage::disk('s3images')->put('profile/' . $fileName, fopen($path, 'r+'))) {
File::delete($path);
}
$this->channel->image_fullname = $fileName;
$this->channel->save();
}
}
$this->dispatch(new UploadImage($channel, $fileId));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment