12/08/2018, 14:06

Laravel filesystem storage

Giới thiệu "Laravel provides a powerful filesystem abstraction thanks to the wonderful 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 ...

Giới thiệu

"Laravel provides a powerful filesystem abstraction thanks to the wonderful 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."

via: https://laravel.com/docs/5.3/filesystem#introduction

Hiểu đơn giản là: "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 chi còn có thể switch các storage một cách dễ dàng sử dụng chung API."

Lợi ích

  • Với việc lưu trữ file sẽ tốn rất nhiều tài nguyên và băng thông của server. Các giải pháp được đưa ra đó là lưu trữ ở các server bên ngoài như Amazon S3.
  • Đơn giản trong việc thiết lập các tùy chọn.

Config: Được đặt tại config/filesystems.php

Một vài thiết lập sẵn:

    /*
    | Default Filesystem Disk
    | Supported: "local", "ftp", "s3", "rackspace"
    */

    'default' => 'local',

    /*
    | Default Cloud Filesystem Disk
    */

    'cloud' => 's3',

    /*
    | Filesystem Disks
    */

    'disks' => [

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

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

        's3' => [
            'driver' => 's3',
            'key' => 'your-key',
            'secret' => 'your-secret',
            'region' => 'your-region',
            'bucket' => 'your-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 địch đượ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

  • Amazon S3: league/flysystem-aws-s3-v3 ~1.0
  • Rackspace: league/flysystem-rackspace ~1.0

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

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

Tham khảo:

'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

Trước hết và đừng quên:

use Storage;

Storage facade có thể được dùng để tương tác với bất kì disks đã cấu hình nào.

Để truy xuất tới một disk cụ thể:

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

Các hàm cơ bản

  • get() Được dung để lấy nội dung của một file:
$contents = Storage::get('file.jpg');
  • exists(): Kiểm tra sự tồn tại của 1 file:
$exists = Storage::disk('s3')->exists('file.jpg');
  • url() để lấy URL của file. Nếu bạn sử dụng local driver, nó sẽ tự động thêm vào /storage cho path và trả về một relative URL của file. Nếu bạn sử dụng s3 driver, URL đầy đủ sẽ được trả về.

    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ụcstorage/app/public.

    $url = Storage::url('file1.jpg');
  • size() trả về kích thước của file, đơn vị là bytes:
$size = Storage::size('file1.jpg');
  • lastModified() trả về giá trị UNIX timestamp của lần cuối cùng file bị thay đổi:
$time = Storage::lastModified('file1.jpg');
  • put(): Có thể được dùng để lưu file lên disk. Ta cũng có thể truyền một PHP resource cho hàm put, nó sẽ sử dụng stream của Flysystem. Bạn nên sử dụng stream khi phải làm việc với file lớn:
Storage::put('file.jpg', $contents);
Storage::put('file.jpg', $resource);
  • copy(): Dùng để copy một file đang tồn tại sang một vị trí mới trên disk:
Storage::copy('old/file1.jpg', 'new/file1.jpg');
  • move(): Dùng để đổi tên hay di chuyển một file đang tồn tại tới vị trí mới:
Storage::move('old/file1.jpg', 'new/file1.jpg');
  • prepend(): Thêm nội dung vào đầu 1 file:
Storage::prepend('file.log', 'Prepended Text');
  • append(): Chèn thêm nội dung vào cuối file:
Storage::append('file.log', 'Appended Text');
  • delete(): Xoá file khỏi disk, bạn có thể xóa 1 file hoặc 1 mảng các file:
Storage::delete('file.jpg');
Storage::delete(['file1.jpg', 'file2.jpg']);
  • files(): trả về một mảng các files trong một thư mục.
$files = Storage::files($directory);
  • allFiles(): Sẽ lấy danh sách tất cả các file trong một thư mục bao gồm các thư mục con:
$files = Storage::allFiles($directory);
  • directories(): Sẽ trả về một mảng gồm tất cả các thư mục bên trong một thư mục.
$directories = Storage::directories($directory);
  • allDirectories(): Hàm này lấy về danh sách tất cả các thư mục trong một thư mục và các thư mục con của nó:
$directories = Storage::allDirectories($directory);
  • makeDirectory(): Sẽ tạo một thư mục mới, bao gồm các thư mục con cần thiết:
Storage::makeDirectory($directory);
  • deleteDirectory(): Xoá một thư mục, bao gồm tất cả các file của nó:
Storage::deleteDirectory($directory);

Via: https://laravel.com/docs/5.3/

0