50 lines
1.7 KiB
PHP
50 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Throwable;
|
|
|
|
class StorageHealthCheck extends Command
|
|
{
|
|
protected $signature = 'storage:health-check {disk? : Disk name, defaults to filesystems.default}';
|
|
|
|
protected $description = 'Check current storage disk, write/read/delete a test file, and print its public URL.';
|
|
|
|
public function handle(): int
|
|
{
|
|
$disk = $this->argument('disk') ?: config('filesystems.default');
|
|
$path = 'healthcheck/' . now()->format('YmdHis') . '-' . uniqid() . '.txt';
|
|
|
|
$this->line('default=' . config('filesystems.default'));
|
|
$this->line('disk=' . $disk);
|
|
$this->line('endpoint=' . (config("filesystems.disks.{$disk}.endpoint") ?: '-'));
|
|
$this->line('url=' . (config("filesystems.disks.{$disk}.url") ?: '-'));
|
|
$this->line('bucket=' . (config("filesystems.disks.{$disk}.bucket") ?: '-'));
|
|
$this->line('path_style=' . json_encode(config("filesystems.disks.{$disk}.use_path_style_endpoint")));
|
|
|
|
try {
|
|
$stored = Storage::disk($disk)->put($path, 'ok');
|
|
|
|
if (!$stored) {
|
|
$this->error("put=false path={$path}");
|
|
return self::FAILURE;
|
|
}
|
|
|
|
$exists = Storage::disk($disk)->exists($path);
|
|
$this->line('put=ok');
|
|
$this->line('exists=' . ($exists ? 'yes' : 'no'));
|
|
$this->line('public_url=' . Storage::disk($disk)->url($path));
|
|
|
|
Storage::disk($disk)->delete($path);
|
|
$this->line('delete=ok');
|
|
} catch (Throwable $e) {
|
|
$this->error(get_class($e) . ': ' . $e->getMessage());
|
|
return self::FAILURE;
|
|
}
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
}
|