Package for laravle 5.* to deal with S3 rest api upload

2.1.2 2017-03-27 09:16 UTC

This package is auto-updated.

Last update: 2024-11-15 10:30:30 UTC


README

Package for laravle 5.* to deal with S3 rest api upload

To use
1、composer require eunion/s3

2、Add the package to your application service providers in config/app.php file.

'providers' => [

    /**
     * Third Party Service Providers...
     */
    Eunion\S3\S3ServiceProvider::class,

],

3、publish the config to app/config/s3.php

	php artisan vendor:publish --provider="Eunion\S3\S3ServiceProvider" --tag=config

4、config you s3 in app/config/s3.php

	return [
		'driver'	=>	's3',		//
		'accessKey'	=>	'',			//your s3 api accessKey
		'secret'	=>	'',			//your s3 api secret
		'bucket'	=>	''			//your s3 bucket
	];


Controller Example:
$s3 = new S3();
$bucket = 'kify';
$path = 'test/'; // Can be empty ''
$lifetime = 3600; // Period for which the parameters are valid
$maxFileSize = (1024 * 1024 * 50); // 50 MB
$metaHeaders = [];
$requestHeaders = array(
	'Content-Type' => 'application/octet-stream',
	'Content-Disposition' => 'attachment; filename=${filename}'
);

$params = $s3->getHttpUploadPostParams(
	$bucket,
	$path,
	S3::ACL_PUBLIC_READ,
	$lifetime,
	$maxFileSize,
	"http://fenke.timelink.cn/test.php", // Or a URL to redirect to on success
	$metaHeaders,
	$requestHeaders,
	false // False since we're not using flash
);

$uploadUrl = "https://{$bucket}.".$s3::$endpoint;

return view('home.s3.index',compact('params','uploadUrl'));


Blade Example:
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>S3 upload</title>
</head>
<body>
    <form action="{{ $uploadUrl }}" method="post" enctype="multipart/form-data">
        @foreach($params as $k=>$v)
            {{ $k }}:<input type="text" name="{{ $k }}" value="{{ $v }}">
        @endforeach
        <hr>
            <input type="file" name="file" id="">
            <input type="submit" value="上传">
    </form>
</body>
</html>