イントロダクションIntroduction
LaravelはFrank de Jongeさんが作成した、ありがたいほど素晴らしい抽象ファイルシステムであるFlysystem PHPパッケージを提供しています。LaravelとFlysystemの統合によりローカルのファイルシステム、Amazon S3、Rackspaceクラウドストレージを操作できる、シンプルなドライバーが提供できました。更に素晴らしいことにそれぞれのシステムに対し同じAPIを使用しているため、ストレージをとても簡単に変更できるのです。Laravel provides a powerful filesystem abstraction thanks to the wonderful Flysystem[https://github.com/thephpleague/flysystem] PHP package by Frank de Jonge. The Laravel Flysystem integration provides simple to use drivers for working with local filesystems, Amazon S3, and Rackspace Cloud Storage. Even better, it's amazingly simple to switch between these storage options as the API remains the same for each system.
設定Configuration
ファイルシステムの設定ファイルは、config/filesystems.phpです。このファイルの中に全「ディスク」の設定があります。それぞれのディスクは特定のストレージドライバーと保存場所を表します。設定ファイルにはサポート済みのドライバーそれぞれの設定例を用意しています。ですからストレージの設定と認証情報を反映するように、設定オプションを簡単に修正できます。The filesystem configuration file is located at config/filesystems.php. Within this file you may configure all of your "disks". Each disk represents a particular storage driver and storage location. Example configurations for each supported driver is included in the configuration file. So, simply modify the configuration to reflect your storage preferences and credentials.
もちろん好きなだけディスクを設定できますし、同じドライバーに対し複数のディスクを持つことも可能です。Of course, you may configure as many disks as you like, and may even have multiple disks that use the same driver.
ローカルドライバーThe Local Driver
localドライバーを使う場合、設定ファイルで指定したrootディレクトリーからの相対位置で全ファイル操作が行われることに注意してください。デフォルトでこの値はstorage/appディレクトリーに設定されています。そのため次のメソッドでファイルはstorage/app/file.txtとして保存されます。When using the local driver, note that all file operations are relative to the root directory defined in your configuration file. By default, this value is set to the storage/app directory. Therefore, the following method would store a file in storage/app/file.txt:
Storage::disk('local')->put('file.txt', 'Contents');
他のドライバー使用要件Other Driver Prerequisites
S3とRackspaceドライバーを使用する場合は、それに適合するパッケージをComposerでインストールする必要があります。Before using the S3 or Rackspace drivers, you will need to install the appropriate package via Composer:
- Amazon S3: 
league/flysystem-aws-s3-v3 ~1.0Amazon S3:league/flysystem-aws-s3-v3 ~1.0 - Rackspace: 
league/flysystem-rackspace ~1.0Rackspace:league/flysystem-rackspace ~1.0 
基本的な使用法Basic Usage
ディスクインスタンス取得Obtaining Disk Instances
Storageファサードを使い設定済みのディスクへの操作ができます。たとえばこのファサードのputメソッドを使用し、デフォルトディスクにアバターを保存できます。Storageファサードのメソッド呼び出しで最初にdiskメソッドを呼び出さない場合、そのメソッドの呼び出しは自動的にデフォルトディスクに対し実行されます。The Storage facade may be used to interact with any of your configured disks. For example, you may use the put method on the facade to store an avatar on the default disk. If you call methods on the Storage facade without first calling the disk method, the method call will automatically be passed to the default disk:
<?php
namespace App\Http\Controllers;
use Storage;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
    /**
     * 指定したユーザーのアバターを更新
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function updateAvatar(Request $request, $id)
    {
        $user = User::findOrFail($id);
        Storage::put(
            'avatars/'.$user->id,
            file_get_contents($request->file('avatar')->getRealPath())
        );
    }
}
複数ディスクを使用する場合、Storageファサードに対しdiskメソッドを使用し特定のディスクへアクセスできます。もちろんdiskに続けメソッドをチェーンして実行できます。When using multiple disks, you may access a particular disk using the disk method on the Storage facade. Of course, you may continue to chain methods to execute methods on the disk:
$disk = Storage::disk('s3');
$contents = Storage::disk('local')->get('file.jpg');
ファイル取得Retrieving Files
getメソッドは指定したファイルの内容を取得するために使用します。メソッドはファイルの内容をそのまま返します。The get method may be used to retrieve the contents of a given file. The raw string contents of the file will be returned by the method:
$contents = Storage::get('file.jpg');
hasメソッドは、ディスクにファイルが存在しているかを判定するために使用します。The has method may be used to determine if a given file exists on the disk:
$exists = Storage::disk('s3')->has('file.jpg');
ファイルのメタ情報File Meta Information
sizeメソッドはファイルのサイズをバイトで取得するために使います。The size method may be used to get the size of the file in bytes:
$size = Storage::size('file1.jpg');
lastModifiedメソッドは最後にファイルが更新された時のUnixタイムスタンプを返します。The lastModified method returns the UNIX timestamp of the last time the file was modified:
$time = Storage::lastModified('file1.jpg');
ファイル保存Storing Files
putメソッドはディスクのファイルに保存するために使用します。putメソッドにはPHPのresourceも渡すことができ、Flysystemの裏で動いているストリームサポートを使用します。大きなファイルを取り扱う場合は、ストリームの使用を強く推奨します。The put method may be used to store a file on disk. You may also pass a PHP resource to the put method, which will use Flysystem's underlying stream support. Using streams is greatly recommended when dealing with large files:
Storage::put('file.jpg', $contents);
Storage::put('file.jpg', $resource);
copyメソッドは存在しているファイルをディスクの新しい場所へコピーします。The copy method may be used to copy an existing file to a new location on the disk:
Storage::copy('old/file1.jpg', 'new/file1.jpg');
moveメソッドは存在しているファイルをリネームするか、新しい場所へ移動します。The move method may be used to rename or move an existing file to a new location:
Storage::move('old/file1.jpg', 'new/file1.jpg');
ファイルの先頭/末尾追加Prepending / Appending To Files
prependとappendメソッドで、ファイルの初めと終わりに内容を簡単に挿入できます。The prepend and append methods allow you to easily insert content at the beginning or end of a file:
Storage::prepend('file.log', 'Prepended Text');
Storage::append('file.log', 'Appended Text');
ファイル削除Deleting Files
deleteメソッドはディスクから削除するファイルを単独、もしくは配列で受け付けます。The delete method accepts a single filename or an array of files to remove from the disk:
Storage::delete('file.jpg');
Storage::delete(['file1.jpg', 'file2.jpg']);
ディレクトリーDirectories
ディレクトリーの全ファイル取得Get All Files Within A Directory
filesメソッドは指定したディレクトリーの全ファイルの配列を返します。指定したディレクトリーのサブディレクトリーにある全ファイルのリストも取得したい場合は、allFilesメソッドを使ってください。The files method returns an array of all of the files in a given directory. If you would like to retrieve a list of all files within a given directory including all sub-directories, you may use the allFiles method:
$files = Storage::files($directory);
$files = Storage::allFiles($directory);
ディレクトリーの全ディレクトリー取得Get All Directories Within A Directory
directoriesメソッドは指定したディレクトリーの全ディレクトリーの配列を返します。指定したディレクトリー下の全ディレクトリーと、サブディレクトリー下の全ディレクトリーも取得したい場合は、allDirectoriesメソッドを使ってください。The directories method returns an array of all the directories within a given directory. Additionally, you may use the allDirectories method to get a list of all directories within a given directory and all of its sub-directories:
$directories = Storage::directories($directory);
// 再帰的…
$directories = Storage::allDirectories($directory);
ディレクトリー作成Create A Directory
makeDirectoryメソッドは必要なサブディレクトリーを含め、指定したディレクトリーを作成します。The makeDirectory method will create the given directory, including any needed sub-directories:
Storage::makeDirectory($directory);
ディレクトリー削除Delete A Directory
最後のdeleteDirectoryはディレクトリーと含まれている全ファイルを削除するために使用されます。Finally, the deleteDirectory may be used to remove a directory, including all of its files, from the disk:
Storage::deleteDirectory($directory);
カスタムファイルシステムCustom Filesystems
LaravelのFlysystem統合には、最初から様々な「ドライバー」が含まれています。しかしFlysystemはこれらのドライバーに限定されず、他の保存領域システムにも適用できます。皆さんのLaravelアプリケーションに適合した保存システムのカスタムドライバーを作成することができます。Laravel's Flysystem integration provides drivers for several "drivers" out of the box; however, Flysystem is not limited to these and has adapters for many other storage systems. You can create a custom driver if you want to use one of these additional adapters in your Laravel application.
たとえばDropboxFilesystemServiceProviderのような、作成するカスタムファイルシステムのためにサービスプロバイダーを用意してください。プロバイダーのbootメソッドの中でStorageファサードのextendメソッドを使い、カスタムドライバーを定義できます。In order to set up the custom filesystem you will need to create a service provider[/docs/{{version}}/providers] such as DropboxServiceProvider. In the provider's boot method, you may use the Storage facade's extend method to define the custom driver:
<?php
namespace App\Providers;
use Storage;
use League\Flysystem\Filesystem;
use Dropbox\Client as DropboxClient;
use Illuminate\Support\ServiceProvider;
use League\Flysystem\Dropbox\DropboxAdapter;
class DropboxServiceProvider extends ServiceProvider
{
    /**
     * サービスの初期処理登録後に実行
     *
     * @return void
     */
    public function boot()
    {
        Storage::extend('dropbox', function($app, $config) {
            $client = new DropboxClient(
                $config['accessToken'], $config['clientIdentifier']
            );
            return new Filesystem(new DropboxAdapter($client));
        });
    }
    /**
     * コンテナで結合の登録
     *
     * @return void
     */
    public function register()
    {
        //
    }
}
extendメソッドの最初の引数はドライバーの名前で、2つ目は$appと$config変数を受け取るクロージャーです。このリゾルバークロージャーはLeague\Flysystem\Filesystemのインスタンスを返す必要があります。$config変数はconfig/filesystems.phpで定義したディスクの値を含んでいます。The first argument of the extend method is the name of the driver and the second is a Closure that receives the $app and $config variables. The resolver Closure must return an instance of League\Flysystem\Filesystem. The $config variable contains the values defined in config/filesystems.php for the specified disk.
拡張を登録するサービスプロバイダーを作成したら、config/filesystem.php設定ファイルでdropboxドライバーを使用できます。Once you have created the service provider to register the extension, you may use the dropbox driver in your config/filesystem.php configuration file.