我尝试通过Laravel 5.1将图片上传到S3,但在S3中接收到的图片被认为是损坏的,如下所示:
第一步:使用Composer
- composer require aws/aws-sdk-php-laravel:~3.0
- 从Laravel 5.1供应商中打开配置文件
'region' => env('AWS_REGION', '我的区域'), 'version' => 'latest', 'ua_append' => [ 'L5MOD/' . AwsServiceProvider::VERSION, ], 'credentials' => [ 'key' => '输入您的代码', 'secret' => '输入您的代码' ], 'scheme' => 'http'
第二步
- 在config/app.php中添加这些行以激活类别名和提供者
Providers 'Aws\Laravel\AwsServiceProvider', Alias 'AWS' => 'Aws\Laravel\AwsFacade'
第三步
set up the controller
namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use \Auth, \Redirect, \Validator, \Input, \Session, \Response; use App\Http\Controllers\Controller; use App\Http\Requests\PhotoRequest; use App\Photo; use \Image; use \File; use AWS; class PhotoController extends Controller { protected $photo; public function __construct(Photo $photo) { $this->photo = $photo; } public function index() { $p = Photo::all(); return view('photo.index') ->with('photos',$p); } public function create() { return view('photo.create')->with('photos',$this->_data); } /** * Store a newly created resource in storage. * * @return Response */ public function store(PhotoRequest $request) { $input = Input::all(); if ($this->photo->isValid($input)) { $img = $request->file('file'); $mime = $input['file']->getMimeType(); $fileName = time() . "." . strtolower($input['file']->getClientOriginalExtension()); $image = Image::make(file_get_contents($img->getRealPath())); $this->upload_s3($image, $fileName, $mime, "resource/Original"); $image->resize(400, 300); $this->upload_s3($image, $fileName, $mime, "resource/Thumbnail"); Photo::create([ 'title' => Input::get('title'), 'file' => $fileName, ]); Session::flash('exito', $image); return Redirect::route('photo.index'); } else { Session::flash('error', 'Failed'); return Redirect::back()->withInput()->withErrors($this->photo->messages); } } private function upload_s3($image, $fileName, $mime, $folder) { $s3 = AWS::createClient('s3'); try{ $s3->putObject(array( 'Bucket' => 'mybucket', 'Key' => "{$folder}/{$fileName}", 'Body' => "$image", 'ContentType' => $mime, 'ACL' => 'public-read', )); } catch(S3Exception $e){ echo $e->getAwsErrorCode() . "\n"; // The bucket couldn't be created echo $e->getMessage() . "\n"; } } private $_data = array(); private $path = "img/upload/";
}
第四步
Set up the View in Laravel 5.1 for UI
@extends('app') @section('content') <h1>Create:</h1> {!! Form::open(array('url' => 'photo/store', 'files' => true)) !!} <div class="form-group"> <label for="title">Title</label> <input type="text" class="form-control" id="title" name="title" placeholder="Photo Title"> <span class="help-block color-red">{!! $errors->first('title') !!}</span> </div> <div class="form-group"> <label for="file">File</label> <input type="file" class="form-control" id="file" name="file"> <span class="help-block color-red">{!! $errors->first('file') !!}</span> </div> <button type="submit" class="btn btn-default">Create</button> {!! Form::close() !!} @endsection
Image
是什么。 - Luceos