我是如何尝试通过Laravel 5.1将图像上传到S3,而S3中收到的图像被认为已损坏,如下图所示。
第1步:使用Composer
'region' => env('AWS_REGION', 'my region'), 'version' => 'latest', 'ua_append' => [ 'L5MOD/' . AwsServiceProvider::VERSION, ], 'credentials' => [ 'key' => 'enter your code', 'secret' => 'enter your code' ], 'scheme' => 'http'
第2步
Providers 'Aws\Laravel\AwsServiceProvider', Alias 'AWS' => 'Aws\Laravel\AwsFacade'
第3步
设置控制器
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/";
}
第4步
在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
请告诉我我离开了什么,我应该寻找什么?,这是因为我搜索了许多解决方案,即使我按照其他一些开发人员提出的几个步骤,它仍然不适用于我。 ,要么是我对这些功能不够理解,要么我的尝试没有合适的解决方案?
答案 0 :(得分:2)
好了,现在我们知道您正在使用干预,使用make
实例化Image对象的适当方式:
$image = Image::make($input['file']);
根据文档,make()
方法可以采用字符串,路径以及UploadedFile对象。
现在安全地将数据写入S3:
$s3->putObject(array(
'Bucket' => 'mybucket',
'Key' => "{$folder}/{$fileName}",
'Body' => (string) $image->encode(),
'ContentType' => $mime,
'ACL' => 'public-read',
));
顺便说一下,为什么不将本机S3适配器用于Laravel的文件系统? https://laravel.com/docs/5.3/filesystem#driver-prerequisites