
S3の利用では、Laravel標準ファイルシステムライブラリを利用するのが良いでしょう🐱
もくじ
関連
アップロードの例
$s3_path = Storage::disk('コンフィグに指定したクレデンシャル名')->putFileAs($path, $pdf, $file_name, 'public');
サンプルコード
config/filesystem.php
<?php
return [
・・・
'pdf-uploader-job-s3' => [
'driver' => 's3',
'key' => env('CLIENT_S3_AWS_ACCESS_KEY_ID', ''),
'secret' => env('CLIENT_S3_AWS_SECRET_ACCESS_KEY', ''),
'region' => env('CLIENT_S3_AWS_DEFAULT_REGION', ''),
'bucket' => env('CLIENT_S3_AWS_BUCKET', ''),
'endpoint' => env('CLIENT_S3_AWS_ENDPOINT'),
'use_path_style_endpoint' => env('CLIENT_S3_AWS_USE_PATH_STYLE', false)
],
・・・
コンフィグのクリア
$ php artisan config:clear
<?php
namespace App\Services\CM\WebSetting;
use App\Entities\Language;
use App\Services\Traits\EntityModifiedTrait;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Illuminate\Http\Response;
abstract class AbstractWebSettingService
{
/**
* お客様IDを設定
*
* @param integer $company_id
* @return self
*/
public function setCustomerId(int $customer_id):self
{
$this->customer_id = $customer_id;
return $this;
}
/**
* お客様IDを取得
*
* @return integer|null
*/
public function getCustomerId():?int
{
return $this->customer_id;
}
/**
* PDFアップロード
*
* @return void
*/
public function uploadCompanyCss(Request $request): array
{
// ファイル名の設定
$date = Carbon::now();
$date = $date->format('YmdHis');
$pdf = $request->file('pdf');
$original_file_name = $request->file('pdf')->getClientOriginalName();
$file_name = "{$date}_{$original_file_name}";
// パス指定
$path = 'pdf_uploads/' . $this->getCustomerId() . '/' . $this->getCompanyId();
$web = $this->i_web->findWhere([
'company_id' => $this->getCompanyId()
]);
if (empty($web)) {
throw new NotFoundHttpException('更新データが存在しません。');
}
// アップロード実行
$s3_path = Storage::disk('pdf-uploader-job-s3')->putFileAs($path, $pdf, $file_name, 'public');
// DB保存
$web->pdf_name = $file_name;
$web->pdf_path = $s3_path;
$web->save();
return [
'pdf' => [
'name' => $file_name,
'path' => $s3_path,
]
];
}
/**
* PDFダウンロード
*
*/
public function downloadCompanyCss()
{
$web = $this->i_web->findWhere([
'company_id' => $this->getCompanyId()
]);
if (empty($web) || !isset($web->pdf_path)) {
return [];
}
$s3 = \Storage::disk('pdf-uploader-job-s3');
return $s3->download($web->pdf_path);
}
}

