文件上传到 aws S3 Laravel 5.1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34574347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
File Upload to aws S3 Laravel 5.1
提问by Sachin Singh
I am getting the Following Error:
我收到以下错误:
FatalErrorException in FilesystemManager.php line 179: Class 'League\Flysystem\AwsS3v3\AwsS3Adapter' not found
FilesystemManager.php 第 179 行中的 FatalErrorException:找不到类“League\Flysystem\AwsS3v3\AwsS3Adapter”
Code:
代码:
//Composer.json
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.1.*",
"laravel/socialite": "~2.0",
"guzzlehttp/guzzle": "~4.0",
"predis/predis": "^1.0",
"tymon/jwt-auth": "0.5.*",
"league/flysystem-aws-s3-v2": "^1.0"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "~4.0",
"phpspec/phpspec": "~2.1"
}
//config/filesystem.php
'default' => 's3',
'cloud' => 's3',
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
's3' => [
'driver' => 's3',
'key' => '***********',
'secret' => '**************************************',
'region' => '*****',
'bucket' => '************',
],
],
//FileController
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Contracts\Filesystem\Filesystem;
use App\Http\Controllers\Controller;
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
public function postProfilePhoto(Request $request)
{
$token=JWTAuth::getToken();
$user = JWTAuth::toUser($token);
$image = $request->file('image');
//return $image;
$id=$user->id;
if($image)
{
$imageFileName = time() . '.' . $image->getClientOriginalExtension();
//return $imageFileName;
$s3 = \Storage::disk('s3');
$filePath = '/profilePhotos/'.$id . $imageFileName;
$s3->put($filePath, file_get_contents($image), 'public');
try{
ProfilePhoto::create(['userId'=>$id,'imgUrl'=>$filePath]);
return json_encode(['message'=>'Done!','Id'=>200,'Response'=>'']);
}
catch(Exception $e)
{
return json_encode(['message'=>'Not Allowed!','Id'=>402,'Response'=>'']);
}
}
else
{
return json_encode(['message'=>'No Pic!','Id'=>404,'Response'=>'']);
}
}
回答by Marcin Nabia?ek
You need to first run in console:
您需要先在控制台中运行:
composer remove league/flysystem-aws-s3-v2
and then you need to run in console:
然后你需要在控制台中运行:
composer require league/flysystem-aws-s3-v3:~1.0
to install S3 filesystem.
安装 S3 文件系统。
Laravel 5.1+ requires V3 versionand not V2.
Laravel 5.1+需要 V3 版本而不是 V2。