Giới thiệu về filesystem storage trong Laravel

Lượt xem: 10990

Laravel Storage

Laravel cung cấp cho ta một lớp abstraction filesystem mạnh mẽ do sử dụng package Flysystem tạo bởi Frank de Jonge. Tích hợp Flysystem vào Laravel với cách sử dụng rất đơn giản để giao tiếp với các local filesystem, Amazon S3, và Rackspace Cloud Storage. Thậm chí còn có thể switch các storage một cách dễ dàng sử dụng chung API.

Lợi ích của Laravel storage:

- Việc lưu trữ các file sẽ tốn rất nhiều tài nguyên và băng thông trên server, giải pháp đưa ra là lưu trữ trên các server bên ngoài nên Laravel Storage được tích hợp hỗ trợ lưu trữ file trên server ngoài như Amazon S3.

- Đơn giản hóa trong việc thiết lập các tùy chon.

Cấu hình (Config):

File cấu hình được đặt tại "config/filesystems.php"  và được thiết lập sẵn như sau.

    /*
    |--------------------------------------------------------------------------
    | Default Filesystem Disk
    |--------------------------------------------------------------------------
    */

    'default' => env('FILESYSTEM_DRIVER', 'local'),

    /*
    |--------------------------------------------------------------------------
    | Default Cloud Filesystem Disk
    |--------------------------------------------------------------------------
    */

    'cloud' => env('FILESYSTEM_CLOUD', 's3'),

    /*
    |--------------------------------------------------------------------------
    | Filesystem Disks
    |--------------------------------------------------------------------------
    */

    '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'),
        ],

    ],

 

 Với Local Driver

Khi thao tác tới file sẽ tương tác tới thư mục root được khai báo trong file, giá trị mặc định được trỏ tới storage/app. Do đó với đoạn code sau file sẽ được lưu trong storage/app/file.txt:

    Storage::disk('local')->put('file.txt', 'Contents');

Với config driver là S3 hoặc Rackspace cần cài đặt các package như yêu cầu thông qua composer

Cấu hình cho FTP Driver

Mặc định FTP driver không được cấu hình mặc định trong filesystems.php. Bạn có thể sử dụng ví dụ cấu hình dưới đây như Laravel Docs:

    'ftp' => [
		'driver'   => 'ftp',
		'host'     => 'ftp.example.com',
		'username' => 'your-username',
		'password' => 'your-password',

		// Optional FTP Settings...
		// 'port'     => 21,
		// 'root'     => '',
		// 'passive'  => true,
		// 'ssl'      => true,
		// 'timeout'  => 30,
	]

Cấu hình cho Rackspace Driver

Cũng giống như FTP, Rackspace driver cũng không được config sẵn trong file filesystems.php.

Các bạn có thể tham khảo cấu hình sau:

    'rackspace' => [
		'driver'    => 'rackspace',
		'username'  => 'your-username',
		'key'       => 'your-key',
		'container' => 'your-container',
		'endpoint'  => 'https://identity.api.rackspacecloud.com/v2.0/',
		'region'    => 'IAD',
		'url_type'  => 'publicURL',
	]

Để sử dụng thư việc Storage các bạn đừng quên khai báo thư viện:

use Illuminate\Support\Facades\Storage;

Sau khi khai báo thư viện các bạn có thể tương tác với các disk đã được cấu hình.

	$disk = Storage::disk('local');
	// or
	$disk2 = Storage::disk('s3');

 

Giới thiệu các hàm cơ bản:

$contents = Storage::get('file.txt');
$exists = Storage::disk('local')->exists('file.txt');

Chú ý: Khi sử dụng local driver, hãy chắc chắn tạo một symbolic link tại public/storage trỏ tới thư mục storage/app/public.

$url = Storage::url('file.txt');
$size = Storage::size('file.txt');
$time = Storage::lastModified('file.txt');
Storage::put('file.txt', $contents);
Storage::put('file.txt', $resource);
Storage::copy('old/file.txt', 'new/file.txt');
Storage::move('old/file.txt', 'new/file.txt');
Storage::prepend('file.txt', 'Prepended Text');
Storage::append('file.txt', 'Appended Text');
Storage::delete('file.txt');
Storage::delete(['file1.txt', 'file2.txt']);
$files = Storage::files($directory);
$files = Storage::allFiles($directory);
$directories = Storage::directories($directory);
$directories = Storage::allDirectories($directory);
Storage::makeDirectory($directory);
Storage::deleteDirectory($directory);
return Storage::download('file.jpg');

Mong rằng qua bài này sẽ giúp các bạn hiểu hơn về Storage trong Laravel. Các bạn có thể xây dựng một hệ thống quản lý các media của mình trên server một cách dễ dàng hơn. Chúc các bạn thành công trong lập trình Laravel.

Ngụy Kim Hưng