使用 Laravel 文件系统在 S3 中上传 base64 图像
确保您的项目已安装 Flysystem S3 驱动程序:
composer require league/flysystem-aws-s3-v3 ~1.0
在 config/filesystems.php 中
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
],
在 .env 文件中
AWS_ACCESS_KEY_ID=[YOURKEY]
AWS_SECRET_ACCESS_KEY=[YOURSECRETACCESSKEY]
AWS_DEFAULT_REGION=[YOURREGION]
AWS_BUCKET=[YOURBUKET]
AWS_URL=[AWSURL]
在你的控制器文件中
存储门面用于与我们配置的 s3 磁盘进行交互。在控制器文件中,我们需要添加
use Illuminate\Support\Facades\Storage;
在你的方法中
$image = $request->image; // your base64 encoded
$data = explode( ',', $image );
$current_timestamp = Carbon::now()->timestamp;
$imageName = rand().'jpg';
$filenametostore='uploads/'. $imageName;
Storage::disk('s3')->put($filenametostore, base64_decode($data[1]), 'public');