LoginSignup
17
21

More than 5 years have passed since last update.

Laravel 5.4/5.5でS3ファイルアップロード

Last updated at Posted at 2017-03-11

ゴール

Laravelを使って、S3に画像ファイルをアップロード。

参考資料

作業開始

Laravel編集

Larval 5.4 composer.json

composer.json
{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=5.6.4",
        "laravel/framework": "5.4.*",
        "laravel/tinker": "~1.0",
        "laravelcollective/html": "~5.2",
        "league/flysystem-aws-s3-v3" : "~1.0"
    },
...省略

Larval 5.5 composer.json

composer.json
{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=7.0.0",
        "fideloper/proxy": "~3.3",
        "laravel/framework": "5.5.*",
        "laravel/tinker": "~1.0",
        "league/flysystem-aws-s3-v3" : "~1.0"
    },

filesystems.php

filesystems.php
...省略
    'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_KEY'),
            'secret' => env('AWS_SECRET'),
            'region' => env('AWS_REGION'),
            'bucket' => env('AWS_BUCKET'),
        ],

    ],

AWS認証情報はenv('AWS_SECRET')で取っているので、.envに保存されている。
直接こちらfilesystems.phpにKeyを入れたらNG。これからパスワードはずっとGithubに保存されてしまう。

.env

.envに下記記述を追加:(パスワードは適宜に入れてね)

AWS_KEY=AKI****
AWS_SECRET=****
AWS_REGION=us-east-1
AWS_BUCKET=***-test

Laravelソースコード

PhotoController.php
class PhotoController extends Controller
...
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        echo "zzzzz01 : aaaaa store 01";
        //Save Photo
        if ($request->hasFile('image_01')) {
            $path = $request->image_01->store('profiles', 's3');
            var_dump($path);
        }
    }

実行結果:

zzzzz01 : aaaaa store 01string(54) "profiles/JlaRMuuWdOJYlbV2MWqYBR9k6XlnZ38OOtXHUk91.jpeg"

S3側
スクリーンショット 2017-03-11 23.26.33.png
/***-test/profiles/JlaRMuuWdOJYlbV2MWqYBR9k6XlnZ38OOtXHUk91.jpegが生成された。

エンハンスメント

上記ソースコードでは、アップロードした画像は非公開状態。
Webサイト用にアップロードするなら、デフォルトを公開で設定したい。

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        echo "zzzzz01 : aaaaa store 01";
        //Save Photo
        if ($request->hasFile('image_01')) {
            $path = Storage::disk('s3')->putFile('profiles', $request->image_01, 'public'); // S3にアップ
            echo "\npath=" . $path;
            $url = Storage::disk('s3')->url($path);
            echo "\nurl=" . $url;
            echo "\n";
        }
    }

zzzzz01 : aaaaa store 01 
path=profiles/R9cs2lgM7G***.jpeg 
url=https://s3.amazonaws.com/***-test/profiles/R9cs2lgM7G***.jpeg

Yeah!
Laravel素晴らしい

17
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
21