Last active
March 19, 2018 05:34
-
-
Save KangYoosam/e6edd3ef16b3a0716b950cb7e0395b39 to your computer and use it in GitHub Desktop.
Laravel5.3のQueue非同期処理 (AWS3へ画像アップロード) ref: https://qiita.com/kangyoosam/items/0ef7822aaac0883175b7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$this->dispatch(new UploadImage($channel, $fileId)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment