參考 AWS – 在 S3 服務建立儲存貯體 建立 AWS S3 儲存貯體及獲取 Access Key ID及 Secret Key,參考 Laravel 官方文件,AWS S3 需安裝 league/flysystem-aws-s3-v3 套件 (我使用的是 Laravel 6.x LTS 所以需要加上版本編號)
composer league/flysystem-aws-s3-v3 "~1.0"
修改 .env 檔,將獲得的 Access Key ID 及 Secret Access Key 加入
AWS_ACCESS_KEY_ID= Key ID
AWS_SECRET_ACCESS_KEY= Access Key
# 填入 S3 所在區域
AWS_DEFAULT_REGION=ap-east-1
# Bucket 名稱
AWS_BUCKET=rvtstorage
新增圖片上傳用路由
//後台圖片上傳功能測試
Route::get('images/upload','Admin\ImagesController@imagesUploadForm');
Route::post('images/upload','Admin\ImagesController@imagesUpload')->name('imageUpload');
新增圖片上傳用表單
<form id="myform" action="{{ route('admin.imageUpload') }}" method="POST" enctype="multipart/form-data">
@csrf
<div class="card-body">
<div class="form-group">
<label for="image">選擇圖片</label>
<div class="input-group">
<div class="custom-file">
<input type="file" id="image" name="image" class="custom-file-input {{ $errors->has('image') ? ' is-invalid' : '' }}" accept="image/*" >
<label class="custom-file-label" for="image">瀏覽選擇圖片</label>
</div>
</div>
@if ($errors->has('image'))
<div>
<span class="text-danger" role="alert">
<strong>{{ $errors->first('image') }}</strong>
</span>
</div>
@endif
</div>
</div>
<div class="card-footer text-center">
<button type="submit" class="btn btn-primary">儲存</button>
</div>
</form>
新增圖片上傳用驗證 Request
php artisan make:request Admin\ImageUploadRequest
<?php
namespace App\Http\Requests\Admin;
use Illuminate\Foundation\Http\FormRequest;
use Auth;
class ImageUploadRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
//必須登入才可以使用這個Request
public function authorize()
{
return Auth::check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'image' => 'required|image|max:2000'
];
}
}
新增圖片上傳用 Controller
php artisan make:controller Admin\ImagesController
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use View;
use Redirect;
use File;
use Carbon\Carbon;
use Mail;
use Session;
use Storage;
use App\Http\Requests\Admin\ImageUploadRequest;
class ImagesController extends Controller
{
public function __construct(){
$this->middleware('auth:admin');
}
/**
* Display the Mail Form.
*
* @return \Illuminate\Http\Response
*/
public function imagesUploadForm()
{
return view('admin.images.imagesUploadForm');
}
/**
* 將檔案傳送到 AWS S3 Storage
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*
*/
public function imagesUpload(ImageUploadRequest $request)
{
// 檢查確認是否可以連線AWS S3
// $s3 = Storage::cloud();
// dd($s3);
if($request->hasFile('image')){
$file = $request->file('image');
$destPath = 'upload/images';
//檢查目錄是否存在,不存在則建立
if(!Storage::disk('s3')->has($destPath)){
Storage::disk('s3')->makeDirectory($destPath);
}
//抓取原始副檔名
$ext = $file->getClientOriginalExtension();
//檔案名稱修改成目前時間標記加上副檔名
$fileName = (Carbon::now()->timestamp) . '.' . $ext;
//將檔案傳送至 S3
//加上 public 讓檔案是 Visibility 不然該檔案是無法被看見的
Storage::disk('s3')->put("$destPath/$fileName", file_get_contents($file) , 'public');
//獲取 S3 圖片連結
$url = Storage::disk('s3')->url("$destPath/$fileName");
}
$message = "檔案上傳成功";
Session::put('success',$message);
return view('admin.images.imagesUploadForm', compact('url'));
}
}
測試上傳