Skip to content

Instantly share code, notes, and snippets.

@localdisk
Created June 30, 2014 01:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save localdisk/5f6608d271c2842255e3 to your computer and use it in GitHub Desktop.
Save localdisk/5f6608d271c2842255e3 to your computer and use it in GitHub Desktop.
aws/aws-sdk-php-laravel で S3
<?php
Route::get('dir', function()
{
return View::make('dir');
});
Route::post('dir', function()
{
$v = Validator::make(Input::all(), ['name' => 'required']);
if ($v->fails()) {
return Redirect::back()->withErrors($v);
}
// ディレクトリを作成
$result = AWS::get('s3')->putObject([
'Bucket' => 'bucketName',
'Key' => rtrim(Input::get('name'), '/') . '/',
'Body' => ''
]);
echo $result;
});
Route::get('aws', function()
{
return View::make('aws');
});
Route::post('awsupload', function()
{
if (Input::hasFile('file')) {
$file = Input::file('file');
$result = AWS::get('s3')->putObject([
'Bucket' => 'bucketName',
'Key' => "images/{$file->getClientOriginalName()}",
'SourceFile' => $file->getRealPath(),
'ACL' => \Aws\S3\Enum\CannedAcl::PRIVATE_ACCESS // 原則非公開
]);
// TODO 画像データを保存
return Response::json(['result' => 'success', 'object' => $result->toArray()]);
}
return Response::json(['result' => 'error'], 500);
});
Route::get('s3result', function()
{
// バケットの特定ディレクトリに入っているオブジェクトを取得
/** @var \Aws\S3\S3Client $s3 */
$s3 = AWS::get('s3');
$objects = $s3->getIterator('ListObjects', [
'Bucket' => 'bucketName',
'Prefix' => 'images/',
'Delimiter' => '/'
]);
$files = [];
foreach ($objects as $o) {
// フォルダを取り除く(フォルダのサイズは 0 )
if ($o['Size'] == 0) {
continue;
}
$clazz = new stdClass;
$clazz->key = $o['Key'];
// pre-signed URL(一時公開URL) を取得
$command = $s3->getCommand('GetObject', [
'Bucket' => 'smoochy',
'Key' => $clazz->key
]);
// 10分間有効なURLを取得
$clazz->url = $command->createPresignedUrl('+10 minutes');
$files[] = $clazz;
}
return View::make('s3result', ['files' => $files, 'objects' => $objects->toArray()]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment