Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
22
Help us understand the problem. What is going on with this article?
@zzeng

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

More than 1 year has passed since last update.

ゴール

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素晴らしい

22
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
zzeng
Solutions Engineer Manager, Japan

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
22
Help us understand the problem. What is going on with this article?